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

//#define READ_FROM_FILE

using namespace std;

typedef long long ll;

pair<ll, int> nextValue(ll prev, ll next)
{
    if (prev < next)
        return {next, 0};
    
    if (prev == next)
        return {next*10, 1};
    
    string prevS = to_string(prev);
    string nextS = to_string(next);
    
    int res = prevS.compare(0, nextS.size(), nextS);
    int c = prevS.size() - nextS.size();
    // assert(c >= 0);
    
    if (res == 0)
    {
        ll ten = 10;
        int tc = c;
        while(--tc)
            ten *= 10;
        
        ll mod = prev % ten;
        
        mod += 1;
        string modS = to_string(mod);
        
        if ((int)modS.size() <= c)
        {
            next *= ten;
            next += mod;
            return {next, c};
        }
        else
        {
            ten *= 10;
            next *= ten;
            return {next, c+1};
        }
    }
    
    int tc = c;
    while(tc--)
        next *= 10;    
    if (res > 0)
    {
        c++;
        next *= 10;
    }
    return {next, c};
}

int main(int argc, char** argv)
{
	ios::sync_with_stdio( false );

#ifdef READ_FROM_FILE
    string inputStrName ( "temp.gen" );
    if ( argc >= 2 )
    {
        inputStrName = string ( argv[1] );
    }

    if ( freopen ( inputStrName.c_str( ), "r", stdin ) == NULL )
    {
        std::cout << "No data file" << std::endl;
        return 0;
    }
#endif // READ_FROM_FILE

//     pair<ll,int> rs = nextValue(900, 9);
//     printf("%lld\n", rs.first);
//     return 0;

    int q = 0;
    cin >> q;
    q--;

    ll result = 0;
    ll removed = 0;
    ll p = 0;
    cin >> p;
    for (int i=0; i<q; ++i)
    {
        ll n = 0;
        cin >> n;
        
        //printf("p=%lld\nn=%lld\n", p, n);
        
        pair<ll, int> r = nextValue(p, n);
        //printf("r=%lld\n==================\n", r.first);
        // assert(r.first > p);
        p = r.first;
        
        result += r.second + removed;
        
        if (p > 10e12)
        {
            while((p > 10e12) && (p % 10 == 0))
            {
                p /= 10;
                removed++;
            }
        }
    }
    
    printf("%lld\n", result);

    return 0;
}