#include <bits/stdc++.h>
using namespace std;
const int N = 5007;
int n;
int a[N];
long long dfs(int l, int r, int del) {
  if (l > r) return 0;
  if (l == r && a[l] == del) return 0;
  long long ans = r - l + 1;
  if (l == r) return ans;
  int mn = a[l] - del;
  for (int i = l; i <= r; i++) mn = min(mn, a[i] - del);
  long long ans2 = mn;
  int pre_pos = l - 1;
  for (int i = l; i <= r; i++) {
    if (a[i] - del == mn) {
      ans2 += dfs(pre_pos + 1, i - 1, del + mn);
      pre_pos = i;
    }
  }
  ans2 += dfs(pre_pos + 1, r, del + mn);
  return min(ans, ans2);
}
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  printf("%lld\n", dfs(1, n, 0));
}
int main() {
  int NT = 1;
  for (int i = 1; i <= NT; i++) solve();
  return 0;
}
