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

#define debug if(0)
#define watch(x) debug cout << __LINE__ << ": " << (#x) << " is " << (x) << endl ;
#define watch_tab(x,n) debug { cout << __LINE__ << ": " << (#x) << " is "; \
    for(int very_long_index=0;very_long_index<n;very_long_index++){ cout << x[very_long_index] << ", ";}\
    cout << endl; }

typedef long long ll;
typedef unsigned long long ull;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll n,sum=0,licz=0,dzies=0,licz_prev=0,dzies_prev=0,count=0,x=0,doom=0,y=0;
    cin >> n;
    while(n--){
        cin >> licz;
        dzies=dzies_prev;
        int temp=licz;
        while(((temp/10>=licz_prev)||((temp/10==licz_prev)&&(temp%10!=0)))&&(dzies>0)){
            --dzies;
            temp/=10;
        }
        temp=licz_prev;
        x=1;
        if(dzies<9){
            for (int j = 0; j < dzies; ++j) {
                x*=10;
            }
        } else {
            x=1e9;
        }
        y=1;
        if(dzies_prev<9){
            for (int j = 0; j < dzies; ++j) {
                y*=10;
            }
        } else {
            y=1e9;
        }
        while(temp>licz){
            ++dzies;
            temp/=10;
            x=min(x*10,ll(1e10));
        }
        watch(licz_prev);
        watch(x);
        watch(y);
        watch(temp);
        watch(licz);
        if(temp==licz){
            if(temp!=doom){
                count=(dzies>9)?(1e9):((10*y-(licz_prev*y-temp*x))-1);
                if(x==y&&x==1){
                    ++dzies;
                    ++count;
                }
                doom=temp;
            } else {
                --count;
            }
            if(count==0){
                ++dzies;
                count=(dzies>9)?(1e9):(x*(y-(licz_prev*y-temp*x)));
            }
        }
        watch(count);
        debug{
        cout << licz;
        for (int i = 0; i < dzies; ++i) {
            cout << '0';
        }
        cout << '\n';
        }


        sum+=dzies;

        licz_prev=licz;
        dzies_prev=dzies;
    }
    cout << sum << '\n';
}