#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int a[N];
long long dfs(int l, int r) {
  if (r < l) return 0;
  int Min = 1 << 30;
  long long sum = 0;
  for (int i = l; i <= r; i++) Min = min(Min, a[i]);
  int las = l - 1;
  for (int i = l; i <= r; i++) {
    a[i] -= Min;
    if (a[i] == 0) {
      sum += dfs(las + 1, i - 1);
      las = i;
    }
  }
  sum += dfs(las + 1, r);
  return min((long long)r - l + 1, sum + Min);
}
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    cout << dfs(1, n) << endl;
  }
  return 0;
}
