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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

char num_length(int n) {
    char result = 0;
    while (n>0) { ++result; n/=10; }
    return result;
}

int pow10(int k) {
    int result = 1;
    for(int i=0; i<k; ++i) result *= 10;
    return result;
}

struct Num {
    int x, w, p;
    Num(int x, int w = 0, int p = 0) { _init(x, w, p); }

    void _init(int x, int w, int p) {
        if (w < 0) w = 0;
        this->w = w;
        this->x = x;
        this->p = p;
        _clean();
    }

    void _clean() {
        char p_length = num_length(p);
        if (p_length > w) {
            int p10 = pow10(w);
            x += p / p10;
            p = p % p10;
        }

        while (x>0 && x%10==0) {
            x /= 10;
            w += 1;
        }
    }

    void inc() {
        ++p;
        _clean();
    }

    long long render(int a_len, int x_len = -1) {
        long long r = x;
        if (x_len == -1) x_len = num_length(x);
        if (a_len <= x_len) {
            for(int i=0; i<x_len-a_len; ++i) r /= 10;
        } else {
            for(int i=0; i<a_len-x_len; ++i) r *= 10;
            int p_len = num_length(p);
            if (w - p_len < a_len-x_len) {
                int shifted_p = p;
                for (int i=0; i<x_len+w-a_len; ++i) shifted_p /= 10;
                r += shifted_p;
            }
        }
        return r;
    }

    int extend(long long a) {
        int a_len = num_length(a);
        int x_len = num_length(x);
        if (a_len > x_len + w) { _init(a, 0, 0); return 0; }
        long long r = render(a_len);
        if (r == a) return x_len + w - a_len;
        int result = x_len + w - a_len;
        if (a < r) result += 1;
        _init(a, result, 0);
        return result;
    }
};

int n, a;
long long w;
int main() {
    w = 0;
    scanf("%d", &n);
    scanf("%d", &a); Num A(a);
    for (int i=1; i<n; ++i) {
        A.inc();
        scanf("%d", &a);
        w = w + A.extend(a);
    }
    printf("%lld\n", w);
}