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
// Grzegorz Bukowiec
// Potyczki Algorytmiczne 2018
// Zadanie 2B - Honorarium

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

struct Cash {
	string number;
	unsigned length;
	
	Cash(int cash) {
		number = to_string(cash);
		length = number.size();
	}
	
	bool operator<(const Cash& X) {
		if (length == X.length) {
			for (unsigned i = 0; i < length; ++i) {
				if (number[i] != X.number[i]) {
					return number[i] < X.number[i];
				}
			}
			return false;
		}
        return length < X.length;
    }
    
    Cash& operator=(Cash X) {
		number = X.number;
		length = X.length;
		return *this;
    }
	
	long long fill(Cash &preq) {
		if (preq < *this) {
			return 0;
		}
		
		long long result = preq.length - length;
		bool justFill = false;
		bool fillMore = false;
		for (unsigned i = 0; i < length; ++i) {
			if (preq.number[i] < number[i]) {
				justFill = true;
				break;
			}
			if (preq.number[i] > number[i]) {
				fillMore = true;
				break;
			}
		}
		
		if (!justFill && !fillMore) {
			fillMore = true;
			for (unsigned i = preq.number.size() - 1; i >= length; --i) {  //cheating
				if (preq.number[i] != '9') {
					fillMore = false;
					break;
				}
			}
		}
		
		if (justFill || fillMore) {
			number.append(min(20u, preq.length) - length, '0'); //cheating
			length = preq.length;
			if (fillMore) {
				if (length < 20) number += '0'; //cheating
				++length;
				++result;
			}
			return result;
		} else {
			number = preq.number;
			length = preq.length;
			for (int i = number.size() - 1; ; --i) { //cheating
				if (number[i] == '9') {
					number[i] = '0';
				} else {
					++number[i];
					break;
				}
			}
			return result;
		}
	}
};

int main() {
	int n, a;
	long long result = 0;
	Cash preq(0);
	
	cin >> n;
	while (n--) {
		cin >> a;
		Cash seq(a);		
		result += seq.fill(preq);
		preq = seq;
	}
	
	cout << result;
	
	return 0;
}