#include <bits/stdc++.h>
using namespace std;
const int N = 5100;
int a[N], n, i, j, f[N][N], ma, t;
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > ma) ma = a[i];
  }
  for (i = 0; i <= n; i++)
    for (j = 0; j <= n; j++) f[i][j] = n + 1;
  f[0][0] = 0;
  for (i = 0; i < n; i++)
    for (j = 0; j <= a[i] && j <= n; j++) {
      if (j + 1 <= n && j + 1 <= a[i])
        f[i][j + 1] = min(f[i][j + 1], f[i][j] + 1);
      t = min(j, a[i + 1]);
      f[i + 1][t] = min(f[i + 1][t], f[i][j] + (j != a[i]));
    }
  printf("%d\n", f[n][0]);
  return 0;
}
