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
#include <iostream>
#include <cmath>

#define MAX_SIZE 200000

struct number_info {
	int positions;
	int digit;
};

number_info get_number_info(unsigned long long x) {
	number_info num_x;

	num_x.positions = 0;
	num_x.digit = 0;

	while (x > 0) {
		num_x.digit = x % 10;
		x /= 10;
		++(num_x.positions);
	}

	return num_x;
}

void print_number_info(number_info ni) {
	std::cout << "DBG NUM INFO: " << ni.positions << " " << ni.digit << std::endl;
}

int make_greater(const unsigned long long& a, unsigned long long& b) {
	number_info num_a = get_number_info(a);
	number_info num_b = get_number_info(b);
	
	// print_number_info(num_a);
	// print_number_info(num_b);

	int pos_diff = num_a.positions - num_b.positions;
	unsigned long long tmp_a = 0;
	unsigned long long rest = 0;
	unsigned long long tmp = 0;

	if (num_a.digit < num_b.digit) {
		b *= pow(10, pos_diff);
	} else if (num_a.digit > num_b.digit) {
		b *= pow(10, ++pos_diff);
	} else {
		tmp_a = a / pow(10, pos_diff);
		if (tmp_a > b) {
			b *= pow(10, ++pos_diff);
		} else if (tmp_a < b) {
			b *= pow(10, pos_diff);
		} else {
			tmp = pow(10, pos_diff);
			b *= tmp;
			rest = a % b;
			if (rest != tmp - 1) { // nie ma samych 9 w reszczie
				b += rest + 1;
			} else {
				++pos_diff;
				b *= 10;
			}
		}
	}

	// std::cout << "DBG GT: " << a << " " << b << " " << pos_diff << std::endl;

	return pos_diff;

}

int main() {
	int n;
	unsigned long long tab[MAX_SIZE];
	unsigned long long result = 0;

	std::cin >> n;

	// make_greater(n, n2); //debug

	for (int i = 0; i < n; ++i) {
		std::cin >> tab[i];
	}

	for (int i = 0, j = 1; j < n; ++i, ++j) {
		if (tab[i] >= tab[j]) {
			result += make_greater(tab[i], tab[j]);
		}
	}

	std::cout << result;

	return 0;
}