#include <bits/stdc++.h>
using namespace std;
int a[5050];
int height[5050];
int dfs(int l, int r, int h) {
  int hmin = height[h + 1];
  int s = 0;
  for (int i = l; i <= r; i++) {
    if (a[i] > hmin) {
      int ll = i;
      while (i <= r && a[i] > hmin) i++;
      s += dfs(ll, i - 1, h + 1);
    }
  }
  return min(1 + r - l, s + hmin - height[h]);
}
int main() {
  int n;
  scanf("%d", &n);
  height[0] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    height[i + 1] = a[i];
  }
  sort(height, height + n + 1);
  int k = 1;
  for (int i = 1; i <= n; i++) {
    if (height[i] != height[k]) {
      height[++k] = height[i];
    }
  }
  height[k + 1] = height[k];
  k++;
  printf("%d\n", dfs(0, n - 1, 0));
}
