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
#include <iostream>
using namespace std;

#define toInt(x) (x-'0')
#define toChar(y) (y+'0')

inline bool isGreater(string &a, string &b) {
	if (a.size() > b.size()) {
		return true;
	} else if (b.size() > a.size()) {
		return false;
	} else {
		for (long long i = 0; i < a.size(); i++) {
			if (a[i] > b[i]) {
				return true;
			}
			if (b[i] > a[i]) {
				return false;
			}
		}
		return false;
	}
}

inline bool isGreaterToEndOfFirst(string &a, string &b) {
	for (long long i = 0; i < a.size(); i++) {
		if (a[i] > b[i]) {
			return true;
		}
		if (b[i] > a[i]) {
			return false;
		}
	}
	return false;
}

inline bool isSmaller(string &a, string &b) {
	for (long long i = 0; i < a.size(); i++) {
		if (a[i] < b[i]) {
			return true;
		}
		if (b[i] < a[i]) {
			return false;
		}
	}
	return false;
}

inline bool addOne(string &number, long long numberCoreSize) {
	int acc = 1;
	for (long long i = number.size() - 1; i >= numberCoreSize; i--) {
		int s = toInt(number[i]) + acc;
		if (s < 10) {
			number[i] = toChar(s);
			return true;
		} else {
			number[i] = toChar(s % 10);
			acc = s / 10;
		}
	}
	return false;
}

int main() {
	long long sum = 0;
	string previous;

	int N;
	cin >> N;

	cin >> previous;

	for (int n = 1; n < N; n++) {
		string current;
		cin >> current;

		int currentSize = current.size();
		int previousSize = previous.size();

		if (!isGreater(current, previous)) {
			if (currentSize == previousSize) {
				current.append("0");
				sum++;
			} else if(isGreaterToEndOfFirst(current, previous)) {
				sum += previousSize - currentSize;
				current.append(previousSize - currentSize, '0');
			} else {
				if (isSmaller(current, previous)) {
					sum += previousSize - currentSize + 1;
					current.append(previousSize - currentSize + 1, '0');
				} else {
					bool result = addOne(previous, currentSize);
					if (result) {
						sum += (previousSize - currentSize);
						current = previous;
					} else {
						sum += previousSize - currentSize + 1;
						current.append(previousSize - currentSize + 1, '0');
					}
				}
			}
		}
		previous = current;
	}
	cout << sum;
	return 0;
}