#include <bits/stdc++.h>
using namespace std;
const int N = 5010, T = 5010, size = 1 << 20, mod = 998244353, inf = 1e9;
template <class o>
void qr(o &x) {
  char c = getchar();
  x = 0;
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
template <class o>
void qw(o x) {
  if (x / 10) qw(x / 10);
  putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar(' ');
}
template <class o>
void pr2(o x) {
  if (x < 0) x = -x, putchar('-');
  qw(x);
  putchar('\n');
}
int n, a[N], lc[N], rc[N], sta[N], top, f[N];
int solve(int x, int l, int r, int last) {
  if (!x) return 0;
  return min(r - l + 1 - (last == a[x]) * f[x],
             solve(lc[x], l, x - 1, a[x]) + solve(rc[x], x + 1, r, a[x]) +
                 a[x] - last);
}
void dfs(int x) {
  if (!x) return;
  f[x] = 1;
  dfs(lc[x]);
  dfs(rc[x]);
  f[x] += f[lc[x]] * (a[x] == a[lc[x]]);
  f[x] += f[rc[x]] * (a[x] == a[rc[x]]);
}
int main() {
  qr(n);
  for (int i = 1; i <= n; i++) {
    qr(a[i]);
    while (top && a[sta[top]] > a[i]) lc[i] = sta[top--];
    if (top) rc[sta[top]] = i;
    sta[++top] = i;
  }
  dfs(sta[1]);
  pr2(solve(sta[1], 1, n, 0));
  return 0;
}
