#include <bits/stdc++.h>
using namespace std;
int h[5001];
int n;
int solve(int* l, int* r, int m) {
  if (l == r)
    return 0;
  else if (l + 1 == r)
    return *l > m;
  int* min_pos = min_element(l, r);
  return min((int)(r - l), solve(l, min_pos, *min_pos) +
                               solve(min_pos + 1, r, *min_pos) + *min_pos - m);
}
int main() {
  cin >> n;
  for (int i = 0, __end = n; i < __end; ++i) cin >> h[i];
  cout << solve(h, h + n, 0);
}
