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
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); (i)<(b); (i)++)
#define PPC(x) __builtin_popcountll((x))
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
using namespace std;

const int maxN = 200042;

char T[maxN];
int s;

void replace(char* t, int s2)
{
	s = max(s, s2);
	FOR(i, 0, s2)
		T[i] = t[i];
	FOR(i, s2, min(s, s2+14))
		T[i] = '0';
	for (int i=s-1; i>=max(s2, s-14); i--)
		T[i] = '0';
}

bool cond_incr(int s2)
{
	for (int i=s-1; i>=s2; i--)
	{
		if (T[i] == '9')
			T[i] = '0';
		else
		{
			T[i]++;
			return true;
		}
	}
	return false;
}

int main()
{
	int n;
	scanf ("%d", &n);
	long long res = 0;
	
	while (n--)
	{
		char in[42];
		scanf ("%s", in);
		int s2 = strlen(in);
		
		if (s < s2)
		{
			replace(in, s2);
			continue;
		}
		
		int i=0;
		while (i < s2 and T[i] == in[i])	i++;
		
		if (i == s2)
		{
			if (!cond_incr(s2))
				T[s++] = '0';
		}
		
		else
		{
			if (in[i] < T[i])	s++;
			replace(in, s2);
		}		
		res += s-s2;
	}
	
	printf("%lld\n", res);
	return 0;
}