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
#include <bits/stdc++.h>
using namespace std;
#define VAR(n,v) decltype(v) n=(v)
#define REP(i,n) for(int i=1; i<=(n); ++i)
#define FOR(i,a,b) for(VAR(i,a); i!=(b); ++i)
#define FORE(it,t) FOR(it,t.begin(),t.end())
typedef vector<int> vi;
#define pb push_back
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef long long ll;
#define INF 1000000001
#define sz size()
#define ALL(t) (t).begin(),(t).end()
#define SC(a) scanf("%d", &a)
#define GET(a) int a; SC(a)
#define ISDEBUG 0
#define dprintf(...) if(ISDEBUG) \
	{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ")
	{if(ISDEBUG) {for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n"); }}

ll counter = 0;

string compute_new_last(string &prev, string &next) {
	if(prev.sz < next.sz)
		return next;
	if(prev.sz == next.sz) {
		if(prev < next)
			return next;
		else {
			++counter;
			return next + "0";
		}
	}
	if(prev.sz > next.sz) {
		int comparsion = prev.compare(0, next.sz, next);
		int k = prev.sz - next.sz;
		if(comparsion == 0) {
			//increment last
			for(int i=prev.sz-1;i>=next.sz;--i) {
				if(prev[i] == '9') {
					prev[i] = '0';
				} else {
					prev[i]++;
					counter += k;
					return prev;
				}
			}
			//in the case of k nines add k + 1 zeroes
			counter += k + 1;
			FOR(i,0,k+1) next += "0";
		} if(comparsion < 0) { //prev prefix is lower, add k zeroes
			counter += k;
			FOR(i,0,k) next += "0";
		} if(comparsion > 0) { //prev prefix is greater, add k + 1 zeroes
			counter += k + 1;
			FOR(i,0,k+1) next += "0";

		}
		return next;
	}
}


int main() {
	GET(n);
	string last, next;
	char buff[16];
	scanf("%s", buff);
	last = string(buff);
	--n;
	while(n--) {
		dprintf("%s\n", last.c_str());
		scanf("%s", buff);
		next = string(buff);
		last = compute_new_last(last, next);
	}
	dprintf("%s\n", last.c_str());
	printf("%lld\n", counter);
}