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
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int len(ll a) {
    //cerr << "len" << endl;
    int l = 0;
    while (a) {
        ++l;
        a/=10;
    }
    return l;
}

int get(ll value, ll pos) {
    //cerr << "get" << value << "," << pos << endl;
    if (pos == 0)
        return value % 10;
    if (value == 0)
        return 0;
    return ::get(value/10, pos-1);
}

struct Sapkint { // a * 10^e + b
    ll a;
    ll e;
    ll b;
    
    int len() const {
        return ::len(a) + e;
    }
    
    Sapkint next() const {
        if (::len(b) == e && ::len(b+1) == e+1) {
            return Sapkint{a+1, e, 0};
        } else {
            return Sapkint{a, e, b+1};
        }
    }
    
    bool operator>(const Sapkint& other) const {
        if (this->len() > other.len()) return true;
        if (this->len() < other.len()) return false;
        
        for (int i = this->len() - 1; i >= 0; --i) {
            if (this->get(i) > other.get(i))
                return true;
            else if (this->get(i) < other.get(i))
                return false;
        }
        return false; // eq
    }
    
    int get(ll pos) const {
        if (pos >= e)
            return ::get(a, pos-e);
        return ::get(b, e);
    }
    
    bool startswith(ll v) const {
        ll of = this->len() - ::len(v);
        if (of < 0) return false;
        for (int i = ::len(v) - 1; i>=0; --i) {
            //cerr << "swfor" << endl;
            if (this->get(i+of) != ::get(v, i)) {
                return false;
            }
        }
        return true;
    }
};

int main() {
    int n;
    cin >> n;
    
    long long ans = 0;
    
    Sapkint cur { 0, 0, 0};
    while (n-->0) {
        int x;
        cin >> x;        
        
        if (cur.startswith(x)) {
            cerr << "Good" << endl;
            // Good.
        } else {
            Sapkint prop1 { x, max<int>(0, cur.len() - len(x)), 0 };
            Sapkint prop2 { x, max<int>(0, cur.len() - len(x)) + 1, 0 };
            
            if (prop1 > cur) {
                cur = prop1;
            } else {
                cur = prop2;
            }
        }
        
        ans += max<int>(0, cur.len() - len(x));
                
        cerr << "add = " << max<int>(0, cur.len() - len(x)) << endl; 
        cerr << "Curr = {" << cur.a << "e" << cur.e << " + " << cur.b << "}" << endl;  
        
        cur = cur.next();
    }
    
    cout << ans;
    
    return 0;
}