#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int n;
int val[5005];
int dfs(int ll, int r, int height) {
  if (ll > r) return 0;
  int min_value = 1e9 + 7;
  int i, pos, sum;
  sum = 0;
  for (i = ll; i <= r; i++) {
    if (val[i] < min_value) {
      min_value = val[i];
      pos = i;
    }
    if (val[i] > height) sum++;
  }
  int temp = min_value - height;
  return min(sum,
             dfs(ll, pos - 1, val[pos]) + dfs(pos + 1, r, val[pos]) + temp);
}
int main() {
  int i, res;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", val + i);
  res = min(n, dfs(1, n, 0));
  printf("%d\n", res);
  return 0;
}
