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
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>

using namespace std;
int pow(int a)
{
    if(a>6)return 3000003;
    int wyn=1;
    for(int i=0;i<a;++i)wyn*=10;
    return wyn;
}
int strtoint(string a)
{
    int wyn=a[0]-'0';
    for(int i=1;i<a.size();++i)
    {
        wyn*=10;
        wyn+=a[i]-'0';
    }
    return wyn;
}
string str(int a)
{
    string wyn;
    while(a>0)
    {
        wyn=(char)(a%10+'0')+wyn;
        a/=10;
    }
    return wyn;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    long long n,wynik=0,counter=0,ile_cyfr;
    string last,element;
    cin>>n>>last;
    ile_cyfr=last.size();
    for(int i=1;i<n;++i)
    {
        //cout<<element<<" "<<ile_cyfr<<" "<<last<<"\n";
        cin>>element;
        if(element.size()==last.size())
        {
            if(element==last)
            {
                counter++;
                if(counter>=pow(ile_cyfr-element.size()))
                {
                    ile_cyfr++;
                    counter=0;
                }
            }
            else
            {
                counter=0;
                if(element<last)ile_cyfr++;
                last = element;
            }
            wynik+=(ile_cyfr-element.size());
        }
        else if(element.size()<last.size())
        {
            string help=last.substr(0,element.size()),help2=last.substr(element.size(),last.size()-element.size());
            if(help==element)
            {
                if(counter+1>=pow(ile_cyfr-last.size()))
                {
                    int k=help2.size()-1;
                    while(k>0&&help2[k]=='9')k--;
                    counter=0;
                    if(k==0&&help2[k]=='9')
                    {
                        ile_cyfr++;
                        last=element;
                    }
                    else
                    {
                        char z=help2[k];
                        z++;
                        string dopisek="a";
                        dopisek[0]=z;
                        k--;
                        while(k>=0)
                        {
                            dopisek=help2[k]+dopisek;
                            k--;
                        }
                        last=element+dopisek;
                    }
                }
                else counter++;
                wynik+=(ile_cyfr-element.size());
            }
            else
            {
                counter=0;
                if(help>element)ile_cyfr++;
                wynik+=(ile_cyfr-element.size());
                last=element;
            }
        }
        else
        {
            if(element.size()>ile_cyfr)
            {
                last=element;
                ile_cyfr=element.size();
                counter=0;
            }
            else
            {
                string help=element.substr(0,last.size()),help2=element.substr(last.size(),element.size()-last.size());
                if(help==last)
                {
                    string b=str(counter);
                    int test=1;
                    for(int i=0;i<ile_cyfr-b.size()-last.size()&&test==1;++i)
                    {
                        if(i==help2.size())
                        {
                            counter++;
                            test=2;
                        }
                        if(help2[i]!='0'&&test==1)
                        {
                            test=0;
                            last=element;
                            counter=0;
                        }
                    }
                    if(test==1)
                    {
                        string help3=help2.substr(ile_cyfr-b.size()-last.size(),help2.size()-(ile_cyfr-b.size()-last.size()));
                        string help4=b.substr(0,help2.size()-(ile_cyfr-b.size()-last.size()));
                        if(help4>=help3)ile_cyfr++;
                        last=element;
                    }
                    wynik+=(ile_cyfr-element.size());
                }
                else
                {
                    counter=0;
                    if(help<last)ile_cyfr++;
                    wynik+=(ile_cyfr-element.size());
                    last=element;
                }
            }
        }
    }
    //cout<<element<<" "<<ile_cyfr<<" "<<last<<"\n";
    cout<<wynik;
    return 0;
}