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

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

	int n;
	cin>>n;
    vector <int> v;
    for(int i = 0; i < n; i++){
        int a; 
        cin >> a;
        v.push_back(a);
    }

    if(n == 1){
        cout << v[0];
        return 0;
    }

    int k = 0, p = v.size() - 1;
    int s = 0, t = 1;
    while(p >= 0 && v[p] > k){
        while(p != 0 && v[p-1] == v[p]){
            t++;
            p--; 
        }
        if(p == 0 && v[p] == v[p+1]){
            t++;
        }
        s += (v[p]-k) * t;
        p--;
        k+=t;
        t = 1;
    }
    cout << s;
}
