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
//Aleksander Łukasiewicz
#include<bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

const int INF = 1000000009;

const int MAXN = 200*1000;

int n;
string tab[MAXN + 3];

inline bool isPrefix(string &A, string &B){
    return (int)A.size() < (int)B.size() && A == B.substr(0, (int)A.size());
}

int main(){
    ios_base::sync_with_stdio(0);
    cin>>n;
    for(int i=0; i<n; i++)
        cin>>tab[i];
    
    LL ans = 0;
    string curr = tab[0];
    int currlen = (int)curr.size();
    for(int i=1; i<n; i++){
        if(curr.size() > 16){
            //cout << tab[i-1] << " " << tab[i] << " " << currlen << " " << (tab[i-1] > tab[i]) << " " << isPrefix(tab[i], tab[i-1]) <<"\n";
            if(tab[i-1] > tab[i] && !isPrefix(tab[i], tab[i-1]))
                currlen++;
            //cout << currlen << "\n";
            //cout << currlen - (int)tab[i].size() << "\n";
            ans += currlen - (int)tab[i].size();
            
            if(isPrefix(tab[i], tab[i-1]))
                tab[i] = tab[i-1];
        }
        else{
            string cand = tab[i];
            if(tab[i] > curr){
                while((int)cand.size() < (int)curr.size())
                    cand += "0";
            }
            else if(isPrefix(tab[i], curr)){
                LL a = stoll(curr);
                //cout << tab[i] << " " << curr << " " << a << "\n";
                a++;
                cand = to_string(a);
                //cout << cand << "\n";
                if(!isPrefix(tab[i], cand)){
                    cand = tab[i];
                    while((int)cand.size() <= (int)curr.size())
                        cand += "0";
                }
            }
            else{
                while((int)cand.size() <= (int)curr.size())
                    cand += "0";
            }
            
            curr = cand;
            currlen = (int)curr.size();
            ans += currlen - (int)tab[i].size();
            //cout << curr << "\n";
        }
    }
    
    cout << ans << "\n";
    
return 0;
}