#include <bits/stdc++.h>
using namespace std;
long long f(vector<int> v) {
  if ((int)v.size() == 0) return 0;
  int n = (int)v.size();
  int mini = *min_element(v.begin(), v.end());
  long long ans = mini;
  for (int i = 0; i < n; i++) {
    v[i] -= mini;
  }
  vector<int> cur;
  for (int i = 0; i < n; i++) {
    if (v[i] != 0) {
      cur.push_back(v[i]);
    } else {
      ans += f(cur);
      cur.clear();
    }
  }
  ans += f(cur);
  long long ans2 = min((long long)n, ans);
  return ans2;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases;
  cases = 1;
  for (int tt = 0; tt < cases; tt++) {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    long long ans = 0;
    ans = f(v);
    cout << ans << endl;
  }
}
