#include <bits/stdc++.h>
using namespace std;
long ar[5002], n;
long long calc(long long l, long long r, long long prev) {
  if (l == r) return 0;
  int in = min_element(ar + l, ar + r) - ar;
  return min(r - l,
             calc(l, in, ar[in]) + calc(in + 1, r, ar[in]) + ar[in] - prev);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> ar[i];
  cout << calc(0, n, 0);
  return 0;
}
