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
#include<iostream>
#include<cmath>
using namespace std;
unsigned long long tab[1000003];
int main ()
{
	unsigned long long n, l=0;
	unsigned long long c1;
	unsigned long long c2;
	unsigned long long r;
	cin>>n;
	for (unsigned long long i=0;i<n;i++)
	{
		cin>>tab[i];		
	}
	for (unsigned long long i=0;i<n-1;i++)
	{
		if (tab[i+1]<=tab[i])
		{
			
			c1=(unsigned long long)log10(tab[i])+1;
			c2=(unsigned long long)log10(tab[i+1])+1;
			if ((unsigned long long)(tab[i]/(pow(10,c1)))<(unsigned long long)(tab[i+1]/(pow(10,c2))))//1234 i 20 --->  1234 2000   dopisano 2 cyfry
			{
				
				tab[i+1]=tab[i+1]*pow(10,c1-c2);
				l=l+(c1-c2);
			}
			else
			{
				if ((unsigned long long)(tab[i]/(pow(10,c1)))>(unsigned long long)(tab[i+1]/(pow(10,c2))))//3234 i 14 ----> 3234 14000 dopisano 3 cyfry
				{
				
					tab[i+1]=tab[i+1]*pow(10,c1-c2+1);
					l=l+(c1-c2+1);
				}
				else//pierwsze cyfry sa rowne
				{
					 r=tab[i+1]*pow(10,c1-c2);// 632 6 ---> 632 r=600 
					 if (r>tab[i])//5090 i 51 ---> 5090 5100
					 {
					 	
					 	tab[i+1]=r;
					 	l=l+(c1-c2);
					 }
					 else
					 {
					 	if (r==tab[i])//9700 i 97 ---> 9700 i r=9700
					 	{
					 		if (tab[i]==tab[i+1])
							{
							
						 		tab[i+1]=tab[i+1]*10;
								l=l+(c1-c2)+1;	
							}
							else
							{
								
								tab[i+1]=r+1;
								l=l+(c1-c2);
							}	
						}
					 	else
					 	{
					 		if (tab[i]-r<pow(10,c1-c2)-1)//3421 i 34 ---> 3421 i 3400   tab[i]-r =21    pow (10,c1-c2)-1=99
					 		{
					 			
					 			tab[i+1]=tab[i]+1;
					 			l=l+(c1-c2);
							}
							else//5699 i 56 ---> 5699 5600 tab[i]-r=99 pow...=99 wiec *10
							{
								
								tab[i+1]=r*10;
								l=l+(c1-c2)+1;	
							}
					 	}
					 
					}
				}
			}
		}	
	}
	cout<<l<<endl;


}