#include <bits/stdc++.h>
using namespace std;
const long long N = 100005;
const long long mod = 1000000007;
struct item {
  long long val, idx;
};
item tree[4 * N];
long long a[N];
long long n;
item merge(item t1, item t2) {
  item node;
  if (t1.val <= t2.val) {
    node.val = t1.val;
    node.idx = t1.idx;
  } else {
    node.val = t2.val;
    node.idx = t2.idx;
  }
  return node;
}
void build(int node, int start, int end) {
  if (start == end) {
    tree[node].val = a[start];
    tree[node].idx = start;
  } else {
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
  }
}
item query(int node, int start, int end, int left, int right) {
  if (right < start or end < left) {
    item xx;
    xx.val = INT_MAX;
    return xx;
  }
  if (left <= start and end <= right)
    return tree[node];
  else {
    int mid = (start + end) / 2;
    item x1 = query(2 * node, start, mid, left, right);
    item x2 = query(2 * node + 1, mid + 1, end, left, right);
    return merge(x1, x2);
  }
}
struct ANS {
  long long ans, cnt;
  ANS(int a, int b) {
    ans = a;
    cnt = b;
  }
};
ANS merge1(ANS a1, ANS a2) {
  ANS a3 = ANS(0, 0);
  a3.ans = a1.ans + a2.ans;
  a3.cnt = a1.cnt + a2.cnt;
  return a3;
}
int fun(int left, int right, int bal) {
  if (left > right)
    return 0;
  else if (left == right) {
    if (a[left] > bal)
      return 1;
    else
      return 0;
  }
  int aa = 0;
  int len = 0;
  item x = query(1, 1, n, left, right);
  if (x.val > bal) {
    aa += (x.val - bal);
    len = (right - left + 1);
    bal = x.val;
    aa += fun(left, x.idx - 1, bal);
    aa += fun(x.idx + 1, right, bal);
    return min(len, aa);
  } else if (x.val == bal) {
    aa += fun(left, x.idx - 1, bal);
    aa += fun(x.idx + 1, right, bal);
    return aa;
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  build(1, 1, n);
  int ans = fun(1, n, 0);
  cout << ans << endl;
}
