#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
int a[N];
long long solve(int, int);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  cout << solve(1, n);
}
long long solve(int l, int r) {
  if (l > r) return 0;
  int mn = 1e9 + 1;
  int id = -1;
  for (int i = l; i <= r; ++i) {
    if (mn > a[i]) {
      mn = a[i];
      id = i;
    }
  }
  int last = max(a[l - 1], a[r + 1]);
  return min(
      {1LL * r - l + 1, solve(l, id - 1) + solve(id + 1, r) + mn - last});
}
