cswork / OA打开练习工作台

UberOA #53

Minimum Operations to Make Sequence Harmonious

估计中等可在线判题

考点(AI 标注)动规入门

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.

输入格式

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

输出格式

Output one integer, the answer.

样例 1

输入:

7
3 7 2 6 2 4 4

输出:

0

样例 2

输入:

3
3 2 1

输出:

3

样例 3

输入:

7
1 8 4 5 2 6 1

输出:

0

样例说明

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.