#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int n, a[N], inf = 1e5;
vector<int> v;
int fact(int l, int r, int cnt) {
  if (l > r) return 0;
  if (cnt > n) return inf;
  vector<int> v;
  for (int i = l; i <= r; i++) {
    a[i]--;
    if (!a[i]) v.push_back(i);
  }
  v.push_back(r + 1);
  int last = l - 1, sz = (int)(v.size()), ret = 1;
  for (int i = 0; i < sz; i++) {
    ret += fact(last + 1, v[i] - 1, cnt + 1);
    last = v[i];
  }
  return min(ret, r - l + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (!a[i]) v.push_back(i);
  }
  v.push_back(n + 1);
  int ans = 0, last = 0;
  for (int i = 0; i < v.size(); i++) {
    ans += fact(last + 1, v[i] - 1, 0);
    last = v[i];
  }
  printf("%d\n", ans);
}
