#include <bits/stdc++.h>
using namespace std;
int getint() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}
const int N = 5e3 + 10;
int a[N];
long long solve(int l, int r) {
  int mn = 0x7f7f7f7f;
  for (int i = l; i <= r; i++) mn = min(mn, a[i]);
  long long ans = 0;
  int t = l;
  for (int i = l; i <= r; i++) {
    a[i] -= mn;
    if (a[i] == 0) {
      if (i != t) ans += solve(t, i - 1);
      t = i + 1;
    }
  }
  if (t != r + 1) ans += solve(t, r);
  ans += mn;
  ans = min(ans, r - l + 1ll);
  return ans;
}
int main() {
  int n = getint();
  for (int i = 1; i <= n; i++) a[i] = getint();
  cout << solve(1, n);
}
