#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = 0;
  while (!isdigit(c)) {
    if (c == '-') flag = 1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  x = flag ? -x : x;
}
template <class T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
const int maxn = 5e3 + 10;
const int mod = 1e9 + 7;
const double eps = 1e-7;
int a[maxn];
int solve(int l, int r) {
  if (l > r) return 0;
  if (l == r) return (a[l] != 0);
  int ans1 = r - l + 1;
  int ans2 = 0, mn = 0x3f3f3f3f;
  for (int i = l; i <= r; ++i) {
    mn = min(mn, a[i]);
  }
  ans2 += mn;
  int p = -1;
  for (int i = l; i <= r; ++i) {
    a[i] -= mn;
    if (a[i] == 0 && p != -1) {
      ans2 += solve(p, i - 1);
      p = -1;
    }
    if (a[i] > 0 && p == -1) {
      p = i;
    }
  }
  if (p != -1) ans2 += solve(p, r);
  return min(ans1, ans2);
}
int main() {
  int n;
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  printf("%d\n", solve(1, n));
}
