#include <bits/stdc++.h>
using namespace std;
int i, a[5001], n;
int f(int b, int s, int y) {
  if (b > s) return 0;
  int m = min_element(a + b, a + s + 1) - a;
  return min(s - b + 1, f(b, m - 1, a[m]) + f(m + 1, s, a[m]) + a[m] - y);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) cin >> a[i];
  cout << f(1, n, 0) << "\n";
}
