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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using ull = unsigned long long;

const int ACC = 18;
const int MAX_N = static_cast<int>(1e6 + 9);

unsigned tab[MAX_N];

ull sol = 0;
int n;

class Number {
public:
    explicit Number(unsigned num) : front(num) {}

    Number makeGreaterThan(unsigned num) {
        auto result = Number(num);

        result.zeroes = this->zeroes;
        sol += result.zeroes;

        auto numFrontStr = std::to_string(result.front);
        auto possibleFrontStr = std::to_string(this->front + 1);

        auto mismatch = std::mismatch(numFrontStr.begin(), numFrontStr.end(), possibleFrontStr.begin());

        if (mismatch.first == numFrontStr.end()) {
            result.front = this->front + 1;
            sol += possibleFrontStr.length() - numFrontStr.length();
            return result;
        }

        while(result.front < this->front && numFrontStr.length() < ACC) {
            result.front *= 10;
            numFrontStr += '0';
            ++sol;
        }

        if(result.front > this->front) {
            return result;
        }
        else if(result.front == this->front) {
            // UNSURE
            auto newBackStr = std::to_string(this->back + 1);

            if(newBackStr.length() <= result.zeroes) {
                result.back = this->back + 1;
                return result;
            }
        }

        if(numFrontStr.length() < ACC) {
            result.front *= 10;
            ++sol;
        }
        else {
            ++result.zeroes;
            ++sol;
        }
        return result;
    }

    friend std::ostream& operator<<(std::ostream& os, const Number& number) {
        return os << "[" << number.front << "]{0x" << number.zeroes << "}[" << number.back << "]";
    }

private:
    ull front;
    ull back{0};
    int zeroes{0};
};

int main() {
    std::cin >> n;

    for(int i = 0; i < n; ++i) {
        std::cin >> tab[i];
    }

    std::vector<Number> numbers {Number(tab[0])};

    for(int i = 1; i < n; ++i) {
        numbers.push_back(numbers.back().makeGreaterThan(tab[i]));
    }

    std::cout << sol << std::endl;

    return 0;
}