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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

long long prefix[N], sufix[N], in[N];

inline int getLength(long long x){
    int result = 0;
    while(x > 0){result++; x /= 10;}
    return result;
}
inline int pre(long long x, int y){
    int d = 1;
    for(int i = 0; i < y; i++){d *= 10;}
    while(x >= d){x /= 10;}
    return x;
}

inline pair<long long, int> kon(long long x, int y){
    int d = 1;
    vector<int> r;
    for(int i = 0; i < y; i++){d *= 10;}
    while(x >= d){r.push_back(x % 10); x /= 10;}
    if(r.size() == 0){
        return make_pair(-1, 0);
    }
    long long re = r[r.size() - 1];
    
    for(int i = r.size() - 2; i >= 0; i--){re *= 10; re += r[i];}
    return make_pair(re, r.size());
}

inline bool check(long long x){
    int s = 9;
    // jeszcze sprawdź czy nie większe niż 1e9
    for(int i = 0; i < 8; i++){
        if(x == s) return true;
        s *= 10;
        s += 9;
    }
    return false;
}

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

    long long result = 0;
    in[0] = getLength(prefix[0]);
    for(int i = 1; i < n; i++){
        int curr_length = getLength(prefix[i]);
        //cerr << prefix[i - 1] << "\t" << in[i - 1] << "<<<<<\n";
        //cerr << prefix[i] <<  "\n";
        // if(curr_length <= in[i - 1]){
        //     cerr << prefix[i] << "\t" << pre(prefix[i - 1], curr_length) << "\n";
        // }
        in[i] = curr_length;
        if(curr_length <= in[i - 1] && pre(prefix[i - 1], curr_length) == prefix[i]){
            if(in[i - 1] <= 10){
            //cerr << pre(prefix[i - 1], curr_length) << "\n";
                pair<int, int> r = kon(prefix[i - 1], curr_length);
                int l;
                //cout << r.first << " " << r.second << "\n";
                if(r.first != -1){
                        if(check(r.first) && r.second == getLength(r.first)){
                            r.first++;
                            l = r.second;
                            for(int j = 0; j <= l; j++){
                                
                                prefix[i] *= 10;
                                result++;
                                in[i]++;
                                //cout << "MNORZENIE\n" << prefix[i] << "\n";
                            }
                        } else {
                            r.first++;
                            l = r.second;
                            //cerr << r << "kurwyy\n";
                            for(int j = 0; j < l; j++){
                                
                                prefix[i] *= 10;
                                result++;
                                in[i]++;
                                //cout << "MNORZENIE\n" << prefix[i] << "\n";
                            }
                            prefix[i] += r.first;
                        }
                    //}
                } else if(curr_length < 10){
                    prefix[i] *= 10;
                    result++;
                    in[i]++;
                }
            } else {
                pair<int, int> r = kon(prefix[i - 1], curr_length);
                int l;
                //cout << r.first << " " << r.second << "\n";
                if(r.first != -1){
                        if(check(r.first) && r.second == getLength(r.first)){
                            
                            l = r.second;
                            for(int j = 0; j <= l; j++){
                                prefix[i] *= 10;
                                result++;
                                in[i]++;
                                //cout << "MNORZENIE\n" << prefix[i] << "\n";
                            }
                        } else {
                            
                            l = r.second;
                            //cerr << r << "kurwyy\n";
                            for(int j = 0; j < l; j++){
                                
                                prefix[i] *= 10;
                                result++;
                                in[i]++;
                                //cout << "MNORZENIE\n" << prefix[i] << "\n";
                            }
                            prefix[i] += r.first;
                        }
                    //}
                } else if(curr_length < 10){
                    prefix[i] *= 10;
                    result++;
                    in[i]++;
                }
            }
            //cout << "TO MOJ PREFIX " << prefix[i] << "\n";
        } else if (curr_length <= in[i - 1]){
            while(in[i] < 10 && prefix[i] <= prefix[i - 1]){
                prefix[i] *= 10;
                result++;
                in[i]++;

            }
        }
        
        curr_length = getLength(prefix[i]);
        

        if(in[i - 1] < 11){
            if(prefix[i - 1] > prefix[i]){
                in[i]++;
                result++;
            } 
             //cout << result << "<\n";
            continue;
        }

        if(prefix[i - 1] < prefix[i]){
            //cout << prefix[i - 1] << " " << prefix[i] << " " << result << "\t" << "zostanie dodane " << in[i - 1] << " " << curr_length << "\n";
            result += in[i - 1] - curr_length;
            //if(in[i - 1] - curr_length < 0){"CHUUUUUUUUUUUUUUJ\n";}
            in[i] = in[i - 1];
            //cerr << "!";
        } else if(prefix[i - 1] == prefix[i]){
            int middle = max(0ll, in[i - 1] - getLength(prefix[i - 1] - getLength(sufix[i - 1])));
            if(check(sufix[i - 1]) && middle == 0){
                in[i] = in[i - 1] + 1;
                result += in[i - 1] - curr_length + 1;
                //if(in[i - 1] - curr_length + 1 < 0){"CHUUUUUUUUUUUUUUJ\n";}
                //cerr << "@";
            } else {
                sufix[i] = sufix[i] + 1;
                in[i] = in[i - 1];
                result += in[i - 1] - curr_length;
                //cerr << "#";
            }
        } else {
            result += in[i - 1] - curr_length + 1;
            in[i] = in[i - 1] + 1;
            //cerr << "$";
        }
        //cout << in[i] << "To ma byc rowne nastepnemu\n";
        //cout << result << "<----\n";
    }
    cout << result << "\n";
}