#include <bits/stdc++.h>
using namespace std;
int A[5010], ans = 0;
int fen(int L, int R) {
  int wz, a = 1000000002;
  int pd = 1, b = 0;
  for (int i = L; i <= R; i++) {
    if (A[i] != 0) pd = 0;
  }
  if (pd) return 0;
  if (L == R) {
    return 1;
  }
  for (int i = L; i <= R; i++) {
    if (a > A[i]) {
      a = A[i];
      wz = i;
    }
  }
  for (int i = L; i <= R; i++) {
    A[i] -= a;
  }
  if (wz == L) {
    return min((R - L + 1), fen(L + 1, R) + a);
  } else if (wz == L) {
    return min(fen(L, R - 1) + a, (R - L + 1));
  } else {
    return min(fen(L, wz - 1) + fen(wz + 1, R) + a, (R - L + 1));
  }
}
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
  cout << fen(1, n);
}
int main() {
  solve();
  return 0;
}
