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
#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<pii, int> para;
const int inf = 1e9 + 7;
const int maxN = 5e6 + 5;

string s;
int n;

struct Number {
	ll prefix;
	int zeros;
	ll suffix;
};

int len(ll x) {
	int res = 0;
	while (x != 0) {
		res++;
		x /= 10;
	}
	return res;
}

struct Number arr[maxN];

int compareLex(struct Number n1, struct Number n2) {
	string s1 = to_string(n1.prefix);
	string s2 = to_string(n2.prefix);
	for (int i = 0; i < s2.size(); i++) {
		if (s1[i] > s2[i]) {
			return 1;
		} else if (s1[i] < s2[i]) {
			return -1;
		}
	}
	return 0;
}

bool multiplyTen(struct Number &n1) {
	if (n1.prefix < 1e9) {
		n1.prefix *= 10LL;
		return true;
	} else {
		if (n1.suffix != 0) {
			n1.suffix = 0;
		}
		n1.zeros++;
		return false;
	}
}

bool checkNines(string s) {
	for (int i = 0; i < s.size(); i++) {
		if (s[i] != '9')
			return false;
	}
	return true;
}

int compute(struct Number &n1, struct Number &n2) {
	if (n1.zeros == 0 && n1.suffix == 0) {
		if (n1.prefix >= n2.prefix) {
			int x = compareLex(n1, n2);
			int diff = len(n1.prefix) - len(n2.prefix);
			if (x == 0) {
				string s = to_string(n1.prefix).substr(len(n2.prefix), diff);
				if (!checkNines(s)) {
					n2.prefix = n1.prefix + 1;
					return diff;
				}
			}
			if (x >= 0) diff++;
			int tmp = diff;
			while (tmp > 0) {
				multiplyTen(n2);
				tmp--;
			}
			return diff;
		}
		return 0;
	} else {
		int x = compareLex(n1, n2);
		int diff = len(n1.prefix) - len(n2.prefix) + n1.zeros + len(n1.suffix);
		//cout << n1.prefix << " " << diff << endl;
		if (x == 0) {
			if (n1.suffix == 0 || !checkNines(to_string(n1.suffix))) {
				int tmp = diff;
				bool l;
				do {
					l = multiplyTen(n2);
					tmp--;
				} while(tmp > 0 && l);
				if (tmp > 0)
					n2.zeros += (tmp - 1);
				n2.suffix = n1.suffix + 1;
				n2.prefix = n1.prefix;
				if (n1.suffix == 0) {n2.zeros--;}
				else {
					n2.zeros = n1.zeros;
				}
				return diff;
			} else {
				//diff++;
				int tmp = diff;
				bool l;
				do {
					l = multiplyTen(n2);
					tmp--;
				} while(tmp > 0 && l);
				n2.prefix = n1.prefix;
				n2.zeros += tmp;
				//cout << "ZEROS " << n2.zeros << endl;
				if (n2.zeros <= 1) {
					n2.prefix += 1;
					if (n2.prefix == 1e10) {
						n2.prefix /= 10;
						n2.zeros += 1;
					}
					n2.suffix = 0;
				} else {
					n2.zeros -= 1;
					n2.suffix = n1.suffix + 1;
				}
				return diff;
			}
		}
		diff += (x > 0);
		int tmp = diff;
		bool l;
		do {
			l = multiplyTen(n2);
			tmp--;
		} while(tmp > 0 && l);
		//cout << n2.zeros << " " << tmp << endl;
		n2.zeros += tmp;
		return diff;
	}
}

int res[maxN];

int main() {
	ios_base::sync_with_stdio(0);
	cin >> n;
	REP(i, n) {
		int a;
		cin >> a;
		struct Number num = {a, 0, 0};
		arr[i] = num;
	}
	ll sum = 0;
	RI(i, n - 1) {
		int x = compute(arr[i - 1], arr[i]);
		sum += x;
		res[i] = x;
		//cout << x << endl;
	}
	/*REP(i, n) {
		cout << arr[i].prefix << " " << arr[i].zeros << " " << arr[i].suffix << " " << res[i] << endl;
	} */
	cout << sum;
	// (maksymalnie < 100 rzeczy do porównania)
	// jesli poprzedni prefix < moj prefix
	// dodaj zera i tyle
	// jesli poprzendi prefix > moj prefix
	// dodaj zera ale o jedno wiecej
	// jesli sa rowne 
	// wez ostatni z poprzedniego, dodaj jeden, przenies az do skutku (99999)
	return 0;
}