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
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
const int C=200001;
typedef long long ll;

int n, i, x, nz[C], val[C], rwr[C][11], k, cw[11], len[C];
ll tab[C], wynn=0;

int lenn(int a){
	int s=0;
	if (a==0)	return 1;
	for (;a>0;a/=10)	s++;
	return s;	
}

void rewrite(int p){
	int i=0, j, a=tab[p], k, l;
	
	for (;a>0;a/=10)	cw[i]=a%10, i++;
	if (i==0)	i=1;
	for (j=0;j<i;j++)	rwr[p][j]=cw[i-j-1];
	k=lenn(val[p]);
	
	l=j;
	for (;j-l<nz[p]-k&&j<11;j++)	rwr[p][j]=0;
	l=j, a=val[p], i=0;
	for (;a>0;a/=10)	cw[i]=a%10, i++;
	
	for (;j<11&&j-l<i;j++){
		rwr[p][j]=cw[i-j+l-1];
	}
}

int comp(int p1, int a){
	int i=0, j;
	for (;a>0;a/=10)	cw[i]=a%10, i++;
	for (j=0;j<i;j++){
		if (cw[i-j-1]>rwr[p1][j])	return 2;
		if (cw[i-j-1]<rwr[p1][j])	return 0;
	}
	return 1;
}

void add(int p, ll x){
	ll gg, lk;
	int j, i, z=len[p]-nz[p], c1;
	
	
	if (lenn(tab[p])>len[p]-nz[p]){
		
		if (nz[p]-lenn(tab[p])>0){
			if (lenn(val[p]+1)<nz[p]-lenn(tab[p])){
				val[p]++;
				return;
			}
		}
		
		
		gg=tab[p], lk=tab[p]+1;
		if (lenn(lk)!=lenn(gg))	{
			nz[p]++, val[p]=0, len[p]++, tab[p]=x;
			return;
		}
		c1=lenn(gg);
		for (;gg>0,lk>10;gg/=10,lk/=10)	{
			if (gg%10!=lk%10&&c1<=z){
				nz[p]++, val[p]=0, len[p]++, tab[p]=x;
				break;
			}
			c1--;	
		}
		tab[p]++;
	}
	
	else {
		val[p]++;
		if (lenn(val[p])>nz[p])	val[p]=0, nz[p]++;
	}
	
	
}


int main(){
	int j;
	ll pure;
	
	cin>>n;
	
	for (i=0;i<n;i++){
		cin>>tab[i];
		if (i==0)	{
			nz[i]=0, len[i]=lenn(tab[i]), val[i]=0;
			rewrite(i);	
			continue;
		}
		
		k=lenn(tab[i]);
		
		x=comp(i-1, tab[i]);
		if (x==0)		val[i]=0, nz[i]=max(len[i-1]-k+1, 0), len[i]=k+nz[i];
		else if (x==1)	{
			pure=tab[i], nz[i]=max(len[i-1]-lenn(tab[i]), 0), tab[i]=tab[i-1], val[i]=val[i-1], len[i]=len[i-1];
			add(i, pure);
		}
		else 	val[i]=0, nz[i]=max(len[i-1]-k, 0), len[i]=k+nz[i];
		//printf ("%d %d %d\n", nz[i], len[i], val[i]);
		rewrite(i);
		
		//for (j=0;j<11;j++)	printf ("%d", rwr[i][j]);
		//printf ("\n");
		
		wynn+=nz[i];
	}
	cout<<wynn;
	
return 0;}