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
#include <cstdio>
#include <cassert>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
using std::string;
const int M = 201013;

int pot[10];

int main() {
	pot[0] = 1;
	for (int i = 1; i <= 9; i++) pot[i] = pot[i-1] * 10;
	int n;
	scanf("%d", &n);
	
	string as = "0";
	int ad = 0;
	int ac = 0;

	long long ret = 0;
	for (int i = 0; i < n; i++) {
		char ss[10];
		scanf("%s", ss);
		
		string bs = ss;
		int bd, bc;

		while (ad && as.size() < bs.size()) {
			ad--;
			if (ad < 9) {
				as += char('0' + (ac/pot[ad]));
				ac %= pot[ad];
			} else {
				as += '0';
			}
		}

		if (as.size() < bs.size()) {
			bd = 0;
			bc = 0;
		} else if (as.substr(0, bs.size()) < bs) {
		  bd = as.size() - bs.size() + ad;
			bc = 0;
		} else if (as.substr(0, bs.size()) == bs) {
			while (as.size() > bs.size() && ad < 9 && ac+1 >= pot[ad]) {
				ac += pot[ad] * (int(as.back()-'0'));
				ad++;
				as = as.substr(0, as.size()-1);
			}
			if (ad > 9 || ac+1 < pot[ad]) {
				ret += as.size();
			  ret	-= bs.size();
				bs = as;
				bd = ad;
				bc = ac+1;
			} else {
				bd = as.size()+ad+1;
				bd -= bs.size();
				bc = 0;
			}
		} else {
			bd = as.size()+ad+1;
			bd -= bs.size();
			bc = 0;
		}

		as = bs;
		ad = bd;
		ac = bc;

		//printf("%s %d %d\n", as.c_str(), ad, ac);

		ret += bd;
	}
	printf("%lld\n", ret);
}