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
#include <iostream>
#include <cstdio>
#include <cmath>

#define MAX_LENGTH 20

using namespace std;

int min(int a, int b)
{
	if (a < b) return a; else return b;
}


int main()
{
	char current[MAX_LENGTH + 1], previous[MAX_LENGTH + 1];
	long long int result = 0;
	int n;
	int previousLength = 0, currentLength;

	cin >> n;

	scanf("%s", current);
	int i = 0;
	while (current[i] != 0)
		i++;
	currentLength = i;

	for (int o = 1; o < n; o++)
	{
		for (int i = 0; i <= MAX_LENGTH; i++)
			previous[i] = current[i];

		previousLength = currentLength;

		scanf("%s", current);

		i = 0;
		while (current[i] != 0)
			i++;
		currentLength = i;

		if (currentLength > previousLength)
		{
			//cout << "p1 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
			continue; //obecna liczba jest dluzsza od poprzedniej, czyli na pewno wieksza - nic nie dodajemy
		}

		if (currentLength == previousLength) //ta sama dlugosc, porownujemy cyfry
		{
			i = 0;
			while (current[i] == previous[i] && current[i] != 0)
				i++;
			if (current[i] == 0) //liczby dokladnie takie same, do current dodajemy na koniec 0
			{
				//cout << "p2 " << "previous: " << previous << " l:" << previousLength << "current: " << current << " l:" << currentLength << " result: " << result << endl;
				result++;
				current[currentLength] = '0';
				current[currentLength + 1] = 0;
				currentLength++;
				continue;
			}
			else //liczby tej samej dlugosci ale rozne
			{
				if (current[i] > previous[i]) //obecna liczba wieksza od poprzedniej, jest ok
				{
					//cout << "p3 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
					continue;
				}
				else //obecna liczba mniejsza od poprzedniej, dodajemy na koniec 0
				{
					//cout << "p4 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
					result++;
					current[currentLength] = '0';
					current[currentLength + 1] = 0;
					currentLength++;
					continue;
				}
			}

		}

		if (currentLength < previousLength) //ostatnia, najciekawsza sytuacja i najczestsza
		{
			i = 0;
			while (current[i] == previous[i] && current[i] != 0)
				i++;

			if (current[i] == 0) //current jest podciagiem previous - najtrudniejsza sytuacja
			{
				if (previousLength > MAX_LENGTH) //spokojnie, na pewno mozna stworzyc liczbe poprzez wyrownanie rozmiarow
				{
					//cout << "p5 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
					result += previousLength - currentLength;
					for (int j = currentLength; j < MAX_LENGTH; j++)
						current[j] = previous[j];
					current[MAX_LENGTH] = 0;
					currentLength = previousLength;
					continue;
				}
				else //tu zaczyna sie zabawa
				{
					//szukamy ostatniej cyfry niedziewiątki zanim osiagniemy current
					int j = previousLength - 1;
					while (previous[j] == '9' && j >= currentLength)
						j--;

					if (j < currentLength) //trzeba dodac jedna cyfre wiecej
					{
						//cout << "p6 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
						result += previousLength - currentLength + 1;
						for (int j = currentLength; j < min(MAX_LENGTH, previousLength + 1); j++)
							current[j] = '0';
						current[min(MAX_LENGTH, previousLength + 1)] = 0;
						currentLength = previousLength + 1;
						continue;
					}
					else //mozna zmienic ktoras z istniejacych cyfr (dokladnie ta z indeksem j)
					{
						//cout << "p7 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
						result += previousLength - currentLength;
						for (int k = currentLength; k < j; k++)
							current[k] = previous[k];
						current[j] = previous[j] + 1;
						for (int k = j + 1; k < previousLength; k++)
							current[k] = '0';
						current[previousLength] = 0;
						currentLength = previousLength;
						continue;
					}

				}
			}
			else //current nie jest podciagiem previous, liczby sie roznia na indeksie i
			{
				if (current[i] > previous[i]) //dokladamy tyle zer, zeby wyrownac dlugosc z previous
				{
					//cout << "p8 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
					result += previousLength - currentLength;
					for (int j = currentLength; j < min(MAX_LENGTH, previousLength); j++)
						current[j] = '0';
					current[min(MAX_LENGTH, previousLength)] = 0;
					currentLength = previousLength;
					continue;
				}
				else //current[i] < previous[i], dokladamy tyle zer zeby byc dluzszym od previous
				{
					//cout << "p9 " << "previous: " << previous << " l:" << previousLength << " current: " << current << " l:" << currentLength << " result: " << result << endl;
					result += previousLength - currentLength + 1;
					for (int j = currentLength; j < min(MAX_LENGTH, previousLength + 1); j++)
						current[j] = '0';
					current[min(MAX_LENGTH, previousLength + 1)] = 0;
					currentLength = previousLength + 1;
					continue;
				}
			}
		}
	}

	cout << result << endl;

	return 0;
}