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
162
163
164
165
166
167
168
169
170
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e6;

struct BigNum
{
    LL pref = 0;
    int zeros = 0, suflen = 0; 
    vector<int> suf;
};

bool canChange = false;

void add(BigNum &n, int i, int val)
{
    if(i == 7)
    {
        n.zeros++;
        for(auto &I : n.suf)
        {
            I = 0;
        }
        canChange = true;
    }
    else if(i >= n.suflen)
    {
        ++n.suflen;
        n.suf.push_back(0);
        canChange = true;
    }
    else if(n.suf[i]+val > 9)
    {
        n.suf[i] = 0;
        add(n, i+1, val);
    }
    else
        n.suf[i] += val;
}

int main()
{
    ios::sync_with_stdio(0);
    BigNum last;
    int n;
    LL x;
    LL res = 0;
    //scanf("%d", &n);
    //scanf("%lld", &last.pref);
    cin >> n >> last.pref;
    n--;
    while(n --> 0)
    {
        cin >> x;
        canChange = false;
        if(x > last.pref)
        {
            last.suf.clear();
            last.pref = x;
            for(int i = 0;i < last.suflen;++i)
                last.suf.push_back(0);
            res += last.suflen;
        }
        else if(x == last.pref)
        {
            if(last.suflen > 0)
            {
                add(last, 0, 1);
            }
            else
            {
                if(last.pref*10 < (LL)1e10)
                {
                    last.pref *= 10;
                    ++res;
                }
                else
                {
                    last.suflen = 1;
                    last.suf.push_back(0);
                }
            }
            res += last.suflen;
        }
        else //x < pref
        {
            LL t1 = x, t2 = x, ct2 = 1;
            int ct1 = 0;
            while(t1*10 <= last.pref)
            {
                ++ct1;
                t1*= 10;
                ct2 *= 10;
            }
            t2 = t1;
            LL x2 = last.pref;
            x2 %= ct2;
            t2 += x2;
            //cout << "x < pref " << last.pref << " " << t1 << " " << t2 << endl;
            if(t2 == last.pref)
            {
                if(last.suflen > 0)
                {
                    add(last, 0, 1);
                    if(canChange)
                    {
                        last.pref = t1;
                    }
                }
                else
                {
                    //cout << ct1 << endl;
                    if(last.pref+1 < (LL)1e10 && (int)((last.pref+1)/(ct2)) == x)
                        last.pref++;
                    else if(t1*10 < (LL)1e10)
                    {
                        last.pref = t1*10;
                        ++res;
                    }
                    else
                    {
                        last.suflen = 1;
                        last.suf.push_back(0);
                        last.pref = t1;
                    }
                }
            }
            else if(t2 > last.pref)
            {
                last.pref = t1;
                last.suf.clear();
                for(int i = 0;i < last.suflen;++i)
                    last.suf.push_back(0);
            }
            else //t2 < pref
            {
                last.pref = t1;
                if(last.pref*10 < (LL) 1e10)
                {
                    last.pref *= 10;
                    ++res;
                }
                else
                {
                    last.suflen++;
                    last.suf.clear();
                    if(last.suflen >= 7)
                    {
                        --last.suflen;
                        ++last.zeros;
                    }
                    for(int i = 0;i < last.suflen;++i)
                        last.suf.push_back(0);
                }
            }
            res += ct1 + last.suflen;
        }
        res += last.zeros;
        //cout << last.pref << " | " << last.zeros << "x0 | ";
        //for(auto I : last.suf)
         //   cout << I;
       // cout << " -> " << last.suflen << endl; 
    }
    cout << res << endl;
    //printf("%lld\n", res);
    return 0;
}