#include <bits/stdc++.h>
using namespace std;
const int MXN = 5010;
int N, arr[MXN], dp[MXN];
int solve(int l, int r, int x) {
  if (l > r) return 0;
  int mn = arr[l];
  int mind = l;
  for (int i = l + 1; i <= r; i++) {
    if (arr[i] < mn) {
      mn = arr[i];
      mind = i;
    }
  }
  int apply = mn - x;
  int ans =
      min(r - l + 1, apply + solve(l, mind - 1, mn) + solve(mind + 1, r, mn));
  return ans;
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> arr[i];
  cout << solve(0, N - 1, 0);
}
