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
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoll

using namespace std;


#define MAXL 13
#define LL long long 

LL to_int(const string& str) {
    return stoll(str);
}

LL decpow[] = {
1ll, 
10ll, 
100ll, 
1000ll, 
10000ll,  
100000ll,  
1000000ll,  
10000000ll,   
100000000ll, 
1000000000ll,
10000000000ll,
100000000000ll,    
1000000000000ll,
10000000000000ll,        
100000000000000ll,
1000000000000000ll,
10000000000000000ll
};


////////////////////////////////////////////////////////////////////////////////////////////////


int len(const string& str) {
    return str.size();
}

bool isPrefix( std::string const& lhs, std::string const& rhs) { //shorter string first
    return std::equal(lhs.begin(), lhs.begin() + std::min( lhs.size(), rhs.size() ), rhs.begin() );
}





int main() {
    int N;
    cin>>N;


    LL c = 0;
    LL prev = -1;
    string prevtxt = "0";
    LL width = 1;
    string atxt;
    LL a;
    for (int i=0; i<N; ++i) {

        cin>>a;
        atxt = to_string(a);


        //cout<<"================================================"<<i<<endl;

        while (len(prevtxt)<width && len(prevtxt)<=MAXL) { prevtxt.append("0"); }
        prev = to_int(prevtxt);

        //cout<<"["<<i<<"]."<<prevtxt<<":"<<prev<<" / "<<atxt<<":"<<a<<endl;


        if (a>prev) {      
            width = len(atxt);
        } else if (a==prev) {   
            width += 1;
            c += width-len(atxt);
        } else { //a jest krótsze niż prev

            LL d = len(prevtxt) - len(atxt);
            if (isPrefix(atxt, prevtxt)) {

                LL increase = to_int( prevtxt.substr(len(atxt)) ) + 1;
                if (len(to_string(increase)) <= d) {   
                    c += width-len(atxt);
                    a = a*decpow[d] + increase;
                    atxt = to_string(a);    
                } else {
                    width += 1;
                    c += width-len(atxt);
                }
            } else if (d==0) {
                width += 1;
                c += width-len(atxt);

            } else { //prev>a ale a nie jest prefixem       
                if (to_int( prevtxt.substr(0,len(atxt)) ) < a) {
                    c += width-len(atxt);
                } else {
                    width += 1;
                    c += width-len(atxt);
                }
            }
        }

        prevtxt = atxt;
        prev = a;
    }
        
    cout<< c <<endl;
    return 0;
}