| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 2 초 | 1024 MB | 15 | 8 | 8 | 57.143% |
You are given a permutation $p_1, p_2, \ldots, p_n$ of integers from 1ドル$ to $n$. Each position from 1ドル$ to $n$ is colored in one of $k$ colors. We want to sort the permutation, and for that, we can apply any number of operations of the following types:
Note that the positions are colored, not the elements, so when you swap two elements, the positions won't change their colors.
Find the minimum number of coins you need to spend to sort the permutation.
The first line of the input contains a single integer $T$ (1ドル \le T \le 10^{3}$) --- the number of independent test cases you need to process. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ (1ドル \le n \le 10^{5},ドル 1ドル \le k \le 5$) --- the size of the permutation and the number of colors.
The second line of each test case contains $(k+1)$ integers $S, C_1, C_2, \ldots, C_k$ (0ドル \le S, C_i \le 10^9$) --- the costs of the operations.
The third line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ (1ドル \le p_i \le n,ドル all $p_i$ are distinct) --- the permutation.
The fourth line of each test case contains $n$ integers $col_i$ (1ドル \le col_i \le k$) --- the colors of the positions.
The sum of $n$ over all test cases in one file does not exceed 10ドル^{5}$.
For each test case print a single integer --- the minimum number of coins you need to spend to sort the permutation.
4 4 1 1 10 2 3 4 1 1 1 1 1 4 1 10 1 2 3 4 1 1 1 1 1 6 2 10 1 1 5 2 4 6 1 3 1 2 1 2 1 2 4 3 6 7 8 9 1 2 3 4 2 2 3 2
3 1 12 0
In the first test case, we can sort the permutation by applying the "Swap" operation 3ドル$ times: $(2, 3, 4, 1) \to (4, 3, 2, 1) \to (4, 2, 3, 1) \to (1, 2, 3, 4)$. This way you will spend 3ドル$ coins.
Another way to sort it would be to permute all elements on positions of color 1ドル,ドル but this would cost 10ドル$ coins, and we can do cheaper.
In the second test case (which differs from the first one only in the costs of operations), however, it's cheaper to just permute all elements on positions of color 1ドル,ドル spending 1ドル$ coin on this.
In the third test case, one of the optimal sequences of operations would be the following:
In total, we spent 12ドル$ coins.
In the fourth test case, the permutation is already sorted, so we don't have to spend anything.