#include <bits/stdc++.h>
using namespace std;
vector<long long int> fence;
long long int solve(int l, int r, long long int ht) {
  long long int m_f = fence[l];
  for (int i = l; i <= r; i++) m_f = min(m_f, fence[i]);
  int next_l = l;
  long long int ans = m_f - ht;
  for (int i = l; i <= r; i++) {
    if (fence[i] == m_f) {
      if (next_l < i) ans += solve(next_l, i - 1, m_f);
      next_l = i + 1;
    }
    if (i == r) {
      if (next_l <= i && fence[i] != m_f) ans += solve(next_l, i, m_f);
    }
  }
  ans = min(ans, (long long int)r - l + 1);
  return ans;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long int tmp;
    scanf("%lld", &tmp);
    fence.push_back(tmp);
  }
  long long int ans = solve(0, n - 1, 0);
  cout << ans;
  return 0;
}
