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
//Tadrion
#include <cstdio>
#include <vector>
#include <iostream>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define CLEAR(x) (memset(x,0,sizeof(x)))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define VAR(v, n) __typeof(n) v = (n)
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define DBG(v) cout<<#v<<" = "<<v<<endl;
#define IN(x,y) ((y).find(x)!=(y).end())
#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

LL curlen = 0;
LL total = 0;
LL n;
string s;
char t[210000];

int main() {
    cin >> n;

    cin >> s;
    REP(i,SZ(s)) t[i]=s[i];

    curlen = SZ(s);

    REP(ttt, n-1)
    {
        cin >> s;
        LL L = SZ(s);
        LL toAdd=0;
        bool bMniejsze = false;
        bool bWieksze = false;

        if (curlen >= L)
        {
            int i = 0;
            for(i=0; i < SZ(s); i++)
            {
                if(s[i] < t[i])
                {
                    bMniejsze = true;
                    break;
                }
                else if (s[i] > t[i])
                {
                    bWieksze = true;
                    break;
                }
            }

            if (bMniejsze)
            {
                //printf("MNIEJSZE\n");
                curlen++;
                memcpy(t, s.c_str(), L*sizeof(char));
                memset(t+L, '0', (curlen-L)*sizeof(char));
                toAdd = curlen-L;
                total += toAdd;
            }

            if (bWieksze)
            {
                //printf("WIEKSZE\n");
                memcpy(t, s.c_str(), L*sizeof(char));
                memset(t+L, '0', (curlen-L)*sizeof(char));
                toAdd = curlen-L;
                total += toAdd;
            }

            // ROWNE -> HIV
            //
            if (!bWieksze && !bMniejsze)
            {
                int j = curlen-1;
                while (j >= i)
                {
                    if(t[j] == '9') t[j] = '0';
                    else {t[j]++; break;}
                    j--;
                }

                if (j<i)
                {
                    t[curlen]='0';
                    curlen++;
                }

                toAdd = curlen-L;
                total += toAdd;
            }
        }
        else
        {
            curlen = L;
            memcpy(t, s.c_str(), L*sizeof(char));
        }

        //printf("dd: %s\n",t);
        //printf("toadd: %d\n",toAdd);
        if (ttt >= n-5)
        {
            //cout << "s:" <<s << "\n";
            //printf("t:%s\n",t);
        }
    }

    cout << total << "\n";

  return 0;
}