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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

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

struct number {
	int val, mul, add;
	number(int a) { val = a; mul = add = 0; }
	long long pref() {
		long long res = val; int rmul = 0;
		while (rmul < mul && res < 1e17) {
			res *= 10; rmul++;
		}
		int radd = add;
		for (int i=rmul; i<mul && radd; i++) radd /= 10;
		res += radd;
		//printf("%d * 10^%d + %d -> %lld (%d %d)\n", val, mul, add, res, rmul, radd);
		return res;
	}
};
bool isgreater(number a, number b) { int la = len(a.val) + a.mul, lb = len(b.val) + b.mul; if (la!=lb) return la>lb; ll pa = a.pref(), pb = b.pref(); if (pa!=pb) return pa>pb; return a.add>b.add; }
bool isprefi(int x, long long y) { while (y > x) y/=10; return y==x; }
bool ispref(int x, number y) { return isprefi(x, y.pref()); }

string get_number(int prv, int le, int add, int pad=0) {
	string ret = to_string(prv);
	if (le>0) {
		ret += string(le-len(add), '0');
		ret += to_string(add);
	}
	while (SZ(ret) < pad) ret = " " + ret;
	return ret;
}

int cmp_pref(int prv, int le, int add, int snd) {
	string sprv = to_string(prv), ssnd = to_string(snd);
	if (SZ(sprv) + le < SZ(ssnd)) return -1;
	int l2 = max(0, le - len(add));
	if (SZ(sprv) + l2 < SZ(ssnd)) {
		sprv += string(l2, '0');
		if (le>0) sprv += to_string(add);
	} else if (SZ(sprv) < SZ(ssnd)) {
		sprv += string(SZ(ssnd)-SZ(sprv), '0');
	}
	FOR(i,SZ(ssnd)) if (sprv[i] != ssnd[i]) {
		if (sprv[i] < ssnd[i]) return -1;
		return 1;
	}
	return 0;
}

int n;
int a[200200];
int main() {
	scanf("%d", &n);
	FOR(i,n) scanf("%d", &a[i]);
	//int prv_pref = a[0], prv_len = 0, prv_add = 0;
	number prv(a[0]);
	long long res = 0;
	for (int i = 1; i < n; i++) {
		number cur(a[i]);
		cur.mul = max(0, len(prv.val) + prv.mul - len(a[i]));
		if (!isgreater(cur, prv)) {
			number tcur(cur); tcur.val++;
			number tprv(prv); tprv.add++;//printf("b\n");
			if (ispref(a[i], tprv) && isgreater(tcur, tprv)) {
				cur = prv;
				cur.add++;
				//printf("add\n");
			} else {
				cur.mul++;
			}
		}
		//printf("current number is %d * 10^%d + %d\n", cur.val, cur.mul, cur.add);
		//printf("current number is %s (%s)\n", get_number(cur.val, cur.mul, cur.add, 30).c_str(), get_number(a[i], 0, 0, 10).c_str());
		res += len(cur.val) + cur.mul - len(a[i]);
		prv = cur;
	}
	printf("%lld\n", res);
	return 0;
}