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

long long zera[2], val[2], result;

int main()
{ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;

for(int i = 0; i<n; i++){
    int p = (i+1)%2;
    int t = i%2;
    cin>>val[t];
    if(val[t]>val[p]){
        continue;
    }
    if(val[t]==val[p]){
        val[t]*=10;
        result++;
        continue;
    }
    string pop = to_string(val[p]);
    string ter = to_string(val[t]);
    string pref_pop = pop.substr(0, ter.size());
    int razy = pop.size()-ter.size();

    if(ter.compare(pref_pop)>0){
        ter.append(razy, '0');
        val[t] = stoll(ter);
        zera[t] = zera[p];
        result+=razy+zera[t];
        continue;
    }
    if(ter.compare(pref_pop)<0){
        if(pop.size()<17){
            ter.append(razy+1, '0');
            val[t] = stoll(ter);
            result+=razy+1;
            continue;
        }
        ter.append(razy, '0');
        val[t] = stoll(ter);
        zera[t] = zera[p]+1;
        result+=razy+zera[t];
        continue;
    }

    string next = to_string(val[p]+1);
    string pref_next = next.substr(0, ter.size());
    if(pref_next.compare(ter)!=0){
        ter.append(razy+1, '0');
        val[t] = stoll(ter);
        result+=razy+1;
        continue;
    }

    ter.assign(next);
    zera[t] = zera[p];
    val[t] = stoll(ter);
    result+=razy+zera[t];
}

cout<<result;
    return 0;
}