#ds603. Partition Game
Partition Game
Partition Game
Problem Source
Codeforces Round #721 (Div. 2) E
Problem Description
You are given an array of integers. Define the cost of some array as follows:
where is the set of all values in without repetitions, and and are the indices of the first and last occurrence of in , respectively. In other words, we compute the distance between the first and last occurrences for each distinct element and sum them up.
You need to split the array into consecutive segments such that each element of belongs to exactly one segment and the sum of the cost of individual segments is minimized.
Input Format
The first line contains two integers ().
The second line contains integers ().
Output Format
Output the minimum sum of the cost of individual segments.
Sample Input 1
7 2
1 6 6 4 6 6 6
Sample Output 1
3
Sample Input 2
7 4
5 5 5 5 2 3 3
Sample Output 2
1
Sample Explanation
In the first example, we can divide the array into and . The cost of is and the cost of is . The total cost is .
In the second example, divide the array into and . The total cost is .