#include <bits/stdc++.h>
using namespace std;
long long INF = LONG_LONG_MAX;
long long mod = 1000000000 + 7;
template <typename T, typename L>
void readVector(vector<T>& _data, L& _size, bool _shift) {
  _data.resize(_size + (long long)_shift);
  for (long long i = (long long)_shift; i < _size + (long long)_shift; i++)
    cin >> _data[i];
}
template <typename T, typename L>
void readMatrix(vector<vector<T>>& _data, L& _rows, L& _cols, bool _shiftRows,
                bool _shiftCols) {
  _data.resize(_rows + (long long)_shiftRows);
  for (long long i = 0; i < _rows + (long long)_shiftRows; i++)
    _data[i].resize(_cols + (long long)_shiftCols);
  for (long long i = (long long)_shiftRows; i < _rows + (long long)_shiftRows;
       i++)
    for (long long j = (long long)_shiftCols; j < _cols + (long long)_shiftCols;
         j++)
      cin >> _data[i][j];
}
long long n;
vector<long long> a;
long long value(long long l, long long r, long long m) {
  if (l > r) return 0;
  long long mi = *min_element(a.begin() + l, a.begin() + r + 1);
  long long ans = mi - m;
  int i = l;
  while (i <= r) {
    while (i <= r && a[i] == mi) i++;
    int lo = i;
    while (i <= r && a[i] > mi) i++;
    ans += value(lo, i - 1, mi);
  }
  ans = min(ans, r - l + 1);
  return ans;
}
void solve() {
  cin >> n;
  readVector(a, n, 0);
  cout << value(0, n - 1, 0) << endl;
}
int main() {
  ios::sync_with_stdio(false);
  bool _multipleTestCases = false;
  if (_multipleTestCases) {
    long long t;
    cin >> t;
    while (t--) solve();
  } else {
    solve();
  }
  return 0;
}
