#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, i, j, ii, jj, zx, xc, pas, p[5009], pi;
map<int, int> m;
map<int, int>::iterator it;
short rec(short q, short w) {
  short jm = 0, we = 0;
  int qw = 1000000002;
  short h = 0;
  for (h = q; h <= w; h++) {
    if (qw > p[h]) qw = p[h];
  }
  if (qw > w - q + 1) {
    return w - q + 1;
  }
  jm = qw;
  for (h = q; h <= w; h++) p[h] -= qw;
  we = q - 1;
  for (h = q; h <= w; h++) {
    if (0 == p[h]) {
      if (we != h - 1) {
        jm += rec(we + 1, h - 1);
        if (jm > w - q + 1) return w - q + 1;
      }
      we = h;
    }
  }
  if (we != w) {
    jm += rec(we + 1, w);
  }
  if (jm > w - q + 1) jm = w - q + 1;
  return jm;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> a;
  for (i = 1; i <= a; i++) {
    cin >> c;
    m[i] = c;
  }
  zx = -1000;
  xc = 0;
  pi = 0;
  for (it = m.begin(); it != m.end(); it++) {
    if ((*it).first != zx + 1) {
      if (xc != 0) {
        pas += rec(1, xc);
        xc = 1;
        pi = 1;
        p[pi] = (*it).second;
        zx = (*it).first;
      } else {
        xc = 1;
        pi = 1;
        p[pi] = (*it).second;
        zx = (*it).first;
      }
    } else {
      xc++;
      pi++;
      p[pi] = (*it).second;
      zx = (*it).first;
    }
  }
  if (xc != 0) {
    pas += rec(1, xc);
  }
  cout << pas;
  return 0;
}
