#include <bits/stdc++.h>
using namespace std;
const long long N = 5e3 + 53, INF = 2147483647;
long long n, H[N];
long long Solve(long long, long long);
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i += 1) cin >> H[i];
  cout << Solve(1ll, n);
  return 0;
}
long long Solve(long long l, long long r) {
  if (l == r) {
    return min(1ll, H[l]);
  }
  if (l > r) {
    return 0ll;
  }
  long long MIN = INF, Cnt_1 = r - l + 1, Pos = 0;
  for (long long i = l; i <= r; i += 1) {
    if (H[i] < MIN) {
      MIN = H[i];
      Pos = i;
    }
  }
  for (long long i = l; i <= r; i += 1) H[i] -= MIN;
  long long Cnt_2 = MIN + Solve(l, Pos - 1) + Solve(Pos + 1, r);
  return min(Cnt_1, Cnt_2);
}
