#include <iostream>
using namespace std;

int main()
{
	int n,w;
	int a[20000];
	cin >> n;
	int i;
	for (i = 1; i <= n; i++)
		cin >> a[i];
	i = n;
	int rez = 0, j;
	bool x=false;
	while (x==false&&i>1)
	{
		rez = rez + a[i];
		w = a[i];
		a[i] = 0;
		for (j = i-1; j >= 1; j--)
			if (a[j]!=w) a[j]--;
		i--;
		x=true;
		for (int k=1;k<=n;k++)if (a[k]!=0)x=false;
	}
	cout << rez;
}
