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
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define umap unordered_map
#define uset unordered_set
#define watch(x) cerr << (#x) << " is " << (x) << endl
#define all(a) begin(a),end(a)

using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
const double oo=1e15;
const double eps=1e-8;
const double pi=acos(-1.0);

template<typename T> T sqr(T a_) { return a_*a_; }
template<typename T> T cub(T a_) { return a_*a_*a_; }

// inline void readI(int*n){register char c=0,sign=1;while(c<33)c=getchar_unlocked();if(c=='-'){sign=-1;c=getchar_unlocked();}*n=0;while(c>33){*n*=10;*n+=c-'0';c=getchar_unlocked();}*n*=sign;}
// inline void printI(int n){if(!n){putchar_unlocked('0');return;}if(n<0){putchar_unlocked('-');n*=-1;}char digits[12];int i=0;while(n){digits[i++]=(n%10)+'0';n/=10;}while(i--)putchar_unlocked(digits[i]);}
// inline void readLL(long long*n){register char c=0,sign=1;while(c<33)c=getchar_unlocked();if(c=='-'){sign=-1;c=getchar_unlocked();}*n=0;while(c>33){*n*=10;*n+=c-'0';c=getchar_unlocked();}*n*=sign;}
// inline void printLL(long long n){if(!n){putchar_unlocked('0');return;}if(n<0){putchar_unlocked('-');n*=-1;}char digits[24];int i=0;while(n){digits[i++]=(n%10)+'0';n/=10;}while(i--)putchar_unlocked(digits[i]);}
// inline void readS(string*s){register char c=0;while(c<33)c=getchar_unlocked();while(c>=33){*s+=c;c=getchar_unlocked();}}
// inline void printS(string s){for(auto c:s)putchar_unlocked(c);}

typedef long double ld;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

//-----------------------------------------------//

const int N=0;
const int M=0;
const int K=0;

vector<ll> V;
vector<ll> v;
ll n,l,x,y,res,z,t;
int ok;
string a,b;
ll po[18];

ll cn(ll xx){
    ll re=0;
    while(xx>0){
        re++;
        xx/=10;
    }
    return re;
}

int main(){

    ios::sync_with_stdio(0);
    cin.tie(0);

    cin>>n;

    //prep
    po[0]=1;
    for(int i=1; i<=17; i++){
        po[i]=po[i-1]*10;
    }

    //input
    while(n--){
        cin>>x; V.PB(x);
        y=0;
        while(x>0){
            y++;
            x/=10;
        }
        v.PB(y);
    }

    //solve
    // ll pres=0;
    for(ll i=1; i<V.size(); i++){
        //
        // watch(res-pres);
        // cout<<res-pres<<endl;
        // pres=res;
        //
        // cout<<"v: "<<v[i-1]<<" "<<v[i]<<endl;
        // cout<<"V: "<<V[i-1]<<" "<<V[i]<<endl;
        x=V[i-1];
        y=V[i];
        if(v[i-1]<v[i]) continue;
        if(v[i-1]==v[i] && y>x) continue;
        if(v[i-1]==v[i] && x==y){
            if(v[i]<18) V[i]*=10;
            v[i]++;
            res++;
            continue;
        }
        a=b="";
        while(x>0){
            a=(char)(x%10+'0')+a;
            x/=10;
        }
        while(y>0){
            b=(char)(y%10+'0')+b;
            y/=10;
        }
        // cout<<"x"<<a<<" "<<b<<endl;
        ok=2;
        for(ll j=0; j<v[i]; j++){
            if(a[j]>b[j]){
                ok=0; break;
            }
            else if(a[j]<b[j]){
                ok=1; break;
            }
        }
        //a>b
        if(ok==0){
            l=18-cn(V[i]);
            V[i]*=po[min(l,v[i-1]-v[i]+1)];
            res+=(v[i-1]-v[i])+1;
            v[i]=v[i-1]+1;
            continue;
        }
        //b<a
        else if(ok==1){
            l=18-cn(V[i]);
            V[i]*=po[min(l,v[i-1]-v[i])];
            res+=v[i-1]-v[i];
            v[i]=v[i-1];
            continue;
        }
        //a==b
        else{
            // cout<<"tu:"<<v[i-1]<<endl;
            z=v[i-1]-v[i];
            res+=z;
            v[i]+=z;
            if(v[i-1]>18){
                V[i]=V[i-1];
                continue;
            }
            t=V[i-1]%po[z];
            t++;
            if(t!=po[z]){
                V[i]*=po[z];
                V[i]+=t;
            }
            else{
                V[i]*=po[min(18-cn(V[i]), z+1)];
                v[i]++;
                res++;
            }
        }
    }

    cout<<res;

    return 0;
}

//-----------------------------------------------//