#include <iostream>
using namespace std;

int main()
{
	long long n,w;
	long long a[20000];
	cin >> n;
	long long i;
	for (i = 1; i <= n; i++)
		cin >> a[i];
	i = n;
	long long 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 (long long k=1;k<=n;k++)if (a[k]!=0)x=false;
	}
	cout << rez;
}
