cswork / OAOpen practice workspace

AmazonOA #69

Get Min Amount (Contiguous Quality)

Estimated HardJudge-ready

Topics (AI-labelled)Prefix & RangesHashingGreedy

One operation chooses x and y and changes every x in the array to y, at a cost equal to the current number of occurrences of x. The goal is for the occurrences of each remaining value to form one contiguous segment; find the minimum total cost.

Input

The first line contains n (1..200000), and the second line contains n quality values (−10⁹..10⁹).

Output

Output a single integer answer.

Example 1

Input:

7
7 7 5 7 3 5 3

Output:

4

Example 2

Input:

4
1 1 2 2

Output:

0

Example 3

Input:

3
1 2 1

Output:

1

Example notes

Example 1: three values interleave within the same closed block; keep the 7 that appears 3 times and change the other 4 positions to 7, cost 4. Example 2: every class is already contiguous, cost 0. Example 3: change the middle 2 to 1, cost 1.

Editorial

Loading solution…