#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#include<stack>
int main() {

	int n;
	std::cin >> n;
	std::vector<int> v;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		std::cin >> v[i];
	}
	unsigned long long sum = 0;
	int count = 0;
	int left = 0, right = n -1;
	while (left <= right)
	{ 
		int num = v[right];

		for (int i = right; i >= left; i--)
		{
			if (v[i] == num)
			{
				sum += v[i];
				count++;
			}
			else
			{
				v[i] -= count;
				if (v[i] <= 0)
				{
					left = i + 1;
					break;
				}
			}
		}
		right -= count;
	}
	std::cout << sum;


}