#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cout << '\n'; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "] ";
  err(++it, args...);
}
pair<int, int> dx[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const int inf = 2e9 + 7;
const long long INF = 3e18 + 7;
const long double pi = 3.1415926535897932384626433;
const long double eps = 1e-6;
const long double EPS = 1e-9;
const int mod = 1e9 + 7;
long long MOD = 1e15 + 7;
int gcd(int a, int b) {
  if (a < b) swap(a, b);
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
const bool multiple_tests = 0;
const int N = 1e5 + 7;
int n, a[N];
int rec(int l, int r) {
  int mn = inf;
  for (int i = l; i <= r; i++) {
    mn = min(a[i], mn);
  }
  int prev = 0, sum = 0;
  for (int i = l; i <= r; i++) {
    a[i] -= mn;
    if (!a[i]) {
      if (prev) {
        sum += rec(prev, i - 1);
        prev = 0;
      }
    } else if (!prev)
      prev = i;
  }
  if (prev) sum += rec(prev, r);
  for (int i = l; i <= r; i++) {
    a[i] += mn;
  }
  return min(sum + mn, r - l + 1);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  cout << min(n, rec(1, n));
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TT = 1;
  if (multiple_tests) cin >> TT;
  for (int T = 1; T <= TT; T++) {
    solve();
    cout << '\n';
  }
  return 0;
}
