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

typedef long long LL;

const int N = 2e5 + 7;
string S[N];
int n;

int GetRelation (string &s1, string &s2)
{
    int m = min(s1.size(), s2.size());

    if (s1.size() > s2.size())
        return 0;

    for (int i = 0; i < m; ++i)
        if (s1[i] != s2[i])
        {
            if (s1[i] > s2[i])
                return 0;
            return 1;
        }

    if (s1.size() == s2.size())
        return 1;
    
    return -1;
}

int GetRelation2 (string &s1, string &s2)
{
    int m = min(s1.size(), s2.size());

    for (int i = 0; i < m; ++i)
        if (s1[i] != s2[i])
        {
            if (s1[i] < s2[i])
                return 1;
            return 0;
        }
    
    return -1;
}

bool Can (string &s, int it)
{
    while (it < s.size())
        if (s[it++] != '9')
            return true;

    return false;
}

void Add (string &s1, string &s2)
{
    vector < char > A;
    int it = s2.size() - 1;

    while (s2[it] == '9')
    {
        A.push_back('0');
        --it;
    }
    A.push_back((char)(s2[it--] + 1));
    while (it >= s1.size())
        A.push_back(s2[it--]);
    
    for (int i = A.size() - 1; i >= 0; --i)
        s1.push_back(A[i]);
}

void Read ()
{
    char Tab[10];
    scanf("%d", &n);
    
    for (int i = 0; i < n; ++i)
    {
        scanf("%s", Tab);
        S[i] = Tab;
    }
}

void Solve ()
{
    LL result = 0LL;
    int last = S[0].size();

    for (int i = 1; i < n; ++i)
    {
        int n1 = S[i].size(), n2 = S[i - 1].size();
        int relation = GetRelation(S[i], S[i - 1]);

        if (last > 15)
        {
            relation = GetRelation2(S[i], S[i - 1]);

            if (relation == -1)
            {
                S[i] = S[i - 1];
                relation = 0;
            }
            while (S[i].size() < 16)
                S[i].push_back('0');

            last += relation;
            result += (LL)(last - n1);
            continue;
        }

        if (relation != -1)
        {
            for (int j = 0; j < n2 - n1 + relation; ++j)
                S[i].push_back('0');
            result += (LL)max(0, n2 - n1 + relation);
        }
        else
        {   
            result += (LL)(n2 - n1);
            if (Can(S[i - 1], S[i].size()))
                Add(S[i], S[i - 1]);
            else
            {
                for (int j = 0; j < n2 - n1 + 1; ++j)
                    S[i].push_back('0');
                result += 1LL;
            }
        }

        last = S[i].size();
    }

    printf("%lld\n", result);
}

int main ()
{
    Read();
    Solve();

    return 0;
}