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

using namespace std;

typedef unsigned long long LL;

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(val) ((val) < 0 ? -(val) : (val))

#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)
#define VAR(v, n) __typeof(n) v = (n)
#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 M 1000000007ULL

/* PA2018 KON */

static int nines[10] = {
	-1, 
	9, 
	99, 
	999, 
	9999, 
	99999, 
	999999, 
	9999999, 
	99999999, 
	999999999
 };

struct R
{
	int no;
	int extra_dig;
	int offset;

	void from(R& old)
	{
		no = old.no;
		extra_dig = old.extra_dig;
		offset = old.offset;
	}

	R(string &s, int sum_of_dig)
	{
		no = stoi(s);
		extra_dig = sum_of_dig - s.length();
		offset = 0;
	}

	void print()
	{
#if 0
		cout << "[" << no << ", " << extra_dig << ", " << offset << "]";
		cout << "\n";
#endif 

		int len_of_offset = offset ? to_string(offset).length() : 0;

		int zrs = extra_dig - len_of_offset;
		cout << no;
		REP(i, zrs)
		{
			cout << "0";
		}
		if (len_of_offset)
			cout << offset;
		cout << "\n";
	}

	void inc()
	{
		if (extra_dig == 0)
		{
			no++;
			return;
		}

		if (extra_dig < 10)
		{
			if (offset == nines[extra_dig])
			{
				offset = 0;
				no++;
				return;
			}
		}
		offset++;
	}

	int get_len()
	{
		return to_string(no).length() + extra_dig;
	}

	string get_rep(int dig)
	{
		string s = to_string(no);
		if (s.length() >= dig)
		{
			return s.substr(0, dig);
		}

		int rem = dig - s.length();

		string offset_s = to_string(offset);
		int len_of_offset = offset ? offset_s.length() : 0;

		int len_of_zers = extra_dig - len_of_offset;

		int eff_zeros = MIN(len_of_zers, rem);
	
		REP(p, eff_zeros)
		{
			s += "0"; 
		}

		rem -= eff_zeros;

		s += offset_s.substr(0, rem);
			
		return s;

	}

};




int main() {

	std::ios_base::sync_with_stdio(false);

	LL digit = 0LL;

	int cnt;
	cin >> cnt;

	string first;
	cin >> first;
	R old(first, first.length());
	
	REP(i, cnt - 1)
	{
		string s;
		cin >> s;

		int len = s.length();

		old.inc();
		int old_len = old.get_len();
		if (old_len < len)
		{
			R cur(s, len);
			old.from(cur);			
		}
		else
		{
			R cur(s, old_len);

			string old_rep = old.get_rep(len);

			int cmp = s.compare(old_rep);
	
			if (cmp < 0)
			{
				R cur(s, old_len + 1);
				old.from(cur);			
				//new dig
			}
			else if (cmp > 0)
			{
				//keep new
				old.from(cur);
			}
			else 
			{
				//keep old
			}
		}

//		old.print();
		
		digit += (LL)(old.get_len() - len);
	
		


			

		//cout << s << " - R - " << cur.get_rep(4) << endl;;

		
	}

	cout << digit;
	
	return 0;
}