#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
using namespace std;
long long n;
vector<long long> a;
long long sol(long long l, long long r, long long sf) {
  if (r - l < 0) return 0;
  long long i = min_element(a.begin() + l, a.begin() + r + 1) - a.begin();
  return min(r - l + 1, a[i] - sf + sol(l, i - 1, a[i]) + sol(i + 1, r, a[i]));
}
signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> n;
  a.resize(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  cout << sol(0, n - 1, 0) << "\n";
}
