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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>

using ull = unsigned long long;
using namespace std;
ull answer = 0;

enum class Case
{
	BEGINNING_EQUAL,
	BEGINNING_GREATER,
	BEGINNING_SMALLER
};

struct number
{
	ull n;
	std::vector<short> num;
	int trailingZeros;
	bool endsWithOne = false;

	size_t size() const { return num.size() + trailingZeros; }
};

void splitNumber(ull n, number& split)
{
	split.num.clear();
	split.n = n;
	split.trailingZeros = 0;
	split.endsWithOne = false;

	while (n != 0) 
	{
		split.num.push_back(n % 10);
		n /= 10;
	}

	std::reverse(begin(split.num), end(split.num));
}

void unroll(number& num, int howManyTimes)
{
	howManyTimes = std::min(howManyTimes, num.trailingZeros);
	num.trailingZeros -= howManyTimes;

	while (howManyTimes--)
	{
		num.num.push_back(0);
		num.n *= 10;
	}

	if(num.endsWithOne && num.trailingZeros == 0)
	{
		num.num[num.num.size() - 1] = 1;
		num.n++;
		num.endsWithOne = false;
	}
}

// * 10^zerosCount
void fillWithZeros(number& a, int zerosCount)
{
	a.trailingZeros += zerosCount;
	answer += zerosCount;
}

// is a >= b?
bool isGreaterOrEqual(number& prev, number& curr)
{
	if (prev.num.size() < curr.num.size())
		unroll(prev, curr.num.size() - prev.num.size());

	return prev.n >= curr.n;
}

Case detectCase(number& prev, number& curr)
{
	if (prev.num.size() < curr.num.size())
		unroll(prev, curr.num.size() - prev.num.size());

	for(int i = 0; i < curr.num.size(); ++i)
	{
		if (curr.num[i] > prev.num[i]) return Case::BEGINNING_GREATER;
		if (curr.num[i] < prev.num[i]) return Case::BEGINNING_SMALLER;
	}

	return Case::BEGINNING_EQUAL;
}

void handleEqualCase(number& prev, number& curr, int currentSize)
{
	if (prev.num.size() < curr.num.size())
		unroll(prev, curr.num.size() - prev.num.size());
	if (curr.num.size() < prev.num.size())
		unroll(curr, prev.num.size() - curr.num.size());

	if(prev.trailingZeros + prev.num.size() >= 18)
	{
		for (int i = prev.num.size() - 1; i >= currentSize; --i)
		{
			curr.num[i] = prev.num[i];
		}
		curr.n = prev.n;
		curr.endsWithOne = true;
		return;
	}

	unroll(prev, prev.trailingZeros);
	unroll(curr, curr.trailingZeros);

	for(int i = prev.num.size() - 1; i >= currentSize; --i)
	{
		if(prev.num[i] != 9)
		{
			curr.num[i] = prev.num[i] + 1;
			curr.n = prev.n + 1;

			--i;
			for (; i >= currentSize; --i)
			{
					curr.num[i] = prev.num[i];
			}
			return;
		}
	}

	//// shit, add zero
	fillWithZeros(curr, 1);
}

ull solve(vector<ull>& numbers)
{
	auto previousNumber = new number;
	auto currentNumber = new number;

	previousNumber->num.reserve(200100);
	currentNumber->num.reserve(200100);

	splitNumber(numbers[0], *previousNumber);

	for(int i = 1; i < numbers.size(); ++i)
	{
		splitNumber(numbers[i], *currentNumber);

		// 2. the same length but prev >= curr
		if(previousNumber->size() == currentNumber->size() && isGreaterOrEqual(*previousNumber, *currentNumber))
		{
			fillWithZeros(*currentNumber, 1);
		}
		// prev length is greater than curr
		else if(previousNumber->size() > currentNumber->size())
		{
			auto whatCase = detectCase(*previousNumber, *currentNumber);
			const auto zerosCount = previousNumber->size() - currentNumber->size();

			if (whatCase == Case::BEGINNING_GREATER)
				fillWithZeros(*currentNumber, zerosCount);
			else if (whatCase == Case::BEGINNING_SMALLER)
				fillWithZeros(*currentNumber, zerosCount + 1);
			else
			{
				int currentSize = currentNumber->size();
				fillWithZeros(*currentNumber, zerosCount);
				handleEqualCase(*previousNumber, *currentNumber, currentSize);
			}
		}

		std::swap(currentNumber, previousNumber);
	}

	return answer;
}

int main()
{
	ios::sync_with_stdio(false);
	int n;
	//std::vector<ull> numbers = { 976, 101 }; // 1010
	//std::vector<ull> numbers = {976, 976}; // 9760
	//std::vector<ull> numbers = {959, 95}; // 9500
	//std::vector<ull> numbers = { 958, 95 }; // 959
	//std::vector<ull> numbers = { 101, 5 }; // 500
	//std::vector<ull> numbers = { 101, 100 }; // 1000
	//std::vector<ull> numbers = {2001, 1}; // 10000
	//std::vector<ull> numbers = { 999, 99 }; // 9900
	//std::vector<ull> numbers = { 998, 99 }; // 999
	//std::vector<ull> numbers = { 995, 9 }; // 996
	//std::vector<ull> numbers = { 106, 10 }; // 107
	//std::vector<ull> numbers = { 1060, 10 }; // 1061

	std::vector<ull> numbers;
	cin >> n;

	numbers.resize(n);
	for(int i = 0; i < n; ++i)
	{
		cin >> numbers[i];
	}

	cout << solve(numbers);
	return 0;
}