#include <bits/stdc++.h>
using namespace std;
int a[60000];
int solve(int l, int r) {
  if (l > r)
    return 0;
  else if (l == r) {
    if (a[l] == 0)
      return 0;
    else
      return 1;
  }
  int cnt1 = 0x3f3f3f3f, mid;
  for (int i = l; i <= r; i++) {
    if (a[i] < cnt1) {
      cnt1 = a[i];
      mid = i;
    }
  }
  for (int i = l; i <= r; i++) a[i] -= cnt1;
  return min(cnt1 + solve(l, mid - 1) + solve(mid + 1, r), r - l + 1);
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int ans = solve(1, n);
  cout << ans << endl;
  return 0;
}
