#include <bits/stdc++.h>
using namespace std;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const long long LINF = 1e18 + 100;
int n, a[5050];
int solve(int l, int r, int h) {
  if (l > r) return 0;
  if (r == l) {
    return (a[l] != h);
  }
  pair<int, int> mn = make_pair(INF, INF);
  for (int i = (l); i < (r + 1); i++) {
    mn = min(mn, make_pair(a[i], i));
  }
  int res = mn.first - h;
  res += solve(l, mn.second - 1, mn.first);
  res += solve(mn.second + 1, r, mn.first);
  res = min(res, r - l + 1);
  return res;
}
int32_t main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = (0); i < (n); i++) cin >> a[i];
  cout << solve(0, n - 1, 0) << '\n';
  return 0;
}
