#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAXN = 5000 + 5;
int a[MAXN];
int solve(int l, int r) {
  if (l > r) return 0;
  int m = min_element(a + l, a + r + 1) - a;
  int a_m = a[m];
  for (int i = l; i <= r; ++i) a[i] -= a_m;
  return min(r - l + 1, solve(l, m - 1) + solve(m + 1, r) + a_m);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  cout << solve(0, n - 1) << endl;
  return 0;
}
