#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int n, a[5005];
int rec(int l, int r) {
  int mn = 1e9;
  for (int i = l; i <= r; i++) mn = min(mn, a[i]);
  int c = mn;
  vector<int> x;
  for (int i = l; i <= r; i++) a[i] -= mn;
  for (int i = l; i <= r; i++) {
    if (a[i]) {
      x.push_back(i);
    } else {
      if (!(int)x.size()) continue;
      int tl = x.front(), tr = x.back();
      c += rec(tl, tr);
      x.clear();
    }
  }
  if ((int)x.size()) {
    int tl = x.front(), tr = x.back();
    c += rec(tl, tr);
    x.clear();
  }
  return min(r - l + 1, c);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cout << rec(1, n);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cout.precision(30);
  cerr.precision(7);
  ;
  int T = 1;
  while (T--) solve();
  return 0;
}
