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
//Piotr Kłpotowski
#include <iostream>
#include <string>
using namespace std;

long long writeBack(long long, int);
int degree(long long);
long long subInt(long long, int, int);
bool expandable(long long);
int n;

int changesCount = 0;

int main() {
    cin >> n;
    int a[n];

    long long prev = 0;

    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    for(int i = 0; i < n; i++){
        long long c = a[i];
        if(c <= prev) {
            int degreeC = degree(c);

            int degreePrev = degree(prev);
            int degreeDiff = degreePrev-degreeC;

            if(degreeDiff == 0) {
                prev = writeBack(c, 0);
                changesCount++;
                continue;
            }
            else if(degreeDiff > 0){
                //Compare bases
                long long basePrev = subInt(prev, 1, degreeC);
                

                if(basePrev == c){
                    //Take the rest
                    long long rest = subInt(prev, degreeC + 1, degreeDiff);

                    if(expandable(rest)){
                        c = writeBack(c, rest+1);
                        changesCount += degreeDiff;
                    } else {
                        for(int j = degreeDiff; j <= degreePrev; j++){
                            c = writeBack(c, 0);
                            changesCount++;
                        }
                    }
                    prev = c;
                    continue;
                }
                else if(basePrev > c){
                    for(int j = degreeDiff; j <= degreePrev; j++){
                        c = writeBack(c, 0);
                        changesCount++;
                    }
                    prev = c;
                    continue;
                }
                else { // c > basePrev
                    for(int j = degreeDiff; j < degreePrev; j++){
                        c = writeBack(c, 0);
                        changesCount++;
                    }

                    prev = c;
                    continue;
                }
            }
        } else prev = c;
    }

    cout << changesCount;
    return 0;
}

long long writeBack(long long a, int b){
    return stoll(to_string(a) + to_string(b));
}

int degree(long long i){
    return to_string(i).length();
}

long long subInt(long long a, int start, int length){
    string s = to_string(a);
    return stoll(s.substr(start+1, length));
}

bool expandable(long long i){
    return to_string(i+1).size() == to_string(i).size();
}