#include <bits/stdc++.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wc++11-extensions"
#pragma GCC diagnostic ignored "-Wunused-const-variable"
using namespace std;
void ain(vector<long long>& a) {
  for (long long i = 0; i < (long long)a.size(); i++) cin >> a[i];
}
void print(vector<long long>& a, char c = ' ') {
  for (long long i = 0; i < a.size(); i++) cout << a[i] << c;
}
long long max(vector<long long>& a) {
  long long Max = a[0];
  for (long long i = 1; i < a.size(); i++) Max = max(Max, a[i]);
  return Max;
}
long long min(vector<long long>& a) {
  long long Min = a[0];
  for (long long i = 1; i < a.size(); i++) Min = min(Min, a[i]);
  return Min;
}
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a > b) ? b : a; }
long long Ceil(double x) {
  return (((long long)x == x) ? x : (long long)x + 1);
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long n;
vector<long long> a;
long long f(long long l, long long r) {
  if (l > r) return 0;
  if (l == r) return a[l] == 0 ? 0 : 1;
  long long Mini = l;
  for (long long i = l; i <= r; i++) {
    if (a[i] < a[Mini]) Mini = i;
  }
  long long Min = a[Mini];
  for (long long i = l; i <= r; i++) {
    a[i] -= Min;
  }
  return min(r - l + 1, f(l, Mini - 1) + f(Mini + 1, r) + Min);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  a.assign(n, 0);
  ain(a);
  cout << f(0, n - 1) << endl;
  return 0;
}
