#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6;
int in[N];
int t[N], d;
int rmq(int l, int r) {
  int p = l;
  l += d;
  r += d;
  while (l <= r) {
    if (l % 2 == 1) p = in[p] > in[t[l]] ? t[l] : p;
    if (r % 2 == 0) p = in[p] > in[t[r]] ? t[r] : p;
    ++l /= 2;
    --r /= 2;
  }
  return p;
}
int calc(int l, int r, int h) {
  if (l > r) return 0;
  int pos = rmq(l, r);
  int minn = in[pos];
  int res = min(r - l + 1,
                calc(l, pos - 1, minn) + calc(pos + 1, r, minn) + minn - h);
  return res;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", in + i);
  in[n] = 2e9;
  for (d = 1; d < n; d *= 2)
    ;
  for (int i = 0; i < n; ++i) t[i + d] = i;
  for (int i = n + d; i < d + d; i++) t[i] = n;
  for (int i = d - 1; i >= 1; --i)
    t[i] = in[t[i * 2]] < in[t[i * 2 + 1]] ? t[i * 2] : t[i * 2 + 1];
  int res = calc(0, n - 1, 0);
  printf("%d\n", res);
}
