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
//#include "/Users/dimazhylko/CPPProjects/bits/stdc++.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

string Z[501];

bool isBigerOrEk(string &a, string &b){
	int n=a.length(),m=b.length();
	if(n>m)return true;
	if(n==m){
		if(a==b)return true;
		for(int i = 0;i<n;i++){
			if((a[i]-'0') == (b[i]-'0'))continue;
			if((a[i]-'0') > (b[i]-'0'))return true;
			if((a[i]-'0') < (b[i]-'0'))return false;
		}
	}
	return false;
}

void mod(string &a,string &b){
	int n=a.length(),m=b.length();
	int d = n-m;
	if(d==0){
		b+="0";
	} else {
		int i = n-1;
		while(a[i]=='9' && i>=0){
			a=a.substr(0,i)+"0"+a.substr(i+1);
			i--;
		}
		if(i==-1){
			a="1"+a;
		} else {
			//a[i]=to_string((a[i]-'0') + 1);
			a=a.substr(0,i)+to_string((a[i]-'0') + 1)+a.substr(i+1);
		}

		int dd = d;
		while(dd>500){
				b+=Z[500];
				dd-=500;
		}
		b+=Z[dd];
		if(!isBigerOrEk(a,b)){
			return;
		}

		for(int j = 0;j<m;j++){
			if(b[j]!=a[j]){
				b+=Z[1];
				return;
			}
		}
		b=a;
	}
}

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(NULL); 
    cout.tie(NULL);
	
	int n;
	cin>>n;
	string a;
	string b;
	cin>>a;
	ll res=0;int s;

	string z="";
	Z[0]="";
	for(int i = 1;i<=500;i++){
		z+="0";
		Z[i]=z;
	}

	for(int i = 1;i<n;i++){
		cin>>b;
		if(isBigerOrEk(a,b)){
			s=b.length();
			mod(a,b);
			res+=(b.length()-s);
		}
		a = b;
	}

	cout<<res<<endl;
	return 0;
}