#include <bits/stdc++.h>
using namespace std;
int n, k, i, j, ans, f[6000][6000], a[6000];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > n) a[i] = n + 1;
  }
  memset(f, 0x7f, sizeof(f));
  f[1][0] = 1;
  f[1][a[1]] = a[1];
  for (i = 1; i < a[1]; i++) f[1][i] = 1 + i;
  for (i = 2; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      for (j = 0; j < a[i]; j++) f[i][j] = f[i - 1][j] + 1;
      for (j = a[i]; j <= a[i - 1]; j++)
        f[i][a[i]] = min(f[i][a[i]], f[i - 1][j]);
    } else if (a[i] > a[i - 1]) {
      for (j = 0; j <= a[i - 1]; j++) f[i][j] = f[i - 1][j] + 1;
      for (j = a[i - 1] + 1; j < a[i]; j++)
        f[i][j] = f[i - 1][a[i - 1]] + j - a[i - 1] + 1;
      f[i][a[i]] = min(f[i - 1][0] + a[i], f[i - 1][a[i - 1]] + j - a[i - 1]);
    }
  }
  ans = n + 1;
  for (j = 0; j <= a[n]; j++) ans = min(ans, f[n][j]);
  cout << ans << endl;
  return 0;
}
