#include <bits/stdc++.h>
using namespace std;
template <class T>
void show(const vector<T> &a) {
  for (T x : a) cout << x << " ";
  cout << '\n';
}
const long long N = 1 << 17, oo = 1e18 + 500, mod = 1e9 + 7;
const long double eps = 1e-9, PI = 2 * acos(0.0);
vector<pair<long long, long long> > vertices[N];
vector<long long> visit(N, 0);
long long n, m, k;
long long cnt = 0;
long long a[5050];
long long dp[5050][5050];
long long rec(long long l, long long r, long long h) {
  if (l > r) return 0;
  long long mn = oo;
  long long ind = -1;
  for (long long i = l; i <= r; i++) {
    if (mn > a[i]) {
      mn = a[i];
      ind = i;
    }
  }
  long long ret =
      min(r - l + 1, mn - h + rec(l, ind - 1, mn) + rec(ind + 1, r, mn));
  return ret;
}
signed main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << rec(0, n - 1, 0);
  return 0;
}
