#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
  cout << x << endl;
  exit(0);
}
const int maxn = 4e5 + 5;
const int inf = 1e9 + 5;
int n;
int a[maxn];
int solve(int l, int r, int h) {
  if (l > r) return 0;
  int vertical = r - l + 1;
  int cur = 0;
  int mn = inf;
  for (int i = l; i <= r; i++) {
    mn = min(mn, a[i]);
  }
  int idx = l - 1;
  for (int i = l; i <= r; i++) {
    if (a[i] == mn) {
      if (idx < i - 1) {
        cur += solve(idx + 1, i - 1, mn);
      }
      idx = i;
    }
  }
  if (idx != r) {
    cur += solve(idx + 1, r, mn);
  }
  return min(vertical, cur + mn - h);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cout << solve(1, n, 0) << '\n';
  return 0;
}
