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
#include <cstdio>
#include <cstdlib>
#include <cstdint>

#include <string>

static const uint64_t TRESHOLD = 10ULL * 1000ULL * 1000ULL * 1000ULL * 1000ULL * 1000ULL;

uint64_t numberLength(uint64_t x) {
	uint64_t ret = 0;
	while (x > 0) {
		x /= 10;
		ret++;
	}
	return ret;
}

uint64_t pow10(uint64_t x) {
	uint64_t ret = 1;
	while (x > 0) {
		x--;
		ret *= 10;
	}
	return ret;
}

uint64_t getu64() {
	unsigned long long int tmp;
	scanf("%llu", &tmp);
	return (uint64_t)tmp;
}

int main() {
	auto n = getu64();

	uint64_t digitsToAdd = 0;
	uint64_t overflow = 0;
	uint64_t current = getu64();

	n -= 1;

	while (n-- > 0) {
		current++;

		const auto next = getu64();
		const auto cLen = numberLength(current);
		const auto nLen = numberLength(next);

		if (nLen > cLen) {
			// Next number is longer, so take it as a current number
			current = next;
		}
		else {
			const auto currentTruncated = current / pow10(cLen - nLen);
			if (next > currentTruncated) {
				// We can take the next number and fill it with zeros at the end
				current = next * pow10(cLen - nLen);
				digitsToAdd += cLen - nLen + overflow;
			}
			else if (next == currentTruncated) {
				// Just take the "current" number
				digitsToAdd += cLen - nLen + overflow;
			}
			else {
				if (current >= TRESHOLD) {
					// Mark added digit as overflow
					current = next * pow10(cLen - nLen);
					overflow += 1;
					digitsToAdd += cLen - nLen + overflow;
				}
				else {
					current = next * pow10(cLen - nLen + 1);
					digitsToAdd += cLen - nLen + 1 + overflow;
				}
			}
		}
	}

	printf("%llu\n", (unsigned long long int)digitsToAdd);

	return 0;
}