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
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstring>

class Number {
private:
	int prefix;
	long long int counter;
	int suffixSize;

	long long int getCounterLimit() const {
		long long int counterLimit = 1;
		for (int i = 0; i < suffixSize && i < 16; ++i)
			counterLimit *= 10;
		return counterLimit;
	}

	int getDigitsCount(long long int number) const {
		int digits = 1;
		long long int num = number / 10;
		while (num != 0) {
			digits++;
			num /= 10;
		}
		return digits;
	}

	void incrementCounter() {
		counter++;
		if (counter == getCounterLimit()) {
			counter = 0;
			suffixSize++;
		}
	}

	void preparePrefix(char* buf, int lenA) {
		int lenN = sprintf(buf, "%d", prefix);
		if (lenN < lenA && suffixSize > 0) {
			int i;
			int zeroes = suffixSize - getDigitsCount(counter);
			for (i = lenN; i < lenA && zeroes > 0; ++i, --zeroes) {
				buf[i] = '0';
			}
			buf[i] = '\0';

			if (i < lenA) {
				sprintf(buf + i, "%lld", counter);
			}
		}
	}
public:
	Number() {
		prefix = 0;
		counter = 0;
		suffixSize = 0;
	}

	int update(int a) {
		char strA[12];
		char strN[30];
		int prefixSize = getDigitsCount(prefix);
		int lenA = sprintf(strA, "%d", a);
		int added = 0;

		if (prefixSize + suffixSize < lenA) {
			prefix = a;
			counter = 0;
			suffixSize = 0;
		} else {
			preparePrefix(strN, lenA);
			int cmpResult = strncmp(strA, strN, lenA);
			if (cmpResult == 0) {
				if (prefixSize < lenA) {
					prefix = a;
					suffixSize -= (lenA - prefixSize);
					if (suffixSize > 0) {
						counter %= getCounterLimit();
					}
					else {
						counter = 0;
					}
				}
				else if (prefixSize > lenA) {
					if (suffixSize + prefixSize < 16) {
						long long int total = prefix;
						for (int i = 0; i < suffixSize; ++i) {
							total *= 10;
						}
						total += counter;

						sprintf(strN, "%lld", total);
						sscanf(strN + lenA, "%lld", &counter);
						prefix = a;
						suffixSize += prefixSize - lenA;
					}
				}

				incrementCounter();
			}
			else if (cmpResult < 0) {
				suffixSize = (suffixSize + prefixSize + 1) - lenA;
				prefix = a;
				counter = 0;
			}
			else {
				suffixSize = (suffixSize + prefixSize) - lenA;
				prefix = a;
				counter = 0;
			}
			added = suffixSize;
		}
		return added;
	}
};

int main()
{
	int n, a;
	scanf("%d", &n);

	long long int result = 0;
	Number lastPrice;
	while (n--) {
		scanf("%d", &a);
		result += lastPrice.update(a);
	}
	
	printf("%lld\n", result);
	return 0;
}