#include <bits/stdc++.h>
using namespace std;
const long long N = 5005;
long long v[N];
long long solve(long long l, long long r, long long delta) {
  if (l == r) return delta < v[l];
  long long mn = 2e9;
  for (long long i = l; i <= r; i++) {
    mn = min(mn, v[i]);
  }
  long long ans = r - l + 1;
  long long tans = mn - delta;
  long long pos = l;
  while (pos <= r) {
    if (v[pos] == mn) {
      pos++;
      continue;
    }
    long long tpos = pos;
    while (tpos <= r && v[tpos] > mn) tpos++;
    tans += solve(pos, tpos - 1, mn);
    pos = tpos;
  }
  return min(ans, tans);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  cout << solve(0, n - 1, 0);
}
