#P1400E. Clear the Multiset

    ID: 5530 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>data structuresdivide and conquerdpgreedy*2200

Clear the Multiset

No submission language available for this problem.

Description

You have a multiset containing several integers. Initially, it contains a1a_1 elements equal to 11, a2a_2 elements equal to 22, ..., ana_n elements equal to nn.

You may apply two types of operations:

  • choose two integers ll and rr (lrl \le r), then remove one occurrence of ll, one occurrence of l+1l + 1, ..., one occurrence of rr from the multiset. This operation can be applied only if each number from ll to rr occurs at least once in the multiset;
  • choose two integers ii and xx (x1x \ge 1), then remove xx occurrences of ii from the multiset. This operation can be applied only if the multiset contains at least xx occurrences of ii.

What is the minimum number of operations required to delete all elements from the multiset?

The first line contains one integer nn (1n50001 \le n \le 5000).

The second line contains nn integers a1a_1, a2a_2, ..., ana_n (0ai1090 \le a_i \le 10^9).

Print one integer — the minimum number of operations required to delete all elements from the multiset.

Input

The first line contains one integer nn (1n50001 \le n \le 5000).

The second line contains nn integers a1a_1, a2a_2, ..., ana_n (0ai1090 \le a_i \le 10^9).

Output

Print one integer — the minimum number of operations required to delete all elements from the multiset.

Samples

Sample Input 1

4
1 4 1 1

Sample Output 1

2

Sample Input 2

5
1 0 1 0 1

Sample Output 2

3