#include <bits/stdc++.h>
using namespace std;
class SegmentTree {
 public:
  pair<long long, long long> seg[4 * 500000 + 5];
  void build(long long id, long long l, long long r);
  pair<long long, long long> findMin(long long id, long long l, long long r,
                                     long long u, long long v);
};
long long n, cnt = 0;
long long a[500000 + 5];
SegmentTree st;
void SegmentTree::build(long long id, long long l, long long r) {
  if (l == r) {
    seg[id] = {a[l], l};
    return;
  }
  long long mid = (l + r) / 2;
  build(2 * id, l, mid);
  build(2 * id + 1, mid + 1, r);
  seg[id] = min(seg[2 * id], seg[2 * id + 1]);
  return;
}
pair<long long, long long> SegmentTree::findMin(long long id, long long l,
                                                long long r, long long u,
                                                long long v) {
  if (l > v || r < u) return {1000000000000000001, 1000000000000000001};
  if (l == r) return seg[id];
  if (u < l && r < v) return seg[id];
  long long mid = (l + r) / 2;
  pair<long long, long long> ret =
      min(findMin(2 * id, l, mid, u, v), findMin(2 * id + 1, mid + 1, r, u, v));
  return ret;
}
long long dp(long long x, long long y, long long val) {
  if (y < x) return 0;
  pair<long long, long long> tmp = st.findMin(1, 1, n, x, y);
  long long ans =
      min(y - x + 1, dp(x, tmp.second - 1, tmp.first) +
                         dp(tmp.second + 1, y, tmp.first) + tmp.first - val);
  return ans;
}
void input() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  return;
}
void solve() {
  st.build(1, 1, n);
  long long ptr1 = 0, ptr2 = 0;
  for (long long i = 1; i <= n; i++) {
    if (ptr1 == 0 && a[i] != 0) ptr1 = i;
    if (a[i] == 0) {
      ptr2 = i - 1;
      if (ptr1 != 0) cnt += min(dp(ptr1, ptr2, 0), ptr2 - ptr1 + 1);
      ptr1 = 0;
    }
    if (i == n && a[i] != 0) {
      ptr2 = i;
      cerr << ptr1 << " " << ptr2;
      if (ptr1 != 0) cnt += min(dp(ptr1, ptr2, 0), ptr2 - ptr1 + 1);
    }
  }
  return;
}
void output() {
  cout << cnt;
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  input();
  solve();
  output();
  return 0;
}
