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
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
typedef long long LL;

LL p[17] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000};

LL len(LL x){
	LL l=0;
	while(x/=(LL)10) ++l;
	return l+(LL)1;
}

LL cut(LL x, LL c){
	return x/p[len(x)-c];
}

int main(){
	LL n, sz=0, rep=0, res=0, a, b, la, lb;
	scanf("%lld %lld", &n, &a);
	sz = la = len(a);
	for (int i = 1; i < n; i++){
		scanf("%lld", &b);
		lb=len(b);
		sz = max(lb, sz);
		res += sz-lb;
		LL orgb = b;
		b = b*p[ (LL)min(sz, (LL)16)-len(b)]+rep;	
	
		if (la == lb){	// rowne dlugosci
			if (a < b){
				rep = 0;
			}
			else if(a == b){
				if (cut(a, la) == cut(b+(LL)1, la)){
					++rep;
				}
				else{
					
					if (len(b) <= (LL)15){
						b *= (LL)10;
					}
					++sz;
					++res;
					rep = 0;
				}
			}
			else{
				if (cut(b, lb) == cut(a+(LL)1, lb)){
					b = a+(LL)1;
					++rep;
				}
				else{
					if (len(b) <= (LL)15)
						b *= (LL)10;
					++sz;
					++res;
					rep = 0;
				}
			}
			
		}
		else if(la > lb){	// pierwszy dluzszy
			if (cut(a, lb) < cut(b, lb)){ // poczatek pierwszego mniejszy
				rep = 0;
				b = orgb*p[ (LL)min(sz, (LL)16)-len(orgb)];
			}
			else if (cut(a, lb) == cut(b, lb)){
				if (orgb == cut(a+(LL)1, lb)){
					b = a+(LL)1;
					++rep;
				}
				else{
					++sz;
					++res;
					b = orgb*p[ (LL)min(sz, (LL)16)-len(orgb)];
					rep = 0;
				}
			}
			else{
				++sz;
				++res;
				if (len(b) <= (LL)15)
					b *= (LL)10;
				rep = 0;
			}
		}
		la = len(b);
		a = b;
	}
	cout << res << endl;

	return 0;
}