#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
int N;
int A[5011];
int rec(int L, int R, int target) {
  int n = R - L;
  if (n <= 0) return 0;
  int s = L;
  for (int i = L; i < R; i++) {
    if (A[s] > A[i]) s = i;
  }
  int lp = rec(L, s, A[s]);
  int rp = rec(s + 1, R, A[s]);
  long long cst = n;
  amin(cst, (long long)lp + rp + A[s] - target);
  return cst;
}
void MAIN() {
  scanf("%d", &N);
  for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", A + i);
  int ans = rec(0, N, 0);
  printf("%d\n", ans);
}
int main() {
  int TC = 1;
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}
