#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5;
const long long INF = 1e18;
long long n;
long long a[MAXN];
long long tinh(long long l, long long r, long long x) {
  long long tong1 = 0;
  long long minx = INF;
  for (long long i = l; i <= r; i++) {
    minx = min(minx, a[i] - x);
    if (a[i] - x > 0) tong1++;
  }
  long long tong2 = minx;
  for (long long i = l; i <= r; i++) {
    if (a[i] - x == minx) continue;
    long long j = i;
    while (i + 1 <= r && a[i + 1] - x != minx) i++;
    tong2 += tinh(j, i, x + minx);
  }
  return min(tong1, tong2);
}
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  cout << tinh(1, n, 0);
}
