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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,n) for (auto i ## __ = (n), i = (a); i <= i ## __; ++i)
#define FORD(i,a,n) for (auto i ## __ = (n), i = (a); i >= i ## __; --i)
#define REP(i,n) FOR(i,0, n - 1)
#define ALL(h) begin(h), end(h)
#define EB emplace_back
#define X first
#define Y second
#define V vector
#define tpv typedef V<

typedef long long LL;
typedef pair<int, int> PII;
tpv int> VI;
tpv VI> VVI;
tpv PII> VPII;
tpv LL> VLL;

constexpr char nl = '\n';
#define endl nl
#define ris return *this
#define tem template<class t

tem, class u> inline void mini(t& a, u&& b) { if (b < a) a = b; }
tem, class u> inline void maxi(t& a, u&& b) { if (b > a) a = b; }
int ceil2(int h) { return h < 2 ? 1 : 1 << (sizeof(h) * 8 - __builtin_clz(h - 1)); }

tem> struct Dump { t a, b; };
tem> auto dump(t&& h) -> Dump<decltype(begin(h))> { return {ALL(h)}; }
tem> auto stub(t* h) -> decltype(cerr << *h, 0);
tem> char stub(...);
#define enif(o) tem> typename enable_if<sizeof stub<t>(0) o 1, debug&>::type operator<<(t h)
#define dor > debug& operator<<
struct debug {
#ifdef DEBUG
#define deb debug()
	~debug() { cerr << nl; }
	enif(!=) { cerr << boolalpha << h; ris; }
	enif(==) {
		*this << '{';
		for (auto a = begin(h), b = end(h); a != b;)
			*this << *a++ << &" "[a == b];
		ris << '}';
	}
	tem, class u dor(pair<t, u> p) { ris << '(' << p.X << ", " << p.Y << ')'; }
	tem dor(Dump<t> d) {
		*this << "{\n";
		for (t a = d.a, c = a; a != d.b; ++a)
			*this << "  " << distance(c, a) << ": " << *a << nl;
		ris << '}';
	}
#else
	operator int() { return 0; }
#define deb 0 and debug()
	tem dor(t&&) { ris; }
#endif
};

#define imie(h...) #h ": " << (h) << " "
#define LOG(h...) deb << imie(h)
#define DOG(h...) deb << #h ": " << dump(h) << " "

// 0 ma 0 cyfr
int digits(int num) {
	int ret = 0;
	while (num > 0) {
		ret++;
		num /= 10;
	}
	return ret;
}

const LL exps[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000};

struct Number {
	int base;
	int exp = 0;
	int reminder = 0;
	int len;
	
	LL to_LL() const {
		assert(len < 19);
		return base * exps[exp] + reminder;
	}
	
	string extractPrefix(int mx_len) const {
		mx_len = min(mx_len, len);
		string ret;
		if (len < 18) { // przerabiamy na long longa i zwracamy prefix long longa
			LL x = to_LL();
			ret = to_string(x);
			ret.resize(mx_len, '0');
		}
		else { // base i zera bo reminder nie ma znaczenia
			ret = to_string(base);
			ret.resize(mx_len, '0');
		}
		return ret;
	}
	
	Number(int n)
		: base(n), len(digits(n)) {}
	
	Number& operator++() {
		if (digits(reminder + 1) > exp) {
			base++;
			reminder = 0;
			len = digits(base) + exp;
		}
		else
			reminder++;
		return *this;
	}
	
	void addZeros(int num) {
		exp += num;
		len += num;
	}
	
	bool operator<(const Number &num) const {
		if (len != num.len)
			return len < num.len;
		
		if (len < 18) // przerabiamy na long longa i porownujemy
			return to_LL() < num.to_LL();
		// reminder ma znaczenie tylko przy =
		if (exp >= num.exp) {
			int dif = exp - num.exp;
			LL base_adj = base * exps[dif];
			if (base_adj == num.base)
				return reminder < num.reminder;
			return base_adj < num.base;
		}
		else {
			int dif = num.exp - exp;
			LL base_adj = num.base * exps[dif];
			if (num.base == base_adj)
				return reminder < num.reminder;
			return base < base_adj;
		}
	}
	
	// czy this jest prefixem num
	bool isPrefix(const Number &num) const {
		assert(reminder == 0 && exp == 0 && base <= 1000000000);
		if (num.len < len)
			return false;
		string num_pref = num.extractPrefix(len);
		string pref = extractPrefix(len);
		REP (i, len)
			if (pref[i] != num_pref[i])
				return false;
		return true;
	}
	
};
ostream& operator<<(ostream &ost, const Number& num) {
	return ost << "(" << num.base << " * 10^" << num.exp << " + " << num.reminder << ")";
}

void input(int &n, V<Number>& v) {
	cin >> n;
	REP (i, n) {
		int num;
		cin >> num;
		v.EB(num);
	}
}

void geninput1(int& n, V<Number>& v) {
	const int val = 1e9;
	n = 2e5;
	for (int i = 0; i < n; ++i)
		v.EB(1 + (i * 1ll * i) % val);
}

void geninput2(int& n, V<Number>& v) {
	const int val = 5e2;
	n = 2e5;
	for (int i = 0; i < n; ++i)
		v.EB(1 + ((i * 1ll * i) ^ i ^ (i + 17)) % val);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n;
	V<Number> v;
	
	input(n, v);
	
	LL befor = 0;
	for (auto &i : v)
		befor += i.len;
	
	FOR (i, 1, n - 1) {
		Number bx = v[i - 1];
		++bx;
		if (v[i] < bx) {
			if (v[i].isPrefix(bx))
				v[i] = move(bx);
			else {
				v[i].addZeros(bx.len - v[i].len);
				if (v[i] < bx)
					v[i].addZeros(1);
			}
		}
	}
	
	LL after = 0;
	REP (i, n) {
		after += v[i].len;
		// LOG(v[i]);
	}
	
	cout << after - befor << endl;
	
	return 0;
}
/*
3
8
5
13
*/