#P11311. 漫长的小纸带

漫长的小纸带

Description

After years of brute force training, Little ζζ has mastered the art. During SPSC 2077, he was delighted to find that the second problem, “The Long Paper Tape,” was a challenging one - perfect for a brute force specialist like him.

This is a multiple test case problem with n n test cases in one test point. The size of the i-th test case is aia_i. He wrote a brute force program where, for a continuous segment of data, the time needed to solve that segment is the square of the number of different aia_i values in that segment. Formally, for a continuous segment from the l-th to the r-th test case, let S={ailir} S = \{a_i|l ≤ i ≤ r\}, the program needs S2 |S|^2 time to solve them together.

Now, given n n and the sizes of n n test cases, find a way to divide these cases into several segments so that the total time consumed by the program is minimized.

Input Format

The first line contains an integer nn .

The next line contains nn integers aia_i, representing the size of each test case.

Output Format

Output one number representing the minimum total time consumption.

5
1 2 3 4 5
5
6
1 2 2 1 2 3
5
9
1 2 1 4 1 2 1 1 2
8
21
1 2 1 2 1 2 1 2 1 2 3 4 5 6 7 1 2 1 2 1 2
13

Hint

「Sample 3 Explanation」

The segments are:

  • First segment {1}\{1\}, cost = 1 1 ;
  • Second segment {2}\{2\}, cost = 1 1 ;
  • Third segment {1}\{1\}, cost = 11;
  • Fourth segment {4}\{4\}, cost = 11;
  • Fifth segment {1,2,1,1,2}\{1, 2, 1, 1, 2\}, cost = 44;

Therefore, the total time consumption is 88.

「Data Constraints」

For 100% of test cases:1n2×105 1 \le n \le 2 \times 10^5 1ai109 1 \le a_i \le 10^9

Subtask ID n n \le Special Property pts
1 1 10 10 - 8 8
2 2 300 300
3 3 2000 2000 16 16
4 4 - A
5 5 B 24 24
6 6 - 28 28

Special Property A: All aia_i are randomly generated with uniform distribution in range [1,109][1, 10^9], and this subtask has only one test point.

Special Property B: 1ai10001 ≤ a_i ≤ 1000.