#include <bits/stdc++.h>

using namespace std;
typedef long long LL;

int main() {
    LL n;
    cin >> n;
    LL a[n], rez = 0, count = 0;
    for(int i = 0; i < n; ++i) cin >> a[i];
    vector<pair<LL, LL> > b;
    b.push_back({a[0], (LL)1});
    for(int i = 1; i < n; ++i) {
        if(a[i] == a[i - 1]) 
        {
            b[b.size() - 1].second++;
        }
        else
        {
            b.push_back({a[i], (LL)1});
        }
    }
    for(int i = b.size() - 1; i >= 0; --i) {
        if(b[i].first - count > 0) {
            rez += (b[i].first - count) * b[i].second;
            count += b[i].second;
        }
    }
    cout << rez;
   return 0;;
}