Square Graph
Time limit5sMemory limit256 MB
Given a sequence and a weight for each half-length k, find the minimum spanning forest weight of the graph built from squares whose two halves match.
- Level
Hard8 of 10
- Topics
- String matching, Union-find, Greedy, Sorting
- Solved
- No attempts yet
Problem
Prof. Elephant has a sequence . He used the sequence to generate an undirected graph with vertices labeled .
For each even-length contiguous subsequence , check whether holds for every . If it does, Prof. Elephant adds edges to . The -th edge connects vertices and , and its weight is .
Prof. Elephant wants to know the total weight of the minimum spanning forest of .
Input
There are multiple test cases. The first line contains an integer , the number of test cases. Each test case is given as follows.
The first line contains an integer ().
The second line contains integers ().
The third line contains integers ().
The sum of over all test cases does not exceed .
Output
For each test case, output one integer: the total weight of the minimum spanning forest of .