#include <bits/stdc++.h>
using namespace std;
int a[5001];
int dfs(int l, int r) {
  int pre = l, ok = 0, k = 1 << 30;
  for (int i = l; i <= r; i++)
    if (a[i]) ok = 1;
  if (!ok) return 0;
  for (int i = l; i <= r; i++) k = min(k, a[i]);
  for (int i = l; i <= r; i++) a[i] -= k;
  int ans = k;
  for (int i = l; i <= r; i++)
    if (a[i]) {
      int j = i + 1;
      while (j <= r && a[j]) j++;
      ans += dfs(i, j - 1);
      i = j;
    }
  return min(ans, r - l + 1);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  cout << dfs(0, n - 1) << endl;
}
