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
184
185
186
187
//Krzysztof Pieprzak
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define Size(x) (int)x.size()
#define VAR(v,n) auto v = (n)
#define FOR(i,a,b) for(VAR(i,a); i < (b); ++i)
#define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i)
#define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i)
#define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step))
#define FOREACH(i,c) for(auto i : (c))
#define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i)
#define ALL(x) x.begin(),x.end()
#define CLEAR(t) memset(t, 0, sizeof(t))
#define F first
#define S second
#define MP make_pair
#define PUB push_back
#define POB pop_back
#define pieprzu ios_base::sync_with_stdio(0);
#define debug if (false)
#define cout debug cout

const int    INF = 1000000001;
const double EPS = 10e-9;

int countLen(ll x) {
    if (x == 0) return 1;

    int len = 0;
    for (; x > 0; x /= 10) ++len;
    return len;
}

ll pow10(int x) {
    ll res = 1;
    for (; x > 0; --x) res *= 10;
    return res;
}

bool isMaxTail(ll tail, int tailLen) {
    return tailLen == 0 || (tailLen < 9 && tail == pow10(tailLen) - 1);
}

ll getTailPref(ll tail, int tailLen, int prefLen) {
    int sufLen = tailLen - prefLen;
    return sufLen < 9 ? tail / pow10(sufLen) : 0;
}

ll getPref(ll number, int numberLen, ll tail, int tailLen, int prefLen) {
    if (prefLen > numberLen) {
        int tailPrefLen = prefLen - numberLen;
        ll tailPref = getTailPref(tail, tailLen, tailPrefLen);
        return number * pow10(tailPrefLen) + tailPref;
    } else {
        return number / pow10(numberLen - prefLen);
    }
}

ll getTailSuf(ll tail, int sufLen) {
    return sufLen < 9 ? tail % pow10(sufLen) : tail;
}

void rob(int test) {
    int n;
    scanf("%d", &n);

    ll curr, prev, tail = 0;
    int currLen, prevLen, tailLen = 0;
    ll res = 0;
    
    scanf("%lld", &prev);
    prevLen = countLen(prev);

    FOR (i, 1, n) {
        scanf("%lld", &curr);
        currLen = countLen(curr);

        cout << "BEFORE: " << "prev=" << prev << ", prevLen=" << prevLen << ", curr=" << curr << ", currLen=" << currLen
             << ", tail=" << tail << ", tailLen=" << tailLen << endl;

        if (currLen > prevLen + tailLen) {
            // prev: 123, tail: 00
            // curr: 123456789
            // result number: 123456789, result tail: none
            tail = 0;
            tailLen = 0;
        } else {
            ll prevPlusTailPref = getPref(prev, prevLen, tail, tailLen, currLen);
            int currTailLen = prevLen + tailLen - currLen;
            
            if (prevPlusTailPref < curr) {
                cout << "1" << endl;
                // prev: 123, tail: 00
                // curr: 4
                // result number: 4, result tail: 0000
                tail = 0;
                tailLen = currTailLen;
            } else if (prevPlusTailPref > curr) {
                cout << "2" << endl;
                // prev: 123, tail: 00
                // curr: 11
                // result number: 11, result tail: 0000
                tail = 0;
                tailLen = currTailLen + 1;
            } else if (prevLen < currLen) {
                ll tailSuf = getTailSuf(tail, currTailLen);
                if (isMaxTail(tailSuf, currTailLen)) {
                    cout << "3.1" << endl;
                    // prev: 12, tail: 09
                    // curr: 120
                    // result number: 120, result tail: 00
                    tail = 0;
                    tailLen = currTailLen + 1;
                } else {
                    cout << "3.2" << endl;
                    // prev: 12, tail: 00
                    // curr: 120
                    // result number: 120, result tail: 1
                    ++tail;
                    tailLen = currTailLen;
                }
            } else {
                int prevSufLen = prevLen - currLen;
                ll prevSuf = prev % pow10(prevSufLen);
                if (isMaxTail(tail, tailLen)) {
                    if (isMaxTail(prevSuf, prevSufLen)) {
                        cout << "4.1.1" << endl;
                        // prev: 129, tail: 9
                        // curr: 12
                        // result number: 12, result tail: 000
                        tail = 0;
                        tailLen = prevSufLen + tailLen + 1;
                    } else {
                        cout << "4.1.2" << endl;
                        // prev: 123, tail: 9
                        // curr: 12
                        // result number: 124, result tail: 0
                        res += prevSufLen;
                        curr = curr * pow10(prevSufLen) + prevSuf + 1;
                        currLen += prevSufLen;
                        tail = 0;
                    }
                } else {
                    cout << "4.2" << endl;
                    // prev: 123, tail: 0
                    // curr: 12
                    // result number: 123, result tail: 1
                    res += prevSufLen;
                    curr = curr * pow10(prevSufLen) + prevSuf;
                    currLen += prevSufLen;
                    ++tail;
                }
            }
        }
            
        res += tailLen;
        cout << "AFTER: " << "prev=" << prev << ", prevLen=" << prevLen << ", curr=" << curr << ", currLen=" << currLen
             << ", tail=" << tail << ", tailLen=" << tailLen << ", res=" << res << endl;

        prev = curr;
        prevLen = currLen;
    }

    printf("%lld\n", res);
}

int main() {
    int test = 1;
    //scanf("%d", &test);

    FORE (i, 1, test) rob(i);

    return 0;
}