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

using namespace std;

typedef long long int LL;

int n;
LL res;
string s;
int Length;

string change(string a, string b){
	if((int)b.size() > Length){
		Length = b.size();
		return b;
	}
	
	if((int)b.size() == Length){
		if(a < b)
			return b;
		
		b.push_back('0');
		Length = b.size();
		return b;
	}
	
	bool eq = true, better = false;
	for(int i = 0; i < (int)b.size(); ++i){
		better |= eq && a[i] < b[i];
		eq &= a[i] == b[i];
	}
	
	if(better){
		for(int i = 0; i < b.size(); ++i)
			a[i] = b[i];

		for(int i = b.size(); i < (int)a.size(); ++i)
			a[i] = '0';
		return a;
	}
	
	if(eq){
		bool can = Length > 16;
		if(!can)
			for(int i = b.size(); i < (int)a.size(); ++i)
				can |= a[i] < '9';
		
		if(!can){
			for(int i = b.size(); i < (int)a.size(); ++i)
				a[i] = '0';
			
			++Length;
			a.push_back('0');
			return a;
		}
		
		a[a.size() - 1] += 1;
		for(int i = a.size() - 1; i >= 0; --i){
			if(a[i] <= '9')
				break;
			
			a[i] = '0';
			a[i - 1]++;
		}
		
		return a;
	}
	
	++Length;
	for(int i = 0; i < (int)b.size(); ++i)
		a[i] = b[i];

	for(int i = b.size(); i < (int)a.size(); ++i)
		a[i] = '0';
	
	if(a.size() < 16)
		a.push_back('0');
	return a;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin >> n;
	for(int i = 1; i <= n; ++i){
		string a;
		cin >> a;
		s = change(s, a);
		res += Length - a.size();
//		cout << s << " ";
	}
	
	cout << res << "\n";
	return 0;
}