Colourful Permutation Sorting

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

요약
각 위치에 색이 있고 원소 두 개를 S의 비용으로 교환하거나 한 색의 위치들을 C_i의 비용으로 마음대로 재배열할 수 있을 때, 순열을 정렬하는 최소 비용을 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 비트 연산, 그래프, 그리디
정답자
아직 제출이 없습니다

문제

You are given a permutation p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n of integers from 11 to nn. Each position from 11 to nn is colored in one of kk colors. We want to sort the permutation, and for that, we can apply any number of operations of the following types:

  • Swap any two elements. This operation costs SS coins;
  • Choose any color ii, and permute the elements on positions of color ii as you wish. This operation costs C_iC\_i coins.

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 TT (1≤T≤1031 \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 nn and kk (1≤n≤1051 \le n \le 10^{5}, 1≤k≤51 \le k \le 5) --- the size of the permutation and the number of colors.

The second line of each test case contains (k+1)(k+1) integers S,C_1,C_2,…,C_kS, C\_1, C\_2, \ldots, C\_k (0≤S,C_i≤1090 \le S, C\_i \le 10^9) --- the costs of the operations.

The third line of each test case contains nn integers p_1,p_2,…,p_np\_1, p\_2, \ldots, p\_n (1≤p_i≤n1 \le p\_i \le n, all p_ip\_i are distinct) --- the permutation.

The fourth line of each test case contains nn integers col_icol\_i (1≤col_i≤k1 \le col\_i \le k) --- the colors of the positions.

The sum of nn over all test cases in one file does not exceed 10510^{5}.

출력

For each test case print a single integer --- the minimum number of coins you need to spend to sort the permutation.

힌트

In the first test case, we can sort the permutation by applying the "Swap" operation 33 times: (2,3,4,1)→(4,3,2,1)→(4,2,3,1)→(1,2,3,4)(2, 3, 4, 1) \to (4, 3, 2, 1) \to (4, 2, 3, 1) \to (1, 2, 3, 4). This way you will spend 33 coins.

Another way to sort it would be to permute all elements on positions of color 11, but this would cost 1010 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 11, spending 11 coin on this.

In the third test case, one of the optimal sequences of operations would be the following:

  • Permute the elements on positions of color 22 to obtain the permutation (5,2,4,3,1,6)(5, 2, 4, 3, 1, 6). This operation costs 11 coin.
  • Swap elements p_3,p_4p\_3, p\_4. The permutation is now (5,2,3,4,1,6)(5, 2, 3, 4, 1, 6). This operation costs 1010 coins.
  • Permute the elements on positions of color 11 to obtain the permutation (1,2,3,4,5,6)(1, 2, 3, 4, 5, 6). This operation costs 11 coin.

In total, we spent 1212 coins.

In the fourth test case, the permutation is already sorted, so we don't have to spend anything.

예제1

  1. 예제 1

    입력
    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