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

bool empty(vector <int> v) {
    if (v.size() == 0) 
        return true;
    return false;
}

int main() {
    ll n, res = 0;
    cin >> n;
    
    vector <ll> dist;
    dist.resize(n);
    for (int i = 0; i < n; i++) 
        cin >> dist[i];

    while (!empty(dist)) {
        for (int i = 0; i < dist.size(); i++) {
            if (dist[i] != dist[dist.size() - 1])
                dist[i]--;
            if (dist[i] == 0)
                dist.erase(dist.begin() + i);
        }
        res += dist[dist.size() - 1];
        dist.erase(dist.end() - 1);
    }
    cout << res;
}