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
#include <iostream>
#include <algorithm>
#include <string>
#include <cassert>

using namespace std;

typedef long long LL;
#define FOR(ii, ll, uu)  for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii)
#define REP(ii, nn) FOR(ii, 0, nn)
#define FORL(ii, ll, uu)  for(LL ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii)
#define REPL(ii, nn) FOR(ii, 0, nn)

typedef LL cnt;

void increment(string& num)
{
    size_t n = num.size();
    bool carry = true;
    REP(i, n)
    {
        if (num[n - 1 - i] != '9')
        {
            carry = false;
            ++num[n - 1 - i];
            break;
        }
        num[n - 1 - i] = '0';
    }
    if (carry)
    {
        num.insert(0, "1");
    }
}

int advance(string& prev, string &next)
{
    if (next.size() > prev.size())
    {
        // not modyfying next
        return 0;
    }

    if (next.size() == prev.size())
    {
        if (next <= prev)
        {
            next.push_back('0');
            return 1;
        }
        return 0;
    }

    assert(next.size() < prev.size());
    auto n_prev = prev.size();
    auto n_next = next.size();

    auto prev_prefix = prev.substr(0, n_prev);

    // just add water (missing digits)
    if (prev_prefix < next)
    {
        next.insert(n_next, n_prev - n_next, '0');
        return n_prev - n_next;
    }

    // need to add more anyway (missing digits + 1)
    if (prev_prefix > next)
    {   
        next.insert(n_next, n_prev - n_next + 1, '0');
        return n_prev - n_next + 1;
    }

    assert(prev_prefix == next);

    // check if all '9' after prefix
    auto num_9 = count(prev.begin() + n_next, prev.end(), '9');

    // cannot increment suffix (missing digits + 1)
    if (num_9 == n_prev - n_next)
    {
        next.insert(n_next, n_prev - n_next + 1, '0');
        return n_prev - n_next + 1;
    }

    next = prev;
    increment(next);

    return n_prev - n_next;
}

int main(int argc, char const *argv[])
{
    cnt added_zeros = 0;
    string prev_amount, amount;
    int N;
    cin >> N;
    cin >> prev_amount;

    REP(i, N-1)
    {
        cin >> amount;
        // cout << i << " Before: " << amount << endl;
        added_zeros += advance(prev_amount, amount);
        // cout << i << " After: " << amount << endl;
        prev_amount = amount;
    }

/*
    REP(i, 1000)
    {
        increment(amount);
        cout << amount << endl;
    }
*/

    cout << added_zeros << endl;
    return 0;
}