#include<bits/stdc++.h>
#define int long long
#define endl "\n";

using namespace std;

int rev(int numb) {
	int n = 0;
	while(numb != 0) {
		n = n * 10 + (numb%10);
		numb/=10;
	}
	return n;
}

signed main() {
	
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	int n;
	cin >> n;
	int cost = 0;
	int buys = 0;
	string str = "";

	for(int i = 0; i < n; i++) {
		int temp;
		cin >> temp;
		str += to_string(temp);
		str += " ";
	}

	int pr_numb = 0;
	int buys1 = 0;

	for(int i = str.length() - 1; i >= 0; i--) {
		if(str[i] == ' ') continue;
 		int numb = str[i]-'0';
		 while(i > 0 && str[i-1] != ' ') {
			 i--;
			 numb = numb *10 + str[i] - '0';
		 }
		 numb = rev(numb);
		 
			if(pr_numb != numb) {
				buys1 = buys;
			}
			if(numb - buys1 <= 0) break;
			cost += numb - buys1;
			buys++;
			pr_numb = numb;
		
	}
 	
	cout << cost;
	
	return 0;
}