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
#include <deque>
#include <stdio.h>

constexpr auto MAX_DIGITS = 15;

class Digit {
public:
    Digit(int value)
        : m_power(0)
    {
        while (value > 0) {
            m_digits.push_front(value % 10);
            value /= 10;
        }
    }

    friend bool operator<(Digit const& lhs, Digit const& rhs)
    {
        if (lhs.m_power == rhs.m_power) {
            if (lhs.m_digits.size() == rhs.m_digits.size()) {
                auto lhsIt = lhs.m_digits.cbegin();
                auto rhsIt = rhs.m_digits.cbegin();
                while (lhsIt != lhs.m_digits.cend()) {
                    if (*lhsIt != *rhsIt) {
                        return *lhsIt < *rhsIt;
                    }
                    lhsIt++;
                    rhsIt++;
                }
                return false;
            } else {
                return lhs.m_digits.size() < rhs.m_digits.size();
            }
        } else {
            return lhs.m_power < rhs.m_power;
        }
    }

    friend bool operator<=(Digit const& lhs, Digit const& rhs)
    {
        return lhs < rhs || lhs == rhs;
    }

    friend bool operator==(Digit const& lhs, Digit const& rhs)
    {
        return (lhs.m_power == rhs.m_power && lhs.m_digits == rhs.m_digits);
    }

    friend bool isSuffix(const Digit& value, const Digit& max)
    {
        auto valueIt = value.m_digits.cbegin();
        auto maxIt = max.m_digits.cbegin();
        while (valueIt != value.m_digits.cend()) {
            if (*valueIt != *maxIt) {
                return false;
            }
            valueIt++;
            maxIt++;
        }
        return true;
    }

    void multiplyByPowerOfTen(int power)
    {
        while (m_digits.size() < MAX_DIGITS && power > 0) {
            m_digits.push_back(0);
            power--;
        }
        m_power += power;
    }

    void increment()
    {
        if (m_power == 0) {
            for (auto it = m_digits.rbegin(); it != m_digits.rend(); ++it) {
                if (*it < 9) {
                    (*it)++;
                    break;
                } else {
                    *it = 0;
                }
            }
        }
    }

    int getDigitsCount() const
    {
        return static_cast<int>(m_digits.size()) + m_power;
    }

private:
    std::deque<char> m_digits;
    int m_power;
};

void process(const Digit& value, Digit& max, long long int& result)
{
    if (max < value) {
        max = value;
        return;
    }

    Digit _max(max);
    _max.increment();
    if (isSuffix(value, _max)) {
        max = _max;
        result += max.getDigitsCount() - value.getDigitsCount();
    } else {
        const int digitsDiff = max.getDigitsCount() - value.getDigitsCount();
        _max = value;
        _max.multiplyByPowerOfTen(digitsDiff);
        result += digitsDiff;

        while (_max <= max) {
            _max.multiplyByPowerOfTen(1);
            result++;
        }
        max = _max;
    }
}

int main()
{
    int n, value;
    Digit max(0);
    scanf("%d", &n);
    long long int result = 0;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &value);
        process(Digit(value), max, result);
    }
    printf("%lld\n", result);
    return 0;
}