#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n, arr[N];
int solve(int l, int r, int minus) {
  if (l > r) return 0;
  int mn = 1e9 + 1;
  for (int i = l; i <= r; i++) mn = min(mn, arr[i] - minus);
  int ans = mn, lst = l;
  for (int i = l; i <= r; i++)
    if (arr[i] - minus == mn) ans += solve(lst, i - 1, minus + mn), lst = i + 1;
  ans += solve(lst, r, minus + mn);
  return min(ans, (r - l + 1));
}
int main() {
  cin >> n;
  int mn = 1e9 + 1;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mn = min(mn, arr[i]);
  }
  if (mn != 0)
    cout << solve(0, n - 1, 0);
  else {
    int ans = 0, lst = 0;
    for (int i = 0; i < n; i++)
      if (arr[i] == 0) ans += solve(lst, i - 1, 0), lst = i + 1;
    ans += solve(lst, n - 1, 0);
    cout << ans;
  }
  return 0;
}
