#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <numeric>
#include <map>
#include <cassert>
#include <algorithm>

using namespace std;

#define int long long

signed main()
{
	int n;
	cin >> n;
	vector<int> a(n);
	for (auto& i : a) cin >> i;
	int ans = 0;
	int times = 1, gtimes = 0;
	int val = a[n - 1];
	for (int i = 1; i < n; ++i)
	{
		if (a[n - i - 1] == a[n - i])
		{
			++times;
		}
		else
		{
			gtimes += times;
			ans += val * times;
			val = max(a[n - i - 1] - gtimes, 0ll);
			//cout << ans << " " << gtimes << " " << times << '\n';
			times = 1;
			//cout << ans << " " << gtimes << '\n';
		}
		
	}
	gtimes += times;
	ans += val * times;
	cout << ans << '\n';
	return 0;
}
