1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <bits/stdc++.h>

#define int long long

using namespace std;

struct liczba {
    list<pair<int, int> > v;
    int length;

    liczba( int x ) {
        vector<int> pom;
        while( x ) {
            pom.push_back( x%10 );
            x /= 10;
        }
        reverse( pom.begin(), pom.end() );
        v.push_back( {0, 0} );
        length = 0;
        for( int i : pom ) {
            length++;
            v.push_back( {length, i} );
        }
    }

    void napraw() {
        auto it = v.begin();
        it++;
        while( it != v.end() ) {
            if( it->second == 0 ) {
                it = v.erase( it );
            } else {
                it++;
            }
        }
    }

    bool add_one() {
        if( length == 0 ) return 0;
        if( v.back().first == length ) v.back().second++;
        else v.push_back( {length, 1} );
        auto it = v.end();
        it--;
        auto it2 = it;
        while( it->second > 9 ) {
            it->second = 0;
            it2 = it;
            it2--;
            if( it2->first+1 != it->first ) {
                v.insert( it, {it->first-1, 0} );
            }
            it--;
            it->second++;
            if( it->first == 0 ) {
                //cout << "niet" << endl;
                return false;
            }
        }
        return true;
    }

    int cmp( liczba & other ) {
        if( length > other.length ) return 1;
        if( length < other.length ) return -1;
        napraw();
        other.napraw();
        auto it = v.begin(), it2 = other.v.begin();
        it++;
        it2++;
        while( it != v.end() && it2 != other.v.end() ) {
            if( it->first == it2->first ) {
                if( it->second > it2->second ) return 1;
                if( it->second < it2->second ) return -1;
            } else {
                if( it->first < it2->first ) return 1;
                if( it->first > it2->first ) return -1;
            }
            it++;
            it2++;
        }
        if( it == v.end() && it2 == other.v.end() ) return 0;
        if( it == v.end() ) return -1;
        return 1;
    }

    bool foo( int x ) {
        napraw();
        int a=0;
        int xx=x;
        int li=0;
        while( xx ) {
            li++;
            xx/=10;
        }
        auto it=v.begin();
        it++;
        int d=1;
        while( it != v.end() && it->first <= li ) {
            while( d<it->first ) {
                a = a*10;
                d++;
                //cout << "sadf" << endl;
            }
            a += it->second;
            it++;
        }
        while( d<li ) {
            a = a*10;
            d++;
        }
        //wypisz();
        //cout << a << endl;
        return a==x;
    }

    /*void debug() {
        cout << length << endl;
        for( auto i : v ) {
            cout << i.first << " " << i.second << "   ";
        }
        cout << endl;
    }

    void wypisz() {
        auto it = v.begin();
        it++;
        int d=1;
        while( it != v.end() ) {
            while( d<it->first ) {
                cout << 0;
                d++;
            }
            cout << it->second;
            d++;
            it++;
        }
        while( d<=length  ) {
            cout << 0;
            d++;
        }
        cout << endl;
    }*/
};

int n;

int32_t main() {
    ios_base::sync_with_stdio( 0 );
    cin.tie( 0 );

    liczba ost( 0 );
    long long res=0;

    cin >> n;
    for( int a, i=1; i<=n; i++ ) {
        cin >> a;
        liczba pom( a );
        res += max( ost.length-pom.length, 0ll );
        pom.length = max( pom.length, ost.length );
        int x = pom.cmp( ost );
        //ost.wypisz();
        //ost.debug();
        //cout << endl;
        //cout << x << endl;
        //ost.debug();
        //pom.debug();
        //cout << res << endl;
        if( x == 1 ) {
            ost = pom;
        } else {
            //cout << "sadf" << endl;
            //cout << "added" << endl;
            if( !ost.add_one() || !ost.foo( a ) ) {
                //cout << "fas" << endl;
                ost = pom;
                ost.length++;
                res++;
            }
        }
        //cout << a << endl;
        //ost.wypisz();
        //cout << endl;
        //cout << res << endl << endl;
    }

    cout << res;

    return 0;
}
/*
13
1 1 1
1 1 1
1 1 1
1 1 1 1
*/