#include <bits/stdc++.h>
using namespace std;
inline const int read() {
  int r = 0, k = 1;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') k = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) r = r * 10 + c - '0';
  return k * r;
}
int n, a[5555];
bool MIN(int &a, int b) { return a > b ? a = b, 1 : 0; }
int cal(int l, int r) {
  if (l > r) return 0;
  int pos, mi = 0x7fffffff, mx = 0;
  for (int i = (l), _ = (r); i <= _; i++)
    if (MIN(mi, a[i])) pos = i;
  for (int i = (l), _ = (r); i <= _; i++) mx = max(mx, a[i]), a[i] -= mi;
  if (mx == 0) return 0;
  return min(r - l + 1, mi + cal(l, pos - 1) + cal(pos + 1, r));
}
void input() {
  n = read();
  for (int i = (1), _ = (n); i <= _; i++) a[i] = read();
}
void solve() { printf("%d\n", cal(1, n)); }
int main() {
  input(), solve();
  return 0;
}
