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

int main() {
    size_t N;
    std::cin >> N;

    std::string prev;
    std::cin >> prev;

    unsigned long long added = 0;
    for (size_t i = 1; i < N; ++i) {
        std::string next;
        std::cin >> next;

        bool less = false;
        bool more = false;

        size_t nexts = next.size();
        size_t prevs = prev.size();

        for (size_t j = 0; j < nexts && j < prevs && !less && !more; ++j) {
            if (next[j] < prev[j]) {
                less = true;
            } else if (next[j] > prev[j]) {
                more = true;
            }
        }

        if (nexts < prevs) {
            if (less) {
                prev = std::move(next);
                prev.append(prevs - nexts + 1, '0');
            } else if (more) {
                prev = std::move(next);
                prev.append(prevs - nexts, '0');
            } else {
                bool done = false;
                for (size_t k = prevs - 1; k >= nexts && !done; --k) {
                    if (prev[k] != '9') {
                        ++(prev[k]);
                        done = true;
                    } else {
                        prev[k] = '0';
                    }
                }
                if (!done) {
                    prev += '0';
                }
            }
        } else if (nexts > prevs) {
            prev = std::move(next);
        } else {
            if (less) {
                prev = std::move(next) + '0';
            } else if (more) {
                prev = std::move(next);
            } else {
                prev += '0';
            }
        }

        added += prev.size() - nexts;
    }

    std::cout << added << "\n";
}