	#include <algorithm>
	#include <iostream>
	#include <cstdlib>
	#include <cstdarg>
	#include <cassert>
	#include <climits>
	#include <cstring>
	#include <complex>
	#include <cstdio>
	#include <vector>
	#include <string>
	#include <queue>
	#include <cmath>
	#include <ctime>
	#include <set>
	#include <map>
	#include <iomanip>
	#include <unordered_map>
	#include <bitset>
	#include<stack>
	#include <type_traits>
	#include <random>
	#include <chrono>
	#define int long long
	using namespace std;
	long long a, b, c, d, e, f, g;
	long long h[1000005];
	void solve()
	{
		cin >> a;
		for (int i = 1; i <= a; i++)
		{
			cin >> h[i];
		}
		g = 0;
		int g2 = 0;
		e = 0;
		for (int i = a; i >= 1; i--)
		{
			if (e != h[i]) {
				g2 += g; e = h[i];
			}
		//	cout << g2 << " " << e << " " << h[i] << '\n';
			if (h[i] - g2 <= 0) continue;
			f += h[i] - g2;
			g++;
		}
		cout << f << '\n';
	}
	signed main() {
		int q = 1;
		//cin >> q;
		while (q != 0)
		{
			solve();
			q--;
		}
	}
