#include <iostream>
#include <vector>
#include <stack>
using namespace std;

typedef long long ll;
const int MAXN = 200010;

int n;
ll a[MAXN];
int lc[MAXN], rc[MAXN];

struct State {
    int u;
    ll base;
    int state;
    ll min_val;
    ll new_base;
    ll temp_ans;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    vector<int> stk;
    int root = -1;
    for (int i = 0; i < n; i++) {
        lc[i] = rc[i] = -1;
    }

    for (int i = 0; i < n; i++) {
        int last = -1;
        while (!stk.empty() && a[stk.back()] > a[i]) {
            last = stk.back();
            stk.pop_back();
        }
        if (stk.empty()) {
            root = i;
        } else {
            rc[stk.back()] = i;
        }
        lc[i] = last;
        stk.push_back(i);
    }

    stack<State> states;
    states.push({root, 0, 0, 0, 0, 0});
    ll ans = 0;

    while (!states.empty()) {
        State cur = states.top();
        states.pop();

        if (cur.state == 0) {
            cur.min_val = a[cur.u] - cur.base;
            cur.new_base = cur.base;
            if (cur.min_val > 0) {
                cur.new_base += cur.min_val;
            }
            cur.state = 1;
            states.push(cur);

            if (lc[cur.u] != -1) {
                states.push({lc[cur.u], cur.new_base, 0, 0, 0, 0});
            }
        } else if (cur.state == 1) {
            cur.state = 2;
            states.push(cur);

            if (rc[cur.u] != -1) {
                states.push({rc[cur.u], cur.new_base, 0, 0, 0, 0});
            }
        } else {
            ll add = (cur.min_val > 0) ? 1 : 0;
            ll total_ans = add + cur.temp_ans;

            if (states.empty()) {
                ans = total_ans;
            } else {
                State& parent = states.top();
                parent.temp_ans += total_ans;
            }
        }
    }

    cout << ans << endl;
    return 0;
}