#include <bits/stdc++.h>
using namespace std;
const int N = 5009, M = 0;
int read() {
  char c;
  int num, f = 1;
  while (c = getchar(), !isdigit(c))
    if (c == '-') f = -1;
  num = c - '0';
  while (c = getchar(), isdigit(c)) num = num * 10 + c - '0';
  return num * f;
}
long long n, a[N];
long long dfs(int l, int r) {
  if (l > r) return 0;
  if (l == r) return min(a[l], 1ll);
  long long minn = (1ll << 63) - 1, p;
  for (int i = l; i <= r; i++)
    if (a[i] < minn) {
      minn = a[i];
      p = i;
    }
  for (int i = l; i <= r; i++) a[i] -= minn;
  return min(dfs(l, p - 1) + dfs(p + 1, r) + minn, 1ll * (r - l + 1));
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  printf("%lld\n", dfs(1, n));
  return 0;
}
