Problem:
Score : 1400 points

Problem Statement

Snuke has a sequence of N integers x_1,x_2,\cdots,x_N.
Initially, all the elements are 0.

He can do the following two kinds of operations any number of times in any order:

Operation 1: choose an integer k (1 \leq k \leq N) and a non-decreasing sequence of k non-negative integers c_1,c_2,\cdots,c_k.
Then, for each i (1 \leq i \leq k), replace x_i with x_i+c_i.

Operation 2: choose an integer k (1 \leq k \leq N) and a non-increasing sequence of k non-negative integers c_1,c_2,\cdots,c_k.
Then, for each i (1 \leq i \leq k), replace x_{N-k+i} with x_{N-k+i}+c_i.

His objective is to have x_i=A_i for all i.
Find the minimum number of operations required to achieve it.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the minimum number of operations required to achieve Snuke's objective.

Sample Input 1

5
1 2 1 2 1

Sample Output 1

3

One way to achieve the objective in three operations is shown below.
The objective cannot be achieved in less than three operations.

Do Operation 1 and choose k=2,c=(1,2). Now we have x=(1,2,0,0,0).

Do Operation 1 and choose k=3,c=(0,0,1). Now we have x=(1,2,1,0,0).

Do Operation 2 and choose k=2,c=(2,1). Now we have x=(1,2,1,2,1).

Sample Input 2

5
2 1 2 1 2

Sample Output 2

2

Sample Input 3

15
541962451 761940280 182215520 378290929 211514670 802103642 28942109 641621418 380343684 526398645 81993818 14709769 139483158 444795625 40343083

Sample Output 3

7

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else 
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> bool chmax(T& t, const U& u) { if (t < u) { t = u; return 1; } return 0; }
template<class T, class U> bool chmin(T& t, const U& u) { if (t > u) { t = u; return 1; } return 0; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

const int INF = TEN(9);

int main() {
	int N; cin >> N;
	V<int> A;
	A.reserve(N + 2);
	A.pb(0);
	rep(i, N) { int x; cin >> x; A.pb(x); }
	A.pb(0);
	N++;

	int dp0 = 0;
	V<int> pos({0});

	for (int i = 1; i <= N; ++i) {
		V<int> ns({A[i]});
		ns.pb(A[i-1]);
		if (A[i-1] > 0) {
			ns.pb(A[i-1]-1);
		}
		rep(j, pos.size()) {
			ns.pb(pos[j]);
			if (pos[j] > 0) {
				ns.pb(pos[j]-1);
			}
			if (pos[j] + A[i] - A[i-1] > 0) {
				ns.pb(pos[j] + A[i] - A[i-1]);
				ns.pb(pos[j] + A[i] - A[i-1] + 1);
			}
		}
		sort(ALL(ns));
		ns.erase(unique(ALL(ns)), ns.end());
		V<int> dp(ns.size(), INF);

		rep(j, ns.size()) if (ns[j] <= A[i-1]) {
			int p = upper_bound(ALL(pos), ns[j]) - pos.begin() - 1;
			rep(k, ns.size()) if (ns[k] <= A[i]) {
				int val = dp0 - p + (j > k) + (A[i-1] - ns[j] < A[i] - ns[k]);
				chmin(dp[k], val);
			}
		}
		dp0 = dp[0];
		V<int> npos({0});
		for (int j = 1; j < ns.size(); ++j) {
			if (dp[j] != INF && dp[j] != dp[j-1]) {
				npos.pb(ns[j]);
			}
		}
		pos = npos;
		dump(dp0);
		dump(pos);
	}
	cout << dp0 + 1 - pos.size() << endl;

	return 0;
}