#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const long long mod = 1e9 + 7;
const int N = 5e3 + 10, M = 1e4 + 10;
const long long INF = 1e18;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
int a[N];
int dfs(int l, int r) {
  if (l > r) return 0;
  if (l == r) return min(a[l], 1);
  int minn = inf, mid;
  for (int i = l; i <= r; i++) {
    if (a[i] < minn) {
      minn = a[i];
      mid = i;
    }
  }
  for (int i = l; i <= r; i++) a[i] -= minn;
  return min(r - l + 1, dfs(l, mid - 1) + dfs(mid + 1, r) + minn);
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cout << dfs(1, n);
}
