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
/*
 *  Copyright (C) 2018  Paweł Widera
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details:
 *  http://www.gnu.org/licenses/gpl.html
 */
#include <iostream>
#include <string>
#include <cmath>
#include <tuple>
using namespace std;

#define DBG(key, value) cerr << key << " " << value << endl;


string increment(const string& s) {
	int i = s.length();
	while (s[--i] == '9') {}

	auto digit = to_string(s[i] - '0' + 1);
	auto zeros = string(s.length() - i - 1, '0');
	return s.substr(0, i) + digit + zeros;
}


int count(string& payment, const string& previous) {
	int diff = previous.length() - payment.length();

	// no change if payment already strictly greater then previous
	if ((diff == 0 && payment > previous) || diff < 0) {
		return 0;
	}

	// add extra zero
	if (diff == 0) {
		payment += "0";
		return 1;
	}

	// match length by right-padding with zeroes
	int status = previous.compare(0, payment.length(), payment);
	if (status < 0) {
		payment += string(diff, '0');
		return diff;
	}

	// right-pad with incremented digits from previous
	if (status == 0) {
		auto digits = previous.substr(previous.length() - diff);
		if (digits < string(diff, '9')) {
			payment += increment(digits);
			return diff;
		}
	}

	// match length + 1 with zeros
	payment += string(diff + 1, '0');
	return diff + 1;
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	string payment;

	string previous = "0";
	unsigned long long int counter = 0;

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> payment;
		counter += count(payment, previous);
		previous = payment;
	}

	cout << counter << endl;
	return 0;
}