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
/** Potyczki Algorytmiczne 2018 - 2 B - Nowy Kontrakt */

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>


using namespace std;


const int MaxLen = 10000000;

int numLines, orgLen1, orgLen2, finLen1, finLen2;
unsigned long long newDigits = 0;
char buff1[MaxLen], buff2[MaxLen], *ptr1, *ptr2;


unsigned long long majorize() {
	unsigned long long result = 0;

	if(orgLen2>finLen1) {
		return 0;
	}

	if(orgLen2<finLen1) {
		for(int i=orgLen2;i<finLen1;++i)
			ptr2[i] = '0';

		ptr2[finLen1] = '\0';
		finLen2 = finLen1;
		result += finLen2-orgLen2;
	}

	int pos = 0;
	for(;ptr1[pos]==ptr2[pos] && pos<finLen2;++pos);

	if(pos==finLen2) {
		if(orgLen2==finLen2) {
			ptr2[finLen2++] = '0';
			ptr2[finLen2] = '\0';

			return result+1;
		}

		ptr2[finLen2-1] = '1';

		return result;
	}

	if(pos<orgLen2) {
		if(ptr2[pos]>ptr1[pos]) {
			return result;
		}
		
		ptr2[finLen2++] = '0';
		ptr2[finLen2] = '\0';

		return result+1;
	}

	if(ptr1[pos]<'9') {
		ptr2[pos] = ptr1[pos]+1;

		return result;
	}

	if(pos>orgLen2) {
		ptr2[pos-1] = '1';

		return result;
	}

	ptr2[finLen2++] = '0';
	ptr2[finLen2] = '\0';
	
	return result+1;
}


int main(void) {
	fgets(buff1,MaxLen,stdin);
	numLines = strtol(buff1,NULL,10);

	fgets(buff1,MaxLen,stdin);
	orgLen1 = finLen1 = strlen(buff1)-1;
	buff1[orgLen1] = '\0';
	ptr1 = buff1;
	ptr2 = buff2;

	for(int i=1;i<numLines;++i) {
		fgets(ptr2,MaxLen,stdin);
		finLen2 = orgLen2 = strlen(ptr2)-1;
		ptr2[orgLen2] = '\0';

		newDigits += majorize();

		swap(ptr1,ptr2);
		swap(orgLen1,orgLen2);
		swap(finLen1,finLen2);
	}

	printf("%llu\n",newDigits);

	return 0;
}