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
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <sstream>


using namespace std;

#define REP(i,n) for(int i=0; i<(int)n; i++)
#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef vector<int> VI;
typedef pair<int,int> PI;
typedef vector<PI> VPII;
typedef set<int> SI;
typedef long long LL;

#ifdef DEBUG
const bool debug = true;
#else
const bool debug = false;
#endif
int n,m,k,l, test = 1;
const int INF = 1000 * 1000 * 1000 ;
const int MAKSN = 1000 * 1000 + 13; // UZUPElnic

long long ans = 0, ileL = 0, ileKonc = 0, last;


void readIn()
{
	cin >> n; 
	cin >> last;
}


int cntDigits(long long num) {
	stringstream ss; 
	ss << num;
	return ss.str().size();
}

long long poww10(int x) {
	long long res = 1;
	for(int i = 0; i < x; i++) 
		res *= 10;
	return res;
}

string cutKDigits(LL from, int num) {
	stringstream ss;
	ss << from;
	string s = ss.str().substr(0, num);
	return s;
}

string lastKDigits(LL from, int pos) {
	stringstream ss;
	ss << from;
	string s = ss.str().substr(ss.str().size() - pos);
	return s;
}

long long strToVal(string s) {
	istringstream ss(s);
	long long res;
	ss >> res;
	return res;
}


void compare() {
	LL tmp = last * poww10(min(9ll, ileL));
	if(ileL <= 9)
		tmp += ileKonc;
	else {
		tmp = tmp + (ileKonc % poww10(min(9ll, ileL)));
	}

	if(k > tmp) {
		ileKonc = 0;
		ileL = 0;
		return;
	}

	int ileCyfrNowa = cntDigits(k);

	int lenSamaPoprz = cntDigits(last);
	int dlPoprz = lenSamaPoprz + ileL;
	long long obcieta = strToVal(cutKDigits(tmp, min(dlPoprz, ileCyfrNowa)));
	// cerr << k << " " <<tmp << " " << last << " " << obcieta << " " << ileL << " " << dlPoprz << "\n";
	// cerr<< "ileKonc = " << ileKonc << "\n";

	if (obcieta > k) {
		ileL = max(0, dlPoprz - ileCyfrNowa) + 1;
		ileKonc = 0;
	}
	if (obcieta < k) {
		ileL = max(0, dlPoprz - ileCyfrNowa);
		ileKonc = 0;
	}
	if (obcieta == k) {
		if(ileL >= 10) {
			k = last;
			ans += lenSamaPoprz - ileCyfrNowa;
			return;
		}

		// if (lenSamaPoprz > ileCyfrNowa) {
		// 	// if ()
		// 	k = last;
		// 	// ans += lenSamaPoprz - ileCyfrNowa;

		// }


		// if (lenSamaPoprz > ileCyfrNowa) {
			LL rem = strToVal(lastKDigits(tmp, ileCyfrNowa));
			int diff = lenSamaPoprz - ileCyfrNowa;

			// cerr << "rem " << rem  << "\n";
			// rem *=  pow10(diff-1);
			rem++;
			int lenKonc = cntDigits(rem);
			// cerr << "rem2 " << rem <<  " " << lenKonc << " " << diff << "\n";
			
			if (lenKonc > diff + ileL) {
				ileL = max(0, dlPoprz - ileCyfrNowa) + 1;
				ileKonc = 0;
				return;
			}
			else {
				ileKonc = rem;
				ileL = max(0, dlPoprz - ileCyfrNowa);
				return;
			}


		// }

		// cerr << "TA\n";
		// ileKonc++;
		// lenKonc = cntDigits(ileKonc);

		// cerr << ileKonc << "KONC\n";
		// int mod = 0;
		// if(lenKonc > ileL) {
		// 	mod = 1;
		// 	ileKonc = 0;
		// } 

		// ileL = max(0, dlPoprz - ileCyfrNowa) + mod;
	}

	// cerr << tmp << " "  <<  cutKDigits(tmp, ileCyfr) << "\n";
	// cerr << "res =  " << res << "\n";
}

// void akt(LL last, LL k) {

// // cerr << k << " " << cntDigits(k) << "\n";;
// 	compare(k, last);
// }	



void solve()
{
	for(int i = 0; i < n - 1; i++) {
		cin >> k;
		// akt(last, k);
		compare();
		ans += ileL;
		last = k;
		// LL tmp = last * poww10(min(9ll, ileL));
	// if(ileL <= 9)
		// tmp += ileKonc;
	// cerr << " === " << tmp << " " << ileL << " " << "\n";
	}

	cout << ans << "\n";
}

void zeruj()
{
}

int main()
{
	ios_base::sync_with_stdio(0);
	// cin >> test;
    // REP(i, test)
    // {
    zeruj();
    readIn();
    solve();
    // }

    return 0;
}