#P12150. 【MX-X11-T4】「蓬莱人形 Round 1」视奸

【MX-X11-T4】「蓬莱人形 Round 1」视奸

Description

A set pair (A,B)(A, B) is defined as "good" if and only if AA can be transformed into BB through a finite number of the following operations:

  • Choose a number xx from AA, remove xx from AA, then add x1x-1 and x+1x+1 to AA. If duplicates exist, only one copy is retained.

Given that the initial value ranges of sets AA and BB are [1,n][1, n] (containing integers), operations may produce values outside [1,n][1, n]. Additionally, you are given the set BB and a length-nn array pp.

Find a valid AA such that (A,B)(A, B) is good, and minimize the sum i=1n[iA]×pi\sum\limits_{i=1}^n [i \in A] \times p_i.

Input Format

Multiple test cases.
The first line contains two integers cc and TT, representing the subtask number and the number of test cases, respectively. Subsequent lines contain the test data. Sample inputs satisfy c=0c=0.

For each test case:

  • The first line contains an integer nn, the initial value range of sets AA and BB.
  • The second line contains a length-nn binary string s1s2sns_1 s_2 \cdots s_n, where si=1s_i = 1 indicates iBi \in B, and si=0s_i = 0 indicates iBi \notin B.
  • The third line contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n.

Output Format

For each test case, output a single integer representing the minimal possible sum.

0 2
9
100110011
3 0 -1 -3 4 -1 -4 -3 -5
8
10100101
2 0 2 4 1 1 2 2
-4
2

Hint

Explanation #1

For the first test case, A={1,4,5,8,9}A = \{1, 4, 5, 8, 9\} is a valid solution. The cost is 3+(3)+4+(3)+(5)=43 + (-3) + 4 + (-3) + (-5) = -4.

For the second test case, A={2,7}A = \{2, 7\} is valid. Operating on 22 and 77 transforms AA into {1,3,6,8}\{1, 3, 6, 8\}, which matches BB. The cost is 0+2=20 + 2 = 2.

Constraints

For all test data: 1T101 \le T \le 10, 1n1051 \le n \le 10^5, 109pi109-10^9 \le p_i \le 10^9.

Subtask nn \le Special Property Points
1 18 None 10
2 10510^5 A
3 B 20
4 C
5 None 40
  • Special Property A: The size of set BB does not exceed 10.
  • Special Property B: The binary string ss contains no substring 101.
  • Special Property C: All pip_i are equal.

Translated by DeepSeek R1