#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep;
  for (const auto &x : v) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename T, size_t size>
ostream &operator<<(ostream &os, const array<T, size> &arr) {
  os << '{';
  string sep;
  for (const auto &x : arr) os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}
void dbg_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
const long long N = 5e3 + 5, mod = 1e9 + 7, bit = 61;
long long a[N], n;
long long solve(long long l, long long r) {
  if (l > r) {
    return 0;
  }
  long long id = min_element(a + l, a + r + 1) - a;
  long long mn = a[id];
  long long offset = max(a[l - 1], a[r + 1]);
  return min(r - l + 1, solve(l, id - 1) + solve(id + 1, r) + mn - offset);
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long t = 1;
  for (long long tc = 1; tc <= t; tc++) {
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cin >> a[i];
    }
    cout << solve(1, n) << '\n';
  }
  return 0;
}
