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
#include <iostream>
using namespace std;

//#define _DEBUG 1
#define _DEBUG2 1
#ifdef _DEBUG
	#define CERR(x) cerr<<x
	#ifdef _DEBUG2
	#define CERR2(x) cerr<<x
	#else
	#define CERR2(x)
	#endif
#else
#define CERR(x)
#define CERR2(x)
#endif

int n;

inline bool numericalLower(const string& a, const string& b) {
	return a.length()==b.length() ? a<b : a.length()<b.length();
}
string inline inc(const string& str, int beg, int end) {
	int pos = str.find_last_not_of('9');
	if(pos==string::npos || pos<beg) {
		return string(end-beg+1, '0');
	}
	char c = str[pos];
	return str.substr(beg, pos-beg)+string(1, (c+1))+string(end-pos-1, '0');
}

const int BOUND=20;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	long long res=0;
	long long prev=0LL,cur;
	string curs, prevs, add;
	int prevLength=BOUND+1;
	long long prevres=0;
	for(int x=0;x<n;++x) {
		cin>>curs;
		if(prevs.length()>BOUND) {
			int cmp = prevs.compare(0, curs.length(), curs);
			if(cmp==0) {
					CERR2("a "<<cmp<<endl);
					res += prevLength-curs.length();
			}
			else if(cmp<0) {
					CERR2("b "<<cmp<<endl);
					res += prevLength-curs.length();
					prevs = curs+string(BOUND+1-curs.length(), '0');
			}
			else /*if(cmp>0)*/ {
				CERR2("c "<<cmp<<endl);
				prevs = curs+string(BOUND+1-curs.length(), '0');
				++prevLength;
				res += prevLength-curs.length();
			}
			CERR(prevs<<"e+"<<prevLength<<" - +"<<(prevLength-curs.length())<<" = " << res <<endl);
			continue;
		}
		if(numericalLower(prevs, curs)) {
				CERR2("a"<<endl);
		}
		else {
			int cmp = prevs.compare(0, curs.length(), curs);
			if(cmp==0) {
					CERR2("b "<<cmp<<endl);
				if(curs.length()==prevs.length()) {
					curs+="0";
					++res;
				}
				else {
					add = inc(prevs, curs.length(), prevs.length());
					res += add.length();
					curs += add;
				}
			}
			else if(cmp>0) {
				CERR2("c "<<cmp<<endl);
				add = string(prevs.length()-curs.length()+1, '0');
				res += add.length();
				curs += add;
			}
			else /*if(cmp<0)*/ {
				CERR2("d "<<cmp<<endl);
				add = string(prevs.length()-curs.length(), '0');
				res += add.length();
				curs += add;
			}
		}
		CERR(curs << " + " << (res-prevres) << " = " << res << endl);
		prevres = res;
		prevs=curs;
	}
	cout<<res;
	return 0;
}