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
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <ctime>
#include<list>
#include <numeric>
#include<bitset>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<functional>
#include<string>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<utility>
#include<fstream>
#include<sstream>
#include<cmath>
#include<stack>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))

//typedef int LL;
typedef long long int LL;
//typedef __int128 LL;
//typedef __int64 LL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<long double, long double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII > VP;
typedef vector<double> VD;
typedef vector<LL> VL;
typedef long double ld;

int NumDigit(int val) {
	int cnt = 0;
	while (val) {
		cnt++;
		val /= 10;
	}
	return cnt;
}

int IsPrefix(LL a, LL b) {
	char S[100], T[100];
	sprintf(S, "%d", a);
	sprintf(T, "%d", b);
	for (int i = 0; S[i]; i++) {
		if (S[i] != T[i]) return 0;
	}
	return 1;
}

tuple<int, int, int> FindNext(int num, int last_num, int last_extra_digit, int last_extra_value) {
	int d = NumDigit(last_extra_value);
	int last_num_zero = last_extra_digit - d;
	char S[100], T[100], E[100];
	sprintf(S, "%d", num);
	sprintf(T, "%d", last_num);
	sprintf(E, "%d", last_extra_value);

	int slen = strlen(S), tlen = strlen(T);
	int compare;
	for (int i = 0; i < slen; i++) {
		int s = S[i] - '0';
		int t;

		if (i < tlen) t = T[i] - '0';
		else if (i < tlen + last_num_zero) t = 0;
		else if (i < tlen + last_extra_digit) t = E[i - tlen - last_num_zero] - '0';
		else {
			return make_tuple(num, 0, 0);
		}

		if (t == s) continue;
		if (t > s) return make_tuple(num, MAX(0, tlen + last_extra_digit - slen + 1), 0);
		if (t < s) {
			return make_tuple(num, MAX(0, tlen + last_extra_digit - slen), 0);
		}
	}
	if (last_extra_digit == 0) {
		if (num == last_num) {
			return make_tuple(num, 1, 0);
		}
		if (IsPrefix(num, last_num + 1)) {
			return make_tuple(last_num + 1, 0, 0);
		}
		return make_tuple(num, tlen - slen + 1, 0);
	}

	// t..numzero..value
	// s is prefix-of-(t)
	if (last_extra_digit > 7) {
		return make_tuple(last_num, last_extra_digit, last_extra_value + 1);
	}

	LL x = last_num;
	for (int i = 1; i <= last_extra_digit; i++) x *= 10;
	x += last_extra_value;
	x++;
	if (IsPrefix(num, x)) return make_tuple(last_num, last_extra_digit, last_extra_value + 1);
	return make_tuple(num, tlen - slen + 1 + last_extra_digit, 0);
}

void solve() {
	int n;
	int last_num, last_extra_digit, last_extra_value;
	LL ans = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		int num;
		scanf("%d", &num);
		if (i == 1) {
			last_num = num;
			last_extra_value = last_extra_digit = 0;
		}
		else {
			tie(last_num, last_extra_digit, last_extra_value) = FindNext(num, last_num, last_extra_digit, last_extra_value);
		}
		//printf("%d>>> %d %d %d\n", num, last_num, last_extra_digit, last_extra_value);
		ans += NumDigit(last_num) + last_extra_digit - NumDigit(num);
	}
	printf("%lld\n", ans);
}

int main()
{
#ifdef LOCAL
	double start_time = clock();
	freopen("C:\\Home\\ContestCodes\\sample.in", "r", stdin);
	//	freopen("out.out", "w", stdout);
#endif

	solve();

#ifdef LOCAL
	double end_time = clock();
	fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC);
#endif

	return 0;
}