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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <numeric>
using namespace std;

namespace stanczyk {
	typedef int INT;
	typedef long long LL;
	typedef long double DOUBLE;

	const INT INF = 1000000001;
	constexpr const INT EPS_PRECISION = -9;
	const DOUBLE EPS = pow((DOUBLE)10.0, (DOUBLE)EPS_PRECISION);

	typedef vector<INT> VI;
}
using namespace stanczyk;



#define FOR(x, b, e) for (int x = b; x <= (e); ++x)
#define FORD(x, b, e) for (int x = b; x >= (e); --x)
#define REP(x, n) for (int x = 0; x<(n); ++x)
#ifdef _MSC_VER 
#define VAR(v, n) decltype(n) v = (n)
#else
#define VAR(v, n) __typeof(n) v = (n)
#endif
#define ALL(c) c.begin(), c.end()
#define SIZE(x) (int)x.size()
#define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define PF push_front
#define MP make_pair

#include <string>

using namespace std;


long long extensionLength = 0;
long long numbersInExtension = 0;
long long result = 0;
long long previousBase;

void newInExtension() {
	numbersInExtension++;
	result += extensionLength;
}

void clearInExtension() {
	numbersInExtension = 0;
	result += extensionLength;
}

void newDigit() {
	extensionLength++;
	clearInExtension();
}

int length(long long x) {
	if (x <= 0) {
		return -1;
	}
	return (int)(log10(x));
}

bool haveRoomInExtension() {
	if (extensionLength == 0)return false;
	if (numbersInExtension % 10 == 0)return true;
	return extensionLength > 0 && length(numbersInExtension) == length(numbersInExtension + 1);
}

int main() {
	int n;
	scanf("%d", &n);
	scanf("%lld", &previousBase);

	vector<int> digits;

	for (int i = 1;i < n;++i) {
		long long currentBase;
		scanf("%lld", &currentBase);

		int previousLength = length(previousBase);
		int currentLength = length(currentBase);

		long long remnant = numbersInExtension;
		digits.clear();
		bool wasExtending = false;

		while (currentLength > previousLength) {
			// Materialize base
			previousBase *= 10;
			extensionLength--;
			digits.push_back(remnant % 10);
			remnant /= 10;
			previousLength++;
			wasExtending = true;
		}

		if (wasExtending) {
			previousBase += remnant;
			numbersInExtension = 0;
			for (auto digit = digits.rbegin();digit != digits.rend();++digit) {
				numbersInExtension *= 10;
				numbersInExtension += *digit;
			}
		}

		if (currentLength < previousLength) {
			// Current is shorter
			long long maxBaseExtended = currentBase;
			long long baseExtended = currentBase;
			long long fillingLength = 0;

			while (length(maxBaseExtended) < length(previousBase)) {
				maxBaseExtended *= 10;
				maxBaseExtended += 9;
				baseExtended *= 10;
				fillingLength++;
			}

			if (maxBaseExtended > previousBase) {
				// I can add new to the same length or increase base
				if (haveRoomInExtension()) {
					// I can add number without new digit
					newInExtension();
				}
				else {
					maxBaseExtended = previousBase + 1;
					clearInExtension();
				}

				currentBase = maxBaseExtended;
			}
			else if (maxBaseExtended == previousBase) {
				// I can add only to the same length
				if (haveRoomInExtension()) {
					// I can add number without new digit
					newInExtension();
					currentBase = maxBaseExtended;
				}
				else {
					// I need new digit
					newDigit();
					currentBase = baseExtended;
				}
			}
			else {
				// I need new digit
				newDigit();
				currentBase = baseExtended;
			}
			result += fillingLength;
		}
		else {
			// The same length
			if (currentBase > previousBase) {
				clearInExtension();
			}
			else if (currentBase == previousBase) {
				if (haveRoomInExtension()) {
					// I can add number without new digit
					newInExtension();
				}
				else {
					// I need new digit
					newDigit();
				}
			}
			else {
				newDigit();
			}
		}

		previousBase = currentBase;
	}

	printf("%lld", result);
}