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

#ifdef LOCAL
template<class T, class U>
ostream& operator<<(ostream& os, pair<T, U> p) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template<class C, class = typename C::value_type>
typename enable_if<!is_same<C, string>::value, ostream&>::type 
operator<<(ostream& os, C c) {
	for (auto i = c.begin(); i != c.end(); i++) {
		os << " {"[i == c.begin()] << *i << ",}"[next(i) == c.end()];
	}
	return os;
}

#define debug(x) { cerr << #x << " = " << x << endl; }
#else
#define debug(...) {}
#endif

const int N = 1000 * 1000;

long long dp[N];
int dod[N];
int counter[N];
long long tab[N];

int cyf(long long x) {
	int res = 0;
	while (x > 0) {
		res ++;
		x /= 10;
	}
	return res;
}

vector<int> split(long long x) {	
	vector<int> res;
	while (res.empty() || x > 0) {
		res.push_back(x % 10);
		x /= 10;
	}
	reverse(res.begin(), res.end());
	return res;
}

bool check(int pos, long long t, int td, long long prev, int d, int cnt) {
	if (cyf(t) + td < cyf(prev) + d) return false;
	if (cyf(t) + td > cyf(prev) + d) {
//		debug("Longer var\n");
		dp[pos] = t;
		dod[pos] = td;
		counter[pos] = 0;
		return true;
	}
	int x = cyf(t);
	int y = cyf(prev);
	vector<int> cnt_split = split(cnt + 1);
	long long prev_cnt = prev;
	if (cnt_split.size() > d) prev_cnt ++;
	vector<int> t_split = split(t);
	vector<int> prev_split = split(prev_cnt);

	if (prev_split.size() + d > x + td) return false;

	while (t_split.size() > prev_split.size()) {
		assert(d >= 1);
		if (d > cnt_split.size()) {
			prev_split.push_back(0);
		} else {
			prev_split.push_back(cnt_split[cnt_split.size() - d]);
		}
		d --;
	}
//	debug(prev_split);
//	debug(t_split);
	for (int i = 0; i < t_split.size(); i ++) {
		if (t_split[i] < prev_split[i]) return false;
		if (t_split[i] > prev_split[i]) {
//			debug("Bigger in t range\n");
			dp[pos] = t;
			dod[pos] = td;
			counter[pos] = 0;
			return true;
		}
	}

//	debug("Equal\n");
	dp[pos] = prev_cnt;
	dod[pos] = dod[pos - 1];
	counter[pos] = (prev_cnt == prev ? cnt + 1 : 0);
	return true;
}

int main() {
	long long res = 0;
	ios_base::sync_with_stdio(false);
	int n;
	cin >> n;
	for (int i = 0; i < n; i ++) {
		cin >> tab[i];
	}
	dp[0] = tab[0];
	for (int i = 1; i < n; i ++) {
		for (int j = max(dod[i-1] - cyf(tab[i]), 0); ; j ++) {
			if (check(i, tab[i], j, dp[i - 1], dod[i - 1], counter[i - 1])) {
				res += j;
//				cerr << tab[i] << "\t" << dp[i] << "\t" << dod[i] << "\n";
				break;
			}
		}
	}
	cout << res << "\n";
}