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
#include <iostream>
using namespace std;

int main(){
    int n;
    cin >> n;

    string last;
    cin >> last;
    int ln = last.length();

    long long int result = 0;

    for(int i = 1; i < n; i++)
    {
        string current;
        cin >> current;
        int cn = current.length();
        if(cn <= ln){
            int dif = 0;
            for(; last[dif] == current[dif] && dif < cn; ++dif)
                ;

            if( dif < cn )
            {
                if(last[dif] > current[dif]){
                    string zeroAppend = string(ln-cn+1,'0');
                    result += ln-cn+1;
                    current = current.append(zeroAppend);
                    cn = ln+1;
                } else {
                    string zeroAppend = string(ln-cn,'0');
                    if(ln-cn != 0)
                        result += ln-cn;
                    current = current.append(zeroAppend);
                    cn = ln;
                }
            } else {
                int nonNine = ln-1;
                for(; last[nonNine] == '9' && nonNine >= cn; nonNine-- )
                    ;

                string first = last.substr(cn, max(0, nonNine-cn));
                
                string mid = "0";

                string third = "";

                if(nonNine >= cn) {
                    mid[0] = (last[nonNine]+1);
                    third = string(ln-nonNine-1, '0');
                } else {
                    third = string(ln-cn, '0');
                }

                current = current.append(first).append(mid).append(third);
                int add = first.length() + mid.length() + third.length();
                result += add;
                cn += add; 
            }
        }

        ln = cn;
        last = current;
    }
    
    cout << result << endl;

    return 0;
}

// #include <stdio.h>
// const int val = 5e2;
// int main() {
// const int n = 2e5;
// printf("%d\n", n);
// for (int i = 0; i < n; ++i)
// printf("%lld\n", 1 + ((i * 1ll * i) ^ i ^ (i + 17)) % val);
// }