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
#include <iostream>
#include <vector>
/*
Autor: Helena Borak-Wejster
*/
using namespace std;

vector<long long int> make_digits(long long int a, long long int min_size){
    vector<long long int> dig;
    vector<long long int> dig_rev;
    while(a>0){
        dig.push_back(a%10);
        a/=10;
    }
    if(dig.size()<min_size){
        long long int diff = min_size-dig.size();
        while(diff>0){
            diff--;
            dig.push_back(0);
        }
    }
    for(auto i=dig.rbegin();i!=dig.rend();i++){
        dig_rev.push_back(*i);
    }
    return dig_rev;
}
long long int value(vector<long long int> dig){
    long long int mnoznik = 1;
    long long int value=0;
    for(auto i=dig.rbegin();i!=dig.rend();i++){
        value+=mnoznik*(*i);
        mnoznik*=10;
    }
    return value;
}

long long int comparator(vector<long long int> now, vector<long long int> last){
    if(last.size()<now.size()) return 1;
    if(last.size()>now.size()) return -1;
    long long int i=0;
    while(i<now.size()){
        if(last[i]<now[i]) return 1;
        else{
            if(last[i]>now[i]) return -1;
        }
        i++;
    }
    return 0;
}

void full_with_zeros(vector<long long int>* v, long long int i){
    //cout << "sthhhhhhhh"<<endl;
    while(i>0){
        (*v).push_back(0);
        i--;
        //cout << "dooo ";
    }
    //cout << endl;
}

int main()
{
    long long int n;
    cin >> n;
    vector<long long int> d(n);
    for(long long int i=0;i<n;i++){
        cin >> d[i];
    }
    vector<long long int> last=make_digits(d[0],0);
    long long int c=0;
    long long int other_c=0;
    for(long long int p=1;p<n;p++){
        vector<long long int> now = make_digits(d[p],0);
        long long int oldSize=now.size();
        //cout << now.size() << " ";
        //for(long long int l=0;l<last.size();l++) cout << last[l] <<" ";
        //cout << endl;

        /*if(value(now)>value(last)) {
            cout << value(now) << " " << value(last) << endl;
            last=now;
            continue;
        }*/
        if(comparator(now, last)>0){
            last=now;
            //cout << now.size() << endl;
            other_c+=now.size()-oldSize;
            continue;
        }
        /*if(last.size()<now.size()){
            last=now;
            continue;
        }*/
        if(last.size()==now.size()){
            c++;
            now.push_back(0);
            last=now;
            //cout << now.size() << endl;
            other_c+=now.size()-oldSize;
            continue;
        }
        long long int i=0;
        long long int j=0;
        while(j<(now.size()-1) && last[i]==now[j]){
            i++;
            j++;
        }

        if(j==now.size()-1){
                //cout << "happy"<<endl;
            if(last[i]<now[j]){
                full_with_zeros(&now, last.size()-now.size());
                c+=last.size()-now.size();
            }
            if(last[i]>now[j]){
                full_with_zeros(&now, last.size()-now.size()+1);
                c+=last.size()-now.size()+1;
            }
            if(last[i]==now[j]){
                vector<long long int> tmp(last.size()-i-1);
                std::copy(last.begin()+i+1,last.end(),tmp.begin());
                long long int up = value(tmp)+1;
                vector<long long int> makeup = make_digits(up, tmp.size());
                //for(long long int l=0;l<tmp.size();l++) cout << tmp[l] <<" ";
                //cout << "tmp"<<endl;
                //for(long long int l=0;l<makeup.size();l++) cout << makeup[l] <<" ";
                //cout << "makeup"<<endl;
                if(makeup.size()>tmp.size()){
                    /*for(auto it=makeup.rbegin();it!=makeup.rend();it++){
                        now.push_back(*it);
                        c++;
                    }*/
                    //cout << "filller"<<endl;
                    full_with_zeros(&now, makeup.size());
                    c+=makeup.size();
                }
                else{
                    for(auto it=makeup.begin();it!=makeup.end();it++){
                        now.push_back(*it);
                        c++;
                    }
                }
            }
        }
        else{
            //cout << "else"<<endl;
            if(last[i]<now[j]){
                //cout << "wtf"<<endl;
                long long int val = last.size()-now.size();
                full_with_zeros(&now, val);
                c+=val;
            }
            if(last[i]>now[j]){
                long long int val = last.size()-now.size();
                full_with_zeros(&now, val+1);
                c+=val+1;
            }
        }
        //cout << now.size() << endl;
        other_c+=now.size()-oldSize;
        last=now;
    }
    //for(long long int l=0;l<last.size();l++) cout << last[l] <<" ";
    //cout <<endl;
    cout << other_c << endl;
    //cout << c << endl;
    return 0;
}