#P1527E. Partition Game
Partition Game
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 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 minimum.
The first line contains two integers , (, ).
The second line contains integers ().
Output the minimum sum of the cost of individual segments.
Input
The first line contains two integers , (, ).
The second line contains integers ().
Output
Output the minimum sum of the cost of individual segments.
Note
In the first example, we can divide the array into and . Cost of will be and cost of will be . Total cost would be .
In the second example, divide the array into and . Total Cost would be .