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
#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie();
    int n;
    cin >> n;
    string current_max, num;
    cin >> current_max;
    int length = 0;
    long long changes = 0;

    for (int i = 1; i < n; i++)
    {
        cin >> num;
        //cout << length<<endl;
        if (length < 20)
        {
        if (num.size() > current_max.size())
        {
            current_max = num;
            continue;
        }
        int ok = 1;
        int change_pos = -1;
        for (int j = 0; j < num.size(); j++)
        {
            if (num[j] > current_max[j])
            {
                ok = 2;
                break;
            }
            if (num[j] < current_max[j])
            {
                ok = 0;
                break;
            }
        }
        for (int j = num.size(); j < current_max.size(); j++)
            if (current_max[j] < '9')
            {
                change_pos = j;
            }
        if (ok == 0 || (ok == 1 && change_pos == -1))
        {
            string new_max = "";
            for (int j = 0; j < num.size(); j++)
                new_max += num[j];
            for (int j = num.size(); j < current_max.size() + 1; j++)
            {
                new_max += '0';
                changes++;
            }
            current_max = new_max;
        }
        else
        {
            for (int j = 0; j < num.size(); j++)
                current_max[j] = num[j];
            changes += (current_max.size() - num.size());
            if (change_pos != -1 && ok != 2)
            {
                current_max[change_pos]++;
                for (int j = change_pos + 1; j < current_max.size(); j++)
                    current_max[j] = '0';
            }
            if (ok == 2)
            {
                for (int j = num.size(); j < current_max.size(); j++)
                    current_max[j] = '0';
            }
        }
        length = current_max.size();
        }
        else
        {
        int ok = 1;
        for (int j = 0; j < min(num.size(), current_max.size()); j++)
        {
            if (num[j] > current_max[j])
            {
                ok = 1;
                break;
            }
            if (num[j] < current_max[j])
            {
                ok = 0;
                break;
            }
        }
        //cout << num << " * "<< ok <<endl;
        if (!ok)
        {
            length ++;
            current_max = num;
        }
        else
            current_max = max(current_max, num);
        changes += (length - num.size());
        }
        //cout << current_max <<" * "<< length<< " "<< num.size()<< " || "<< changes<< endl;
    }
    cout << changes;
}
/*30
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4*/