#include<bits/stdc++.h>
#define MAXN 2000000
using namespace std;
int a[MAXN], n;
int f(int x, int y){
    //cout << x << " " << y << endl;
    if(y < 0 or x > n or x == y or x > y){
        return 0;
    }
    int maxch = -1 , ind = -1;
    for(int i = x; i <= y; i++){
        if(maxch < a[i]){
            maxch = a[i];
            ind = i;
        }
    }
    return (y - x) + f(x, ind - 1) + f(ind + 1, y);
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    cout << f(0, n - 1) << "\n";
    return 0;
}
