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
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    vector <string> a(n);
    for(int i = 0;i < n; ++i){
        cin >> a[i];
    }
    int mnoznik = 0;
    int previous_mnoz = 0;
    long long ans = 0;
    for(int i = 1; i < n; ++i){
        mnoznik = 0;
        int k;
        int w = min((int)a[i].size(), (int)a[i-1].size());
        int ok = 0;
        for(k = 0; k < w; ++k){
            if(a[i-1][k] != a[i][k]){
                if(a[i][k] > a[i-1][k]) ok = 1;
                else ok = -1;
                break;
            }
        }
        if(ok == 1){
            mnoznik = max(0, (int)a[i-1].size() + previous_mnoz - (int)a[i].size());
        }
        else if(ok == -1){
            mnoznik = max(0, (int)a[i-1].size() + previous_mnoz - (int)a[i].size() + 1);
        }
        else {
            string orga = a[i];
            if(a[i].size() <= a[i-1].size()){
                long long S = stol(a[i-1]);
                S += 1;
                string b = to_string(S);
                bool r = 0;
                for(int p = 0; p < (int)orga.size(); ++p){
                    if(b[p] != orga[p]){
                        r = 1; 
                    }
                }
                if(r){
                    mnoznik = max(0, (int)a[i-1].size() + previous_mnoz - (int)a[i].size() + 1);
                }
                else { 
                    a[i] = b;
                    ans += a[i].size() - orga.size();
                    mnoznik = max(0, (int)a[i-1].size() + previous_mnoz - (int)a[i].size());
                }
            }
        }

        ans += mnoznik;
        while(mnoznik > 0 && a[i].size() < 17){
            a[i] += '0';
            mnoznik -= 1;
        }
        previous_mnoz = mnoznik;
        //cerr << a[i] << ' '  << mnoznik << '\n';
    }
    cout << ans << '\n';
}