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

#define lint long long

const lint small_part_mod = 1000 * 1000, leading_nonzeros = 10 * 1000 * 1000 * 1000;

struct almost_num {
    lint big_part, zeros;
};

int len(lint n) {
    int result = 0;
    while(n) {
        n /= 10;
        result++;
    }
    return result;
}

lint pow(lint base, lint exp) {
    lint result = 1;
    for(lint i = 0; i < exp; i++)
        result *= base;
    return result;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    lint n, result = 0, state, current;
    almost_num state_big;
    std::cin >> n;
    std::cin >> state;
    //std::cout << state << "\n";
    for(int i = 1; i < n; i++) {
        std::cin >> current;
        if(state < small_part_mod * leading_nonzeros / 10) {
            if(current > state) {
                state = current;
                //std::cout << state << "\n";
                continue;
            }

            int len_current = len(current), len_state = len(state);
            if(len_current == len_state) {
                result += 1;
                state = current * 10;
            } else {
                lint shift = pow(10, len_state - len_current);
                if(current == state / shift) {
                    if(current == (state + 1) / shift) {
                        result += len_state - len_current;
                        state += 1;
                    } else {
                        result += len_state - len_current + 1;
                        state = current * shift * 10;
                    }
                } else if(current > state / shift) {
                    result += len_state - len_current;
                    state = current * shift;
                } else {
                    result += len_state - len_current + 1;
                    state = current * shift * 10;
                }
            }
            //std::cout << state << "\n";
            state_big.big_part = state / small_part_mod;
            state_big.zeros = 6;
        } else {
            int len_current = len(current), len_state = state_big.zeros + 10;

            lint big_part_shift = pow(10, 10 - len_current);
            if(current > state_big.big_part / big_part_shift) {
                result += len_state - len_current;
                state_big.big_part = current * big_part_shift;
            } else if(current == state_big.big_part / big_part_shift) {
                result += len_state - len_current;
            } else {
                result += len_state - len_current + 1;
                state_big.big_part = current * big_part_shift;
                state_big.zeros += 1;
            }
        }
    }

    std::cout << result << "\n";
    return 0;
}