#include <bits/stdc++.h>
inline bool isdig(const char &c) { return c >= '0' && c <= '9'; }
inline char gc(void) {
  static char buf[100000], *p1 = buf, *p2 = buf;
  if (p1 != p2) return *p1++;
  p1 = buf;
  p2 = p1 + fread(buf, 1, 100000, stdin);
  return p1 == p2 ? EOF : *p1++;
}
template <typename T>
inline void read(T &num) {
  bool neg = false;
  char c = gc();
  num = (T)0;
  while (!isdig(c)) {
    neg |= (c == '-');
    c = gc();
  }
  while (isdig(c)) {
    num = num * 10 + c - '0';
    c = gc();
  }
  num = neg ? -num : num;
  return;
}
template <typename T>
inline T min(const T &a, const T &b) {
  return a < b ? a : b;
}
const int N = 500005;
const long long inf = (1LLU << 63) - 1;
long long a[N];
long long solve(int l, int r, int h) {
  if (l == r) return 1;
  long long ans, minh = inf;
  int nr = 0;
  for (register int i = l; i <= r; ++i) minh = min(minh, a[i]);
  ans = minh - h;
  for (int i = l; i <= r; ++i) {
    if (a[i] == minh) continue;
    for (int j = i; j <= r; ++j) {
      nr = j;
      if (j == r || a[j + 1] == minh) break;
    }
    ans += solve(i, nr, minh);
    i = nr + 1;
  }
  return min(ans, r - l + 1LL);
}
int main() {
  int n;
  read(n);
  for (register int i = 1; i <= n; ++i) read(a[i]);
  printf("%lld", solve(1, n, 0));
  return 0;
}
