#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 10;
int a[N];
int findAns(int l, int r, int lev) {
  if (r == l) return 0;
  int mn = a[l], ind = l;
  for (int i = l + 1; i < r; i++)
    if (a[i] < a[ind]) {
      ind = i;
      mn = a[i];
    }
  return min(mn - lev + findAns(l, ind, mn) + findAns(ind + 1, r, mn), r - l);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  cout << findAns(0, n, 0) << "\n";
  return 0;
}
