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

int n, a, b, c, d, have[15];
int cnt, siz, zer=-1, nnine;
int pot[15];
long long odp;
vector<int> v;

void powers(){
	int x=1;
	for(int i=0; i<=9; i++){
		pot[i]=x;
		x*=10;
	}
}

void ini_v(int x){
	v.clear();
	d=-1;
	zer=-1;
	nnine=-1;
	bool ok=0;
	for(int i=9; i>=0; i--){
		if(x>=pot[i]){
			d=i+1;
			break;
		}
	}
	for(int i=d-1; i>=0; i--){
		c=x/pot[i];
		if(c!=9) nnine=d-1-i;
		v.push_back(c);
		x-=c*pot[i];
	}
	// cout<<"nn"<<nnine<<"\n";
	// for(int i=0; i<v.size(); i++) cout<<v[i]<<" ";
	// cout<<"\n";
}

bool fir_digits(int x){
	siz=0;
	for(int i=9; i>=0; i--){
		if(x>=pot[i]){
			siz=i+1;
			break;
		}
	}
	// cout<<"siz"<<siz<<" "<<"d"<<d<<"\n";
	for(int i=siz-1; i>=0; i--){
		have[siz-1-i]=x/pot[i];
		x-=have[siz-1-i]*pot[i];
	}
	if(siz>d) return 1;
	if(siz<d) return 0;
	for(int i=0; i<siz; i++){
		if(have[i]<v[i]) return 0;
		if(have[i]>v[i]) return 1;
	}
	return 0;
}

void fix(){
	int ok=0;
	for(int i=0; i<siz; i++){
		if(have[i]>v[i]){
			ok=1;
			break;
		}
		if(have[i]<v[i]){
			ok=-1;
			break;
		}
	}
	for(int i=0; i<siz; i++){
		v[i]=have[i];
	}
	if(ok!=0){
		int y=min(9, d-1);
		for(int i=siz; i<=y; i++) v[i]=0;
		if(ok==-1){ 
			v.push_back(0);
			d++;
			nnine=d-1;
		}
		zer=d-1;
		if(d>10) nnine=d-1;
		else{
			y=min(9, d-1);
			for(int i=y; i>=0; i--){
				if(v[i]!=9){
					nnine=i;
					break;
				}
			}
		}
	}
	if(ok==0){
		if(nnine>=siz){
			if(zer==nnine){
				zer--;
				v[nnine]=1;
			}
			else v[nnine]++;
			for(int i=nnine+1; i<d; i++) v[i]=0;
			if(v[d-1]==0) nnine=d-1;
			if(v[nnine]==9 && zer!=nnine){
				while(nnine>=0 && v[nnine]==9 && nnine!=zer) nnine--;
				if(nnine==0 && v[0]==9) nnine--;
			}
		}
		else{
			int y=min(9, d-1);
			for(int i=siz; i<=y; i++) v[i]=0;
			v.push_back(0);
			d++;
			zer=d-1;
			nnine=d-1;
		}
	}
	odp+=d-siz;
}

int main(){
	powers();
	scanf("%d", &n);
	scanf("%d", &a);
	ini_v(a);
	for(int i=2; i<=n; i++){
		scanf("%d", &a);
		if(fir_digits(a)){
			ini_v(a);
			// cout<<"tak\n";
		}
		else{
			// cout<<"nie\n";
			fix();
		}
		 /*for(int j=0; j<v.size(); j++) printf("%d", v[j]);
		 printf(" %d\n", d);*/
	}
	printf("%lld\n", odp);
}