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 <cstdio>
#include <cstring>
#include <algorithm>

typedef long long LL;

void inc(char x[20], int len) {
	for(int i = len-1; i >= 0; i--) {
		if(x[i] == '9') x[i] = '0';
		else {
			x[i]++;
			return;
		}
	}
	x[0] = '1';
	x[len] = '0';
}

int next1(char x[20], char y[20]) {
	int lx = strlen(x);
	int ly = strlen(y);

	if(lx < ly) {
		std::copy(y, y+ly, x);
		return 0;
	}

	for(int i = 0; i < ly; i++) {
		if(x[i] < y[i]) {
			std::copy(y, y+ly, x);
			std::fill(x+ly, x+lx, '0');
			return lx-ly;
		}
		if(x[i] > y[i]) {
			std::copy(y, y+ly, x);
			std::fill(x+ly, x+lx+1, '0');
			return lx-ly+1;
		}
	}

	for(int i = ly; i < lx; i++)
		if(x[i] != '9') {
			inc(x, lx);
			return lx-ly;
		}

	std::copy(y, y+ly, x);
	std::fill(x+ly, x+lx+1, '0');
	return lx-ly+1;
}

int next2(char x[20], int& len, char y[20]) {
	int ly = strlen(y);

	for(int i = 0; i < ly; i++) {
		if(x[i] != y[i]) {
			if(x[i] > y[i]) len++;
			std::copy(y, y+ly, x);
			std::fill(x+ly, x+10, '0');
			break;
		}
	}

	return len-ly;
}

int main() {
	int n;
	scanf("%i", &n);

	LL ans = 0;

	char x[20] = {0};
	char y[20] = {0};

	scanf("%s", &x);
	n--;

	for(; n > 0 && strlen(x) < 18; n--) {
		scanf("%s", y);
		ans += next1(x, y);
	}

	int extra = strlen(x);

	for(; n > 0; n--) {
		scanf("%s", y);
		ans += next2(x, extra, y);
	}

	printf("%lli", ans);
}