Logo
(追記) (追記ここまで)

23701번 - Square Graph 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
5 초 (추가 시간 없음) 256 MB308422.222%

문제

Prof. Elephant has a sequence $a_1, a_2, \ldots, a_n$. He has used the sequence to generate an undirected graph $G$ with $n$ vertices labeled by 1,ドル 2, \ldots, n$.

For each even-length contiguous subsequence $a_l, a_{l + 1}, \ldots, a_{l + 2 k - 1},ドル if $a_{l + i - 1} = a_{l + k + i - 1}$ always holds for $i = 1, 2, \ldots, k,ドル Prof. Elephant would add $k$ edges to $G,ドル where the endpoints of the $i$-th edge are vertices labeled by $(l + i - 1)$ and $(l + k + i - 1),ドル and its weight is $w_k$.

Prof. Elephant would like to know the total weight of the minimum spanning forest of $G$.

입력

There are multiple test cases. The first line of the input contains an integer $T,ドル indicating the number of test cases. For each test case:

The first line contains an integer $n$ (2ドル \leq n \leq 3 \times 10^5$).

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ (1ドル \leq a_i \leq n$).

The third line contains $\lfloor\frac{n}{2}\rfloor$ integers $w_1, w_2, \ldots, w_{\lfloor\frac{n}{2}\rfloor}$ (1ドル \leq w_i \leq 10^9$).

It is guaranteed that the sum of $n$ in all test cases will not exceed 3ドル \times 10^5$.

출력

For each test case, output an integer denoting the total weight of the minimum spanning forest of $G$.

제한

예제 입력 1

1
8
2 2 5 6 2 5 6 2
5 1 4 4

예제 출력 1

21

힌트

출처

Contest > Open Cup > 2018/2019 Season > Stage 15: Grand Prix of China J번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /