#include <bits/stdc++.h>
using namespace std;
int n;
long long a[5001];
int res = 0;
void input() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
}
void output() {
  for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
  cout << endl;
}
int solve(int l, int r) {
  if (l > r) return 0;
  if (l == r) {
    return 1;
  }
  int ans = 0;
  vector<int> minList;
  minList.push_back(l);
  for (int i = l + 1; i <= r; ++i) {
    if (a[i] < a[minList[0]]) {
      minList.clear();
      minList.push_back(i);
    } else if (a[i] == a[minList[0]]) {
      minList.push_back(i);
    }
  }
  ans += a[minList[0]];
  int minValue = a[minList[0]];
  for (int i = l; i <= r; ++i) {
    a[i] = a[i] - minValue;
  }
  int len = minList.size();
  ans += solve(l, minList[0] - 1);
  for (int k = 0; k < len - 1; ++k) {
    ans += solve(minList[k] + 1, minList[k + 1] - 1);
  }
  ans += solve(minList[len - 1] + 1, r);
  return min(ans, r - l + 1);
}
int main() {
  input();
  cout << solve(1, n) << endl;
}
