cswork / OAOpen practice workspace

UberOA #53

Minimum Operations to Make Sequence Harmonious

Estimated MediumJudge-ready

Topics (AI-labelled)DP Basics

The first number of a group states exactly how many group elements follow it. A sequence is valid if it can be split contiguously into several such groups; the empty sequence is also valid. Each operation deletes any one element. Find the minimum number of deletions.

This site has made the standard input and output explicit.

Input

The first line contains n, and the second line contains n integers; 1≤n≤200000, 1≤a[i]≤100000.

Output

Output one integer, the answer.

Example 1

Input:

7
3 7 2 6 2 4 4

Output:

0

Example 2

Input:

3
3 2 1

Output:

3

Example 3

Input:

7
1 8 4 5 2 6 1

Output:

0

Example notes

Example 1: it can be split into [3,7,2,6] and [2,4,4], so no deletion is needed. Example 2: deleting the leading 3 leaves [2,1], which is still too short; deleting 3 and 2 leaves [1], which is also too short, so the best is to delete all 3. Example 3: split into [1,8] and [4,5,2,6,1], so the answer is 0.

Editorial

Loading solution…