#P3411. 序列变换

序列变换

Description

Given a sequence AiA_i of length nn.

You may perform several operations. In each operation, you can choose any number in the sequence and move it to the beginning or to the end of the sequence.

Find the minimum number of operations needed to make the sequence non-decreasing. "Non-decreasing" means that every number in the sequence is not greater than the number that follows it.

Input Format

The first line contains a positive integer nn.

The second line contains nn positive integers AiA_i.

Output Format

Output a single integer, representing the minimum number of operations required.

5
6 3 7 8 6

2

Hint

For 30%30 \% of the testdata, 1n101 \le n \le 10.

For 60%60 \% of the testdata, 1n10001 \le n \le 1000.

For 100%100 \% of the testdata, 1n1061 \le n \le {10}^6, 1Ai1061 \le A_i \le {10}^6.

Translated by ChatGPT 5