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
197
198
199
200
201
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <sstream>
#include <iostream>
#include <cstring>
#define REP(i,x,v)for(int i=x;i<=v;i++)
#define REPD(i,x,v)for(int i=x;i>=v;i--)
#define FOR(i,v)for(int i=0;i<v;i++)
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define IN(x,y) ((y).find((x))!=(y).end())
#define LOLDBG
#ifdef LOLDBG
#define DBG(vari) cout<<#vari<<" = "<<vari<<endl;
#define DBG2(v1,v2) cout<<(v1)<<" - "<<(v2)<<endl;
#else
#define DBG(vari)
#define DBG2(v1,v2)
#endif
#define CZ(x) scanf("%d",&(x));
#define CZ2(x,y) scanf("%d%d",&(x),&(y));
#define ALL(x) (x).begin(),(x).end()
#include <random>
#include <ctime>
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; }
//mt19937 rng(time(0));
#define tresh 10000000000000000LL
//mt19937 rng(41234);

ll p10[20];
pii zz=mp(0,0);

int num_len(ll x)
{
    int cnt=0;
    while(x)
    {
        if (x>1000000)
        {
            x/=1000000;
            cnt+=6;
        }
        else
        {
            x/=10;
            cnt++;
        }
    }
    return cnt;
}

ll pref_num(ll x,int k)
{
    int len_x = num_len(x);
    return x/p10[len_x-k];
}

void print_L(pair<ll,pii> L)
{
    while(L.fi%10==0)
    {
        L.se.fi++;
        L.fi/=10;
    }
    cout<<L.fi;
    if (L.se.se)
    {
        int nlen = num_len(L.se.se);
        FOR(i,L.se.fi - nlen) cout<<"0";
        cout<<L.se.se<<endl;
    }
    else
    {
        FOR(i,L.se.fi) cout<<"0";
        cout<<endl;
    }


}

pair<pair<ll,pii>, int > combine(pair<ll,pii> L, ll x)
{
    pair<ll,pii> N;
    int to_add = 0;
    int len_x = num_len(x);
    int len_p = num_len(L.fi);
    ll p1 = pref_num(L.fi, len_x);
    //DBG(L);
    //DBG(x);
    //DBG(p1);
    if (x>p1)
    {
        N.fi = x*p10[len_p - len_x];
        N.se = mp(L.se.fi, 0);
        to_add = len_p - len_x + L.se.fi;
    }
    else if (x==p1)
    {
        if (L.se.fi==0)
        {
            ll p2 = L.fi - p1*p10[len_p - len_x];
            if (num_len(p2+1)> len_p - len_x)// the 9999999 case
            {
                N.fi = x*p10[len_p - len_x];
                N.se = mp(1, 0);
                to_add = len_p - len_x +1;
            }
            else
            {
                N.fi = L.fi+1;
                N.se = mp(0, 0);
                to_add = len_p - len_x;
            }
        }
        else
        {
            N.fi = L.fi;
            N.se = mp(L.se.fi, L.se.se+1);
            to_add = len_p - len_x +L.se.fi;
        }
    }
    else if (x<p1)
    {
        N.fi = x*p10[len_p - len_x];
        N.se = mp(L.se.fi+1, 0);
        to_add = len_p - len_x + L.se.fi+1;
    }
    while (N.se.fi && N.fi<tresh)
    {
        N.se.fi-=1;
        N.fi*=10;
    }
    return mp(N, to_add);
}

ll solve(vi v)
{
    int n=v.sz;
    vector<pair<ll,pii> > S(n);
    pair<ll,pii> L=mp(0,zz);
    ll added = 0;
    FOR(i,n)
    {
        if (i) L = S[i-1];
        //DBG(L);
        ll x = v[i];
        if (x>L.fi)
        {
            S[i] = mp(x,zz);
            continue;
        }
        if (x==L.fi)
        {
            S[i] = mp(x*10, zz);
            added++;
            continue;
        }
        pair<pair<ll,pii>, int > p = combine(L,x);
        S[i]=p.fi;
        added += p.se;
    }/*
    FOR(i,n)
    {
        DBG(S[i]);
        print_L(S[i]);
    }*/
    //DBG(S[n-1]);
    return added;
}




int main()
{
    ios_base::sync_with_stdio(0);
    p10[0]=1;
    REP(j,1,18) p10[j]=p10[j-1]*10;

    int n;cin>>n;
    vi v(n);
    FOR(i,n) cin>>v[i];

    cout<<solve(v)<<"\n";

    return 0;
}