#P1361. 小M的作物

小M的作物

Description

Xiao M opened two huge farmlands AA and BB in MC (you can assume their capacity is infinite). Now, Xiao P has seeds of nn crop types, exactly 11 seed for each type (i.e., you can plant at most one plant of each type), numbered from 11 to nn.

For the ii-th crop, planting it in AA yields a profit of aia_i, and planting it in BB yields a profit of bib_i. There is also a special phenomenon: certain sets of crops planted together on the same field yield an extra profit. Xiao M found mm such crop combinations in total. For the ii-th combination, if all its crops are planted together in AA, you gain an extra c1,ic_{1,i} profit; if they are planted together in BB, you gain an extra c2,ic_{2,i} profit.

Xiao M quickly computed the maximum profit, but he wants to test you. Can you answer this question?

Input Format

The first line contains an integer nn, the number of crop types.

The second line contains nn integers, representing aia_i.

The third line contains nn integers, representing bib_i.

The fourth line contains an integer mm, the number of combinations.

Each of the next mm lines describes one combination: on the ii-th line, the first integer kik_i is the number of crops in the ii-th combination, followed by two integers c1,i,c2,ic_{1,i}, c_{2,i}, then kik_i integers indicating the indices of the crops in this combination.

Output Format

Output a single line containing one integer, the maximum profit.

3
4 2 1
2 3 2
1
2 3 2 1 2
11

Hint

Sample Explanation:

Plant crops 1,21, 2 in field AA, and crop 33 in field BB. Profit: 4+2+3+2=114+2+3+2=11.

Constraints:

For 100%100\% of the testdata, 1ki<n1031 \le k_i < n \le 10^3, 1m1031 \le m \le 10^3. All values that appear in the problem are non-negative integers not exceeding 10001000.

Translated by ChatGPT 5