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
#include <iostream>

using namespace std;

typedef unsigned long long ull;

ull countDigits(ull x) {
    ull result = 0;
    while(x != 0) {
        result++;
        x /= 10;
    }
    return result;
}

bool isPrefixOf(ull s, ull s_digits, ull l, ull l_digits) {
    while (l_digits > s_digits) {
        l /= 10;
        l_digits--;
    }
    return l == s;
}

ull padToDigits(ull s, ull s_digits, ull target_digits) {
    for(ull i = 0; i < (target_digits - s_digits); i++) {
        s *= 10;
    }
    return s;
}


int main() {
    ios_base::sync_with_stdio(false);
    int n;

    cin >> n;

    ull prev = 0;
    ull prev_digits = 1;
    ull curr;
    ull curr_digits;
    ull result = 0;
    ull temp;
    ull temp_digits;

    for(int i = 0; i < n; i++) {
        cin >> curr;
        curr_digits = countDigits(curr);

        if(curr <= prev) {
            if(curr_digits >= prev_digits) {
                curr *= 10;
                curr_digits++;
                result++;
            }
            else {
                if(isPrefixOf(curr, curr_digits, prev, prev_digits)) {
                    temp = prev;
                    temp++;
                    temp_digits = countDigits(temp);

                    if(isPrefixOf(curr, curr_digits, temp, temp_digits)) {
                        result += (temp_digits - curr_digits);
                        curr_digits = temp_digits;
                        curr = temp;
                    }
                    else {
                        result += (prev_digits - curr_digits) + 1;
                        curr = padToDigits(curr, curr_digits, prev_digits + 1);
                        curr_digits = prev_digits + 1;
                    }
                }
                else {
                    result += (prev_digits - curr_digits);
                    curr = padToDigits(curr, curr_digits, prev_digits);
                    curr_digits = prev_digits;

                    if(curr <= prev) {
                        curr *= 10;
                        curr_digits++;
                        result++;
                    }

                }
            }
        }

//        cout << curr << endl;

        prev = curr;
        prev_digits = curr_digits;

    }

    cout << result << endl;

    return 0;
}