아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Cooperation Game

시간 제한3초메모리 제한1024 MB

요약
반 번호가 붙은 학생들이 일렬로 서 있을 때, 같은 반 학생 두 명을 뽑아 나가기 직전 두 사람 사이의 거리를 점수로 더한다. 얻을 수 있는 최대 점수를 구한다.
난이도

보통10점 중 7점

유형
그리디, 스택, 구현, 배열
정답자
아직 제출이 없습니다

문제

Athletic contests in schools sometimes get too heated. To promote cooperation, the teachers have devised a game where cooperation leads to better scores.

This cooperation game starts with NN students standing in a line. To get a score, two students from the same class will get out of the line. If they were ii-th and jj-th students along the line, just before they went out, they add ∣i−j∣|i - j| to the total score. The empty places of the two students that just came out will be compacted. The game stops when there are no more pairs of students from the same class.

For example, consider six students initially standing in a line:

1,2,3,3,2,11, 2, 3, 3, 2, 1

The numbers are class numbers of the students. If the students come out from the line in the order of class number 11, then class number 22, then class number 33, the total score is 5+3+1=95 + 3 + 1 = 9. However, in the same initial situation, if the students come out from the line in the order of 33, 22, 11, then the total score is 1+1+1=31 + 1 + 1 = 3.

Given the class numbers in the initial line, write a program to calculate the maximum possible score.

입력

The first line contains an integer TT, the number of test cases (1≤T≤481 \le T \le 48). The test cases follow.

The first line of each test case contains a single integer NN, the number of students (1≤N≤300,0001 \le N \le 300\\,000).

In the next line, the sequence of class numbers are given. The class numbers are integers from 11 to NN.

The sum of NN over all test cases does not exceed 7,000,0007\\,000\\,000.

출력

For each test case, print a line with a single integer: the maximum possible score.

예제1

  1. 예제 1

    입력
    2
    7
    1 2 1 1 2 1 2
    12
    1 2 3 1 2 3 1 2 3 1 2 3
    
    예상 출력
    10
    30