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
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a) FOR(i, 0, a - 1)
#define ST first
#define ND second
#define V vector
#define RS resize
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
#define S(a) (int)a.size()

template<class T> void db(T a) { cerr << a; }
template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; }
template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; }
template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; }
template<class T, class... TS> void dump(const char *s, T a, TS... x)
{ while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); }

#ifdef DEBUG
#define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); 
#else
#define DB(...)
#endif

using LL = long long;
using PII = pair<int, int>;
using VI = V<int>;
using VLL = V<LL>;
using VVI = V<VI>;
using VPII = V<PII>;

bool is_prefix(string a, string b) // if a is a prefix of b
{
    if(S(a) > S(b))
        return 0;
    REP(i, S(a))
        if(a[i] != b[i])
            return 0;
    return 1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;
    string last = "0";
    LL ans = 0;
    REP(i, n)
    {
        string next;
        cin >> next;

        auto clear = [&]()
        {
            int q = S(next);
            REP(i, q)
            {
                if(S(last) <= i)
                    last += next[i];
                else
                    last[i] = next[i];
            }
            FOR(i, q, min(S(last) - 1, q + 10))
                last[i] = '0';
            int r = S(last) - 1;
            FOR(i, max(q, r - 10), r)
                last[i] = '0';
        };

        if(next > last)
        {
            ans += max(0, S(last) - S(next));
            clear();            
        }
        else if(is_prefix(next, last))
        {
            ans += S(last) - S(next);
            int q = S(next);
            int pos = S(last) - 1;
            while(pos >= q && last[pos] == '9')
               pos--;

            if(pos < q)
            {
                last += "0";
                ans++;
                clear();
            }
            else
            {
                last[pos]++;
                FOR(j, pos + 1, S(last) - 1)
                    last[j] = '0';
            }
        }
        else if(next < last && S(next) <= S(last))
        {
            last += "0";
            ans += S(last) - S(next);
            clear();
        }
        else
            last = next;

        DB(i, last);
    }

    cout << ans << "\n";
}