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
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
template<class T, class U>
ostream& operator<<(ostream& os, pair<T, U> p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template<class C, class = typename C::value_type>
typename enable_if<!is_same<C, string>::value, ostream&>::type
operator<<(ostream& os, C c) {
    auto i = c.begin();
    while (i != c.end()) {
        os << " {"[i == c.begin()] << *i;
        os << ",}"[++i == c.end()];
    }
    return os;
}
#define debug(x) { cerr << #x << " = " << x << endl; }
#else
#define debug(...) {}
#endif

using ll = long long;

struct Num {
    int num, pad, len1, len2;
    
    Num(int a) : num(a), pad(0), len1(bin_len(a)), len2(0) {}

    Num bigger(int a) {
        Num res0(a);
        Num res(a);
        int l = len(), a_l = res.len();
        
        if (a_l > l) {
            return res;
        }
        // a_l <= l
        
        for (int i = 0; i < a_l; i++) {
            int c = get(i), a_c = res.get(i);
            
            if (a_c < c) {
                res.len2 = l - a_l + 1;  // pad with zeros
                return res;
            }
            else if (a_c > c) {
                res.len2 = l - a_l;  // pad with zeros
                return res;
            }
        }
        
        res.num = num;
        res.len1 = len1;
        res.pad = pad + 1;
        res.len2 = len2;

        if (bin_len(res.pad) > len2) {
            res.pad = 0;
            res.num++;
            if (bin_len(res.num) > len1) {
                res.num = res0.num;
                res.len1 = res0.len1;
                res.pad = 0;
                res.len2 = l - a_l + 1;
                return res;
            }
        }

        for (int i = 0; i < a_l; i++) {
           if (res.get(i) != res0.get(i)) {
               res.num = res0.num;
               res.len1 = res0.len1;
               res.pad = 0;
               res.len2 = l - a_l + 1;
               return res;
           }
        }
        
        return res;
    }
    
    int diff(int a) {
        return len() - bin_len(a);
    }

private:
    int len() {
        return len1 + len2;
    }

    int get(int pos) {
        if (pos < len1) {
            return bin_get(num, pos);
        }
        else if (pos < len() - bin_len(pad)) {
            return 0;
        }
        else {
            return bin_get(pad, pos - (len() - bin_len(pad)));
        }
    }

    int bin_len(int x) {
        int l = 0;
        while (x) {
            l++;
            x /= 10;
        }
        return l;
    }

    int bin_get(int x, int pos) {
        vector<int> dig = digits(x);
        return dig[dig.size() - 1 - pos];
    }

    vector<int> digits(int x) {
        vector<int> res;
        while (x) {
            res.push_back(x % 10);
            x /= 10;
        }
        return res;
    }
};

#ifdef LOCAL
    ostream& operator<<(ostream &os, Num x) {
        return os << "(" << x.num << ", " << x.pad << ")" << ", lengths (" << x.len1 << ", " << x.len2 << ")";
    }
#endif

ll solve(int n, vector<int> a) {
    ll res = 0;
    
    Num cur(a[0]);
    for (int i = 1; i < n; i++) {
        cur = cur.bigger(a[i]);
        debug(cur);
        res += cur.diff(a[i]);
    }

    return res;
}

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

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

    cout << solve(n, a) << "\n";
}