#include <bits/stdc++.h>
using namespace std;

int n, buf,cost = 0;
vector <int> balls; 

void grab(){
    buf = balls[balls.size()-1];
    cost += balls[balls.size()-1];
    balls.erase(balls.end()-1);
    for (int i = 0; i < balls.size(); i++) {
        if (balls[i] != buf) balls[i]-=1;
        if (balls[i] == 0) {   
            balls.erase(i+balls.begin());
            i--;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	
	cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> buf;
        balls.push_back(buf);
    }
    while (balls.size() > 0) grab();
    cout << cost;
}
