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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <bits/stdc++.h>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ff first
#define dd second
#define mp make_pair
typedef long long lld;
#define pb emplace_back
#define sz size()
#define For(i,s,a) for(lld i=(lld)s;i<(lld)a;++i)
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
#define rpt(S,it) for(auto it=S.begin();it!=S.end();++it)
#define mod (lld)(1e9+7)
#define scanf(...) scanf(__VA_ARGS__)?:0
#define P first
#define S second
#ifdef __WIN32__
#define gcx getchar
#elif __linux__
#define gcx getchar_unlocked
#endif
#define piii pair<pii,pii>
//template<typename T>
#define T lld
inline void scan(T *i)
{
	register T t=0;
	register char z='a';
	register bool neg=0;
	while(z<'0' || z>'9'){if(z=='-')neg^=1;
	z=gcx();}
	while(z>='0' && z<='9')
	{
		t=(t<<3ll)+(t<<1ll)+z-'0';
		z=gcx();
	}
	if(neg)t=~(t-1);
	*i=t;
}

lld l10(lld x)
{
	if(x<0)return 0;
	if(!x)return 1;
	lld ret=0;
	while(x)++ret,x/=10;
	return ret;
}

bool all9(lld x,lld w)
{
	if(x==-1)return 1;
	if(!x)return 0;
	while(w--){
		if(x%10!=9)return 0;x/=10;
	}
	return 1;
}

lld p10[17];
#define gran (lld)(9999999999999999)
lld glob;

lld truncc(lld x)
{
	lld z=0,ret=0,info=l10(x);
	if(!x)return 1;
	while(x%10==0 && info>9)++z,x/=10,--info;
	while(z>6)--z,++glob,++ret;
	return p10[ret];
}

bool prefok(lld x, lld y)
{
	while(l10(x)>l10(y))x/=10;
	if(x==y)return 1;
	return 0;
	
}

const int debug1=0,debug2=0;
#define D1 if(debug1)
#define D2 if(debug2)

int main()
{
	glob=0;
	p10[0]=1;
	For(i,1,17)p10[i]=p10[i-1]*10ll;
	lld a,pop=0;
	lld t;
	lld len=0,popt=0,count=0,ctlen=0;
	lld wyn=0;
	scan(&t);
//	t=200000;
	while(t--)
	{
		scan(&a);
//		a=1000000000;
	//	cout<<a<<endl;
		if(glob<3 && a==pop*p10[glob])
		{
	D1		cout<<"ROWNE"<<" "<<count<<endl;
			if(all9(count,l10(count)))
			count=-1,++ctlen;
			++count;
			ctlen=l10(count);
			wyn+=ctlen+glob;
			popt=a;
			len=ctlen+l10(a);
			pop=a*p10[ctlen];
			pop/=truncc(pop);
		}
		else
		if(glob<3 && a>pop*p10[glob])
		{
	D1		cout<<"EASY"<<endl;
			count=-1;
			wyn+=glob;
			popt=pop=a;
			ctlen=0;
			len=l10(a);
		}
		else
		{
	D1			cout<<"MALYPRZYPS"<<endl;
				lld tp=popt,ta=a,tta,ttp;
				lld tcut=0;
				while(ta>pop && ta%10==0 && pop%10==0 && tcut<glob)ta/=10,++tcut;
				tta=ttp=0;
				lld tmp=l10(tp)-l10(ta);
				lld tmp2=l10(pop)-l10(tp);
				bool nier=prefok(tp,ta) && l10(tp)>l10(ta);
				bool big=1;
				while(l10(tp)>l10(ta))ta*=10ll,ta+=(nier?(tp/p10[max(0ll,tmp-1)]%10ll):0),++tta,--tmp;
				while(l10(tp)<l10(ta))tp*=10ll,tp+=(tmp2>0?pop/p10[tmp2-1]%10ll:0),++ttp;
				if(ta%10!=9 && nier &&big)++ta;
	D1			cout<<ta<<" "<<tp<<" "<<pop<<" "<<popt<<endl;
				if(ta>tp)
				{
	D1				cout<<ttp<<endl;
	D1				cout<<"JEDNAK EZ"<<endl;
	///					while(ttp>0 && glob>0)--glob,--ttp;
				//	tta-=min(0ll,-len+l10(ta));
					wyn+=tta+glob+max(0ll,len-l10(ta));
					ta=ta*p10[max(0ll,len-l10(ta))];
					pop=ta;
					popt=a;
					count=-1;
					ctlen=0;
					len=l10(ta);
				}
				else
				
				if(ta==tp)
				{
	D1				cout<<"TAK SRENDIO xd"<<"   "<<count<<" "<<l10(pop)<<"   "<<ta<<endl;
					if(all9(count,ctlen))
					count=-1,++ctlen;
					++count;
	D1				cout<<l10(pop)-l10(ta)<<endl;
						lld res=pop;
						res%=max(1ll,p10[max(0ll,l10(pop)-l10(a))]);
						if(all9(res,max(0ll,l10(pop)-l10(a))))
						res=max(0ll,l10(pop)-l10(a))+1,big=0;
						else
						res=l10(pop)-l10(a),big=0;
	D1					cout<<big<<" "<<res<<" "<<ttp<<endl;
					wyn+=res+glob/*-(a>pop)?ttp:0)*/;
					if(res<0)glob+=res,res=0;
					//len=l10(a)+tta+ctlen;
					while(l10(ta)<res+l10(a))ta*=10;
	D1				cout<<ta<<"   ";
					while(tcut)a/=10ll,--tcut;
	D1				cout<<"XD"<<" "<<ta<<endl;
					ta/=truncc(ta);
					a=min(a,ta);
					ta+=count+big;
	D1				cout<<ta<<" "<<a<<endl;
					pop=ta;
					popt=a;
				}
				else
				{
	D1				cout<<"O"<<endl;
					tta+=max(0ll,len-l10(ta));
					ta*=p10[max(0ll,len-l10(ta))];
					if(ta<pop){
					++tta;
					ta*=10ll;}
					wyn+=tta+glob;
					ta/=truncc(ta);
					len=l10(ta);
					ta=min(gran,ta);
					pop=ta;
					popt=min(a,ta);
					count=-1;
					ctlen=0;
				}
		}
	D2	cout<<wyn<<" aaaaaaaa "<<pop<<" "<<popt<<" "<<len<<" "<<glob<<endl<<endl;
	}
	printf("%lld",wyn);
}