#include <bits/stdc++.h>
using namespace std;
using VI = vector<short>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
using VB = vector<bool>;
const int Inf = 0x3f3f3f3f;
const int DIM = 5004;
int T;
int N;
vector<int> a;
int dp[DIM][DIM][2];
void ReadData();
void Solve();
int main() {
  ReadData();
  Solve();
  return 0;
}
void ReadData() {
  cin >> N;
  a = vector<int>(N + 1);
  for (int i = 1; i <= N; ++i) cin >> a[i];
  cin.get();
}
void Solve() {
  for (int i = 0; i < DIM; ++i)
    for (int j = i; j < DIM; ++j) dp[i][j][0] = dp[i][j][1] = Inf;
  for (int i = 1; i <= N; ++i) {
    dp[i][i][0] = a[i] > 0 ? 1 : 0;
    dp[i][i][1] = min(Inf, a[i]);
  }
  for (int i = 1; i < N; ++i) {
    dp[i][i + 1][0] = dp[i][i][0] + dp[i + 1][i + 1][0];
    dp[i][i + 1][1] =
        min(dp[i][i + 1][1], (min(a[i], a[i + 1]) + (a[i] != a[i + 1])));
  }
  for (int d = 2; d < N; ++d) {
    deque<short> Q;
    for (int i = 1; i <= d; ++i) {
      while (!Q.empty() && a[i] <= a[Q.back()]) Q.pop_back();
      Q.push_back(i);
    }
    for (int i = 1; i + d <= N; ++i) {
      int j = i + d;
      while (!Q.empty() && Q.front() < i) Q.pop_front();
      while (!Q.empty() && a[j] <= a[Q.back()]) Q.pop_back();
      Q.push_back(j);
      int p = Q.front();
      dp[i][j][0] = min(min(dp[i + 1][j][0], dp[i + 1][j][1]),
                        min(dp[i][j - 1][0], dp[i][j - 1][1])) +
                    1;
      dp[i][j][1] =
          min(dp[i][j][1],
              a[p] + min(dp[i][p - 1][0], max(0, dp[i][p - 1][1] - a[p])) +
                  min(dp[p + 1][j][0], max(0, dp[p + 1][j][1] - a[p])));
    }
  }
  cout << min(dp[1][N][0], dp[1][N][1]) << '\n';
}
