#include <bits/stdc++.h>
using namespace std;
int a[50001];
int paintfence(int x, int y, int ht) {
  if (x >= y) return 0;
  int z = x;
  for (int i = x; i < y; ++i) {
    if (a[i] < a[z]) z = i;
  }
  return min(y - x,
             a[z] - ht + paintfence(x, z, a[z]) + paintfence(z + 1, y, a[z]));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int i, res;
  for (i = 0; i < n; ++i) {
    cin >> a[i];
  }
  res = paintfence(0, n, 0);
  cout << res;
  return 0;
}
