#include <bits/stdc++.h>
using namespace std;
const int N = 5003;
long long inf = 1e10;
int a[N];
int rmq(int l, int r) {
  long long m = inf;
  int x;
  for (int i = (l); i <= (r); ++i)
    if (a[i] < m) m = a[i], x = i;
  return x;
}
long long solve(int l, int r) {
  if (l > r) return 0;
  int m = rmq(l, r);
  return min((long long)r - l + 1, solve(l, m - 1) + solve(m + 1, r) + a[m] -
                                       max(a[l - 1], a[r + 1]));
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
  printf("%lld\n", solve(1, n));
}
