#include <bits/stdc++.h>
using namespace std;
int dp[5009][5009], a[5009];
int query(int l, int r) {
  if (l == r) {
    return 1;
  }
  int &x = dp[l][r];
  if (x != -1) {
    return x;
  }
  x = r - l + 1;
  int mini = 100000001;
  int i = 0, j = 0;
  for (int k = l; k <= r; k++) {
    mini = min(a[k], mini);
  }
  int y = mini;
  for (int k = l; k <= r; k++) {
    a[k] -= mini;
    if (a[k] > 0) {
      if (a[i] > 0) {
        j = k;
      } else {
        i = j = k;
      }
    } else {
      if (a[i] > 0) {
        y += query(i, j);
      }
      i = j = k;
    }
  }
  if (a[i] > 0) {
    y += query(i, j);
  }
  x = min(x, y);
  return x;
}
int main() {
  int i, j, n, m, x, y, px, py, k;
  long long int z;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  memset(dp, -1, sizeof(dp[0][0]) * 5009 * 5009);
  x = query(1, n);
  cout << x << endl;
  ;
}
