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

using namespace std;

void getNumberOfAddedDigits(long long a[], int n) {
    long long digits = 0;
    long long next = 0;
    long long prev = 0;
    
    for (int i = 0; i < n-1; i++) 
    {
        long long temp = a[i+1];
        if (a[i+1] <= a[i]) {
            string s1 = to_string(a[i]);
            string s2 = to_string(a[i+1]);
            int size1 = s1.size();
            int size2 = s2.size();
            
            if (size2 == size1) 
            {
                string addedStr = to_string(next);
                s2 += addedStr;
                a[i+1] = stoll(s2);
                next++;
                digits += addedStr.size();
            } 
            else 
            {
                string addedStr;
                if (prev > a[i+1]) {
                    addedStr = string(to_string(prev).size() - size2, '0') + "0";
                    s2 += addedStr;                                        
                }
                else 
                {
                    int diff = size1 - size2;
                    addedStr = to_string(stoll(s1.substr(size2 - diff + 1, diff))+1);                    
                    s2 += string(diff - addedStr.size(), '0') + addedStr;                    
                }
                a[i+1] = stoll(s2);
                next = 0;                
                digits += addedStr.size();
            }
        }
        prev = temp;

    }

    // TODO: remove
//    for (int i = 0; i < n; i++) {
//        cout << a[i] << " ";
//    }
//    cout << endl; // TODO: remove!
    cout << digits;
}

long long arr[200000];
int main(int argc, char**argv) {
    int n = 1;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    getNumberOfAddedDigits(arr, n);
   
    return 0;
}