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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <string>

using namespace std;

string pre, cur;
int zeros_in_memory = 0;

inline void read_next() {
    pre = cur;
    cin >> cur;
}

int additional_digits() {
    int n = pre.size(), m = cur.size();
    if (n + zeros_in_memory < m) {
        zeros_in_memory = 0;
        return 0;
    }

    int i;
    for (i = 0; i < m && i < n; ++i) { // zoptym. nie dopisywanie zer
        if (pre[i] < cur[i]) {
            int len = n + zeros_in_memory - m;
            zeros_in_memory = len;
            // string fill(len, '0');
            // cur += fill;
            return len;
        }
        if (pre[i] > cur[i]) {
            int len = n + zeros_in_memory + 1 - m;
            zeros_in_memory = len;
            // string fill(len, '0');
            // cur += fill;
            return len;
        }
    }
    if (zeros_in_memory > 0) {
        for (i = n; i < m && i < n + zeros_in_memory; ++i) {
            if ('0' < cur[i]) {
                int len = n + zeros_in_memory - m;
                zeros_in_memory = len;
                return len;
            }
        }
        
    }
    if (i == n + zeros_in_memory) {// to i i = n cos nie gra dla zeros = 0
        //cerr << "i == n + zeros_in_memory" << endl;
        zeros_in_memory = 1;
        // cur += '0';
        return 1;
    }

    // check for situation 9...9 in pre on remaining positions
    int first_diff = m;
    int rightmost_non_nine = 0;
    if (zeros_in_memory > 0) {
        rightmost_non_nine = n + zeros_in_memory - 1;
    } else {
        bool all_nines = true;
        for (i = n - 1; i >= m; --i) {
            if (pre[i] != '9') {
                all_nines = false;
                break;
            }
        }
        // string nines = string(n - m, '9');
        // if (pre.compare(m, n - m, nines) == 0) {
        if (all_nines) {
            int len = n - first_diff + 1;
            zeros_in_memory = len;
            // string fill(len, '0');
            // cur += fill;
            return len;
        }
        rightmost_non_nine = i;
    }
    
    if (n > m) {
        string fill = pre.substr(m);
        cur += fill;
    }
    
    // not 9...9 so increment the rightmost non 9
    // for (i = first_diff; i < n; ++i) {
    for (i = n - 1; i >= first_diff; --i) {
        if (i > rightmost_non_nine) {
            cur[i] = '0';
        } else if (i == rightmost_non_nine) {
            cur[i] = pre[i] + 1;
            break;
        } else {
            break;
        }
    }

    int added_digits = n + zeros_in_memory - first_diff;;
    if (n <= rightmost_non_nine) {// last zero in mem is first non nine
        string zeros(added_digits - 1, '0');
        cur += zeros + '1';
        zeros_in_memory = 0;
    }

    return added_digits;
}

int main() {
    ios::sync_with_stdio(false);
    // cin.tie(nullptr);

    int n;
    unsigned long long count = 0;
    cin >> n;

    read_next();
    if (n == 1) {
        cout << "0\n";
        return 0;
    }
    
    for (int i = 1; i < n; ++i) {
        read_next();
        unsigned long long add = additional_digits();
        //cerr << i  << " idx: " <<"digits added: " << add << " zeros in mem: " << zeros_in_memory << " cur: " << cur<< endl;
        count += add;
    }

    cout << count << "\n";

    return 0;
}

/*
3
5999
5
5999 
out 5 ok

13
5999
5
5999
5999
5999
5999
5999
5999
5999
5999
5999
5999
5999
*/