#include <bits/stdc++.h>
using namespace std;
vector<int> V;
int N;
int go(int L, int R) {
  if (R < L)
    return 0;
  else if (R == L)
    return 1;
  int mini = INT_MAX;
  for (int i = L; i < R + 1; ++i) mini = min(mini, V[i]);
  for (int i = L; i < R + 1; ++i) V[i] -= mini;
  int ans = 0, s;
  for (int i = L; i <= R;) {
    if (V[i]) {
      s = i;
      while (i <= R && V[i]) i++;
      ans += go(s, i - 1);
    } else
      i++;
  }
  return min(mini + ans, R - L + 1);
}
int main() {
  cin >> N;
  int a;
  for (int i = 0; i < N; ++i) {
    scanf("%d", &a);
    V.push_back(a);
  }
  cout << go(0, N - 1) << endl;
  return 0;
}
