#include <bits/stdc++.h>
using namespace std;
int height[50005];
int ans = 0;
int dfs(int left, int right, int t) {
  if (left == right) return min(height[left] - t, 1);
  if (left > right) return 0;
  int ans = left;
  for (int i = left; i <= right; ++i) {
    if (height[i] < height[ans]) ans = i;
  }
  return min(right - left + 1, height[ans] - t +
                                   dfs(left, ans - 1, height[ans]) +
                                   dfs(ans + 1, right, height[ans]));
}
int main(int argc, char const *argv[]) {
  int k;
  cin >> k;
  for (int i = 1; i <= k; ++i) {
    cin >> height[i];
  }
  cout << dfs(1, k, 0) << endl;
  return 0;
}
