#include <bits/stdc++.h>
using namespace std;
int po(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res *= x;
    y >>= 1;
    x *= x;
  }
  return res;
}
struct Compare {
  constexpr bool operator()(
      pair<long long, pair<long long, long long> > const& a,
      pair<long long, pair<long long, long long> > const& b) const noexcept {
    return a.first < b.first ||
           (a.first == b.first && a.second.first > b.second.first);
  }
};
long long solve(long long a[], long long n) {
  if (n == 1 && a[0] > 0) return 1;
  if (n == 1 && a[0] == 0) return 0;
  long long mn = 10000000000;
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    mn = min(mn, a[i]);
  }
  long long ar[n + 1];
  for (long long i = 0; i < n; i++) {
    a[i] -= mn;
  }
  ans += mn;
  long long u = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] != 0) {
      ar[u] = a[i];
      u++;
    } else if (a[i] == 0 && u != 0) {
      ans += solve(ar, u);
      u = 0;
    }
  }
  if (u != 0) {
    ans += solve(ar, u);
  }
  return (min(ans, n));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  long long arr[n + 1];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  cout << solve(arr, n) << endl;
}
