#include <bits/stdc++.h>
using namespace std;
const long long N = 2e6 + 7;
const long long M = 1e4 + 7;
const long long INF = 1e9 + 7;
const long long Mod = 1e9 + 7;
long long a[N];
long long Solve(long long l, long long r) {
  if (l == r) {
    return min(a[l], 1ll);
  }
  long long mn = INF;
  for (long long i = l; i <= r; ++i) {
    mn = min(mn, a[i]);
  }
  long long res1 = r - l + 1;
  for (long long i = l; i <= r; ++i) a[i] -= mn;
  long long res2 = mn;
  for (long long i = l; i <= r; ++i) {
    if (a[i] != 0) {
      long long L = i, R = -1;
      for (long long j = i + 1; j <= r; ++j) {
        if (a[j] == 0) {
          R = j - 1;
          break;
        }
      }
      if (R == -1) R = r;
      res2 += Solve(L, R);
      i = R + 1;
    }
  }
  return min(res1, res2);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  cout << Solve(1, n);
}
