#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
int N;
vector<int> a;
int solve(vector<int>& vi, int rem) {
  if (vi.empty()) return 0;
  if (vi.size() == 1) {
    if (vi[0] == rem)
      return 0;
    else
      return 1;
  }
  int n = vi.size();
  int res = 0;
  int mi = 1e9;
  for (auto& it : vi) {
    mi = min(mi, it);
  }
  vector<int> now;
  for (auto& it : vi) {
    if (it == mi) {
      res += solve(now, mi);
      now.clear();
    } else {
      now.push_back(it);
    }
  }
  res += solve(now, mi);
  return min((int)vi.size(), res + mi - rem);
}
signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  a.assign(N, 0);
  for (auto& it : a) {
    cin >> it;
  }
  cout << solve(a, 0);
}
