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
#include<map>
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
	long long int N,i,cos,mod,mod2,num,dl,dl2,zapdl,zapdl2,wynik=0;
	cin >> N;
	mod = 1;
	mod2 = 1;
	cos = 1;
	map<int, long long> tab;
	//long long int *tab = new long long int[200000];
	for (i = 0; i < N; i++)
		cin >> tab[i];
	for (i = 1; i < N;i++)
	{
		if (tab[i] < tab[i - 1])
		{
			if (tab[i] * 10 > tab[i - 1])
			{
				tab[i] = tab[i] * 10;
				wynik++;
			}
			else
			{
				cos = 1;
				if (tab[i-1] <= 9)
					dl = 1;
				else if (tab[i-1] <= 99)
					dl = 2;
				else if (tab[i-1] <= 999)
					dl = 3;
				else if (tab[i-1] <= 9999)
					dl = 4;
				else if (tab[i-1] <= 99999)
					dl = 5;
				else if (tab[i-1] <= 999999)
					dl = 6;
				else if (tab[i-1] <= 9999999)
					dl = 7;
				else if (tab[i-1] <= 99999999)
					dl = 8;
				else if (tab[i-1] <= 999999999)
					dl = 9;
				else if (tab[i-1] <= 9999999999)
					dl = 10;
				else if (tab[i - 1] <= 99999999999)
					dl = 11;
				else if (tab[i - 1] <= 999999999999)
					dl = 12;
				else if (tab[i - 1] <= 9999999999999)
					dl = 13;
				else if (tab[i - 1] <= 99999999999999)
					dl = 14;
				else if (tab[i - 1] <= 999999999999999)
					dl = 15;
				else if (tab[i - 1] <= 9999999999999999)
					dl = 16;
				else if (tab[i - 1] <= 99999999999999999)
					dl = 17;
				else if (tab[i - 1] <= 999999999999999999)
					dl = 18;
				else if (tab[i - 1] <= 9999999999999999999)
					dl = 19;
				else if (tab[i - 1] <= 9223372036854775808)
					dl = 20;
				if (tab[i] <= 9)
					dl2 = 1;
				else if (tab[i] <= 99)
					dl2 = 2;
				else if (tab[i] <= 999)
					dl2 = 3;
				else if (tab[i] <= 9999)
					dl2 = 4;
				else if (tab[i] <= 99999)
					dl2 = 5;
				else if (tab[i] <= 999999)
					dl2 = 6;
				else if (tab[i] <= 9999999)
					dl2 = 7;
				else if (tab[i] <= 99999999)
					dl2 = 8;
				else if (tab[i] <= 999999999)
					dl2 = 9;
				else if (tab[i] <= 9999999999)
					dl2 = 10;
				else if (tab[i] <= 99999999999)
					dl = 11;
				else if (tab[i] <= 999999999999)
					dl = 12;
				else if (tab[i] <= 9999999999999)
					dl = 13;
				else if (tab[i] <= 99999999999999)
					dl = 14;
				else if (tab[i] <= 999999999999999)
					dl = 15;
				else if (tab[i] <= 9999999999999999)
					dl = 16;
				else if (tab[i] <= 99999999999999999)
					dl = 17;
				else if (tab[i] <= 999999999999999999)
					dl = 18;
				else if (tab[i] <= 9999999999999999999)
					dl = 19;
				else if (tab[i] <= 9223372036854775808)
					dl = 20;
				
				zapdl = dl;
				zapdl2 = dl2;
				dl--;
				dl2--;
				//if (tab[i - 1] / pow(10, dl - 1) == tab[i] / pow(10, dl2 - 1))
				//{
				while (cos == 1)
				{
					mod2 = pow(10, dl2)/10;
					mod = pow(10, dl)/10;	
					if (mod != 0)
						mod = tab[i-1] / mod;
					if (mod2 != 0)
						mod2 = tab[i] /mod2;
					
					if (mod == mod2) //sprawdzamy ile elementow wspolnych
					{
						dl--;
						dl2--;
					}					
					if(dl2==0)
					{
						wynik = wynik + dl;
						tab[i] = tab[i - 1] + 1;
						cos = 0;
					}
					if(mod!=mod2 && dl2!=0)
					{
						if (tab[i] * pow(10, zapdl - zapdl2) > tab[i - 1])
						{
							tab[i] = tab[i] * pow(10, zapdl - zapdl2); //sprawdzamy czy wystarczy pomnozyc przez ta liczbe elementow
							cos = 0;
							wynik=wynik+ zapdl - zapdl2;
						}
						else
						{
							tab[i] = tab[i] * pow(10, zapdl - zapdl2 + 1); //jezeli nie to mnozymy o 10razy wiecej
							cos = 0;
							wynik = wynik + zapdl - zapdl2 + 1;
						}
					}
				}
				
			}
		}
	}
	cout << wynik;
}