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

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); }

#ifdef LOCAL
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
#define __gcd gcd
#else
#define DBG(...) (__VA_ARGS__)
#define cerr if(0) cout
#endif

// ********************** CODE ********************** //

const int N = 2e5 + 7;

int n;
LL A[N];

const LL C = 1e17;

int main()
{
    _upgrade

    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> A[i];

    string cur = to_string(A[1]);
    LL fst = A[1], ans = 0, d = sz(cur);

    for(int i = 2; i <= n; i++)
    {
        string s = to_string(A[i]);
        if(fst >= C)
        {
            for(int j = 0; j < sz(s); j++)
            {
                if(s[j] < cur[j])
                {
                    d++;
                    cur = s;
                    cur.resize(10, '0');
                    break;
                }
                else if(s[j] > cur[j])
                {
                    cur = s;
                    cur.resize(10, '0');
                    break;
                }
            }
            ans += d - sz(s);
        }
        else
        {
            bool flag = false;
            if(sz(cur) < sz(s))
            {
                cur = s;
                fst = A[i];
                d = sz(s);
                flag = true;
            }
            if(!flag)
            {
                for(int j = 0; j < sz(s); j++)
                {
                    if(s[j] < cur[j])
                    {
                        d++;
                        fst = A[i];
                        for(int k = sz(s); k < d; k++)
                            fst *= 10;
                        cur = s;
                        cur.resize(d, '0');
                        flag = true;
                        ans += d - sz(s);
                        break;
                    }
                    else if(s[j] > cur[j])
                    {
                        fst = A[i];
                        for(int k = sz(s); k < d; k++)
                            fst *= 10;
                        cur = s;
                        cur.resize(d, '0');
                        flag = true;
                        ans += d - sz(s);
                        break;
                    }
                }
            }
            if(!flag)
            {
                // s jest prefiksem cur
                fst++;
                string cur2 = to_string(fst);
                bool ok = true;
                for(int j = 0; j < sz(s); j++)
                {
                    if(sz(cur2) == j || cur2[j] != s[j])
                    {
                        ok = false;
                        break;
                    }
                }
                if(!ok)
                {
                    d++;
                    cur = s;
                    fst = A[i];
                    for(int k = sz(s); k < d; k++)
                        fst *= 10;
                    cur.resize(d, '0');
                    ans += d - sz(s);
                }
                else
                {
                    cur = cur2;
                    d = sz(cur);
                    ans += d - sz(s);
                }
            }
        }
        //DBG(i, A[i], fst, cur, ans, d);
    }
    cout << ans << "\n";
	return 0;
}