#include <bits/stdc++.h>
using namespace std;
mt19937 rnd;
const int N = 5e3 + 10;
const int inf = 1e9 + 10;
int a[N], dp[N], new_dp[N], pref[N], suf[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int j = 0; j <= n; j++) {
    if (j <= a[1]) {
      dp[j] = j + (a[1] > j);
    } else {
      dp[j] = inf;
    }
  }
  pref[0] = dp[0];
  for (int j = 1; j <= n; j++) {
    pref[j] = min(pref[j - 1], dp[j] - j);
  }
  suf[n] = dp[n];
  for (int j = n - 1; j >= 0; j--) {
    suf[j] = min(suf[j + 1], dp[j]);
  }
  for (int i = 2; i <= n; i++) {
    new_dp[0] = suf[0] + (a[i] > 0);
    for (int j = 1; j <= n; j++) {
      if (j <= a[i]) {
        new_dp[j] = min(suf[j], pref[j] + j) + (a[i] > j);
      } else {
        new_dp[j] = inf;
      }
    }
    swap(dp, new_dp);
    pref[0] = dp[0];
    for (int j = 1; j <= n; j++) {
      pref[j] = min(pref[j - 1], dp[j] - j);
    }
    suf[n] = dp[n];
    for (int j = n - 1; j >= 0; j--) {
      suf[j] = min(suf[j + 1], dp[j]);
    }
  }
  cout << suf[0] << "\n";
  return 0;
}
