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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#include <vector>
#include <cassert>
using namespace std;
const int64_t kInf = (1LL << 62);

int getDigitCount(int64_t x) {
    int ans = 0;
    while(x > 0) {
        ans += 1;
        x /= 10;
    }
    return ans;
}

struct BigNumber {
    int64_t head;
    int head_size;

    int middle_size;

    int64_t tail;
    int tail_size;
    
    BigNumber(int64_t _head, int _middle_size, int64_t _tail) {
        head = _head;
        middle_size = _middle_size;
        tail = _tail;

        head_size = getDigitCount(_head);
        tail_size = getDigitCount(_tail);
    }

    int getSize() const {
        return head_size + middle_size + tail_size;
    }

    int64_t smallView() const {
        if(getSize() > 18) 
            return -1LL;
        int64_t ans = head;
        for(int i = 0; i < middle_size + tail_size; i += 1)
            ans *= 10;
        ans += tail;
        return ans;
    }

    string print() {
        string tail_string = to_string(tail);
        if(tail_string == "0")
            tail_string = "";
        return to_string(head) + string(middle_size, '0') + tail_string;
    }

    int64_t getPrefix(int k) const {
        int64_t ans = 0;
        if(k <= head_size) {
            ans = head;
            for(int it = head_size; it > k; it -= 1) {
                ans /= 10;
            }
        } else if(k <= head_size + middle_size) {
            ans = head;
            for(int it = head_size; it < k; it += 1) {
                ans *= 10;
            }
        } else {
            ans = head;
            for(int it = 0; it < middle_size + tail_size; it += 1)
                ans *= 10;
            ans += tail;
            for(int it = head_size + middle_size + tail_size; it > k; it -= 1) {
                ans /= 10;
            }
        }
        return ans;
    }
    
    BigNumber increment() {
        int64_t small_x = smallView();

        if(small_x >= 0) {
            int64_t new_x = small_x + 1;
            if(getDigitCount(new_x) > getSize()) {
                return BigNumber(-1, -1, -1);
            } else {
                return BigNumber(new_x, 0, 0);
            }
        } else {
            if(tail == 0) {
                return BigNumber(head, middle_size - 1, 1);
            } else {
                int64_t new_tail = tail + 1;
                if(getDigitCount(new_tail) > tail_size) {
                    return BigNumber(head, middle_size - 1, new_tail);
                } else {
                    return BigNumber(head, middle_size, new_tail);
                }
            }
        }
        return BigNumber(-2, -2, -2);
    }

    BigNumber upperBound(int64_t y) {
        int y_count = getDigitCount(y);

        auto x_pref = getPrefix(y_count);

        if(y < x_pref) {
            return BigNumber(y, getSize() + 1 - y_count, 0);
        } else if(y > x_pref) {
            return BigNumber(y, max(0, getSize() - y_count), 0);
        } else {
            auto res = this->increment();
            if(res.head < 0 or res.getPrefix(y_count) != getPrefix(y_count)) {
                return BigNumber(y, getSize() + 1 - y_count, 0);
            } else {
                return res;
            }
        }
        return BigNumber(-2, -2, -2);
    }
};

int main() {
    ios_base::sync_with_stdio(false);

    int n; cin >> n;
    
    vector<int> a(n, 0);
    
    for(int i = 0; i < n; i += 1) {
        cin >> a[i];
    }   

    BigNumber prev(a[0], 0, 0);
    
    int64_t ans = 0;

    for(int i = 1; i < n; i += 1) {
        //cerr << prev.print() << "\n";
        prev = prev.upperBound(a[i]);
        assert(prev.head >= 0);
        ans += prev.getSize() - getDigitCount(a[i]);
    }
    
    //cerr << prev.print() << "\n";
    cout << ans << "\n";
}