#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[N];
int dfs(int s, int e, int h) {
  if (s > e) return 0;
  int mid = min_element(a + s, a + e + 1) - a;
  return min(e - s + 1,
             dfs(s, mid - 1, a[mid]) + dfs(mid + 1, e, a[mid]) + a[mid] - h);
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  cout << dfs(1, n, 0) << endl;
  return 0;
}
