luogu#P7791. [COCI 2014/2015 #7] TETA

[COCI 2014/2015 #7] TETA

Background

You find yourself playing the role of a kind lady working as a cashier in the cafeteria. One of the many reasons all students think she is a good person is that she cares about making you spend as little money as possible when you visit the cafeteria.

Problem Description

How does she do this? The strategy is actually very simple. In the cafeteria, you can buy various dishes, and their prices are well known. Every day, there is a set meal. A set meal includes 44 dishes (usually soup, main course, side dish, and dessert), but its price xx is less than or equal to the sum of the prices of its components. The lady has noticed that if you take some individual items that are part of the set meal, and if paying for a whole set meal would cost less, then she will definitely do so, and you will leave with a full tray and more money left in your pocket than before.

You are standing at the cashier with your tray, wondering how much you need to pay. Write a program to determine it.

Note: The lady may charge you using multiple set meals if that makes the total price cheaper.

Input Format

The input consists of six lines.

The first line contains an integer kk, the total number of dishes available in the cafeteria today.
The second line contains kk integers; the ii-th integer cic_i is the price of the ii-th dish.
The third line contains an integer xx, the price of the set meal.
The fourth line contains 44 integers p1,p2,p3,p4p_1,p_2,p_3,p_4, representing the 44 dishes included in the set meal. It is guaranteed that the pip_i are pairwise distinct.
The fifth line contains an integer tt, the number of dishes you ordered.
The sixth line contains tt integers; the ii-th integer sis_i is the index of the ii-th dish you ordered. The sis_i are not guaranteed to be pairwise distinct.

Output Format

Output a single line containing one integer, the amount of money you spend in the cafeteria today.

7
10 6 8 9 4 5 3
14
1 2 3 4
5
1 3 4 6 7
22
6
12 5 7 8 9 3
14
4 3 1 2
5
1 2 1 6 6
32

Hint

[Sample 1 Explanation]

You ordered dishes 1,3,41,3,4, which are in the set meal. The total price if bought separately is 10+8+9=2710+8+9=27, which is greater than the set meal price 1414, so the cashier lady will charge these dishes as 1414. The other two dishes 6,76,7 are not in the set meal, so they are charged separately. Therefore, the total cost is 14+5+3=2214+5+3=22.

[Sample 2 Explanation]

You ordered dishes 1,21,2, which are in the set meal. The total price if bought separately is 12+4=1612+4=16, which is greater than the set meal price 1414, so the cashier lady will charge these dishes as 1414. In addition, there is one more dish 11 and two dishes 66 left. Since the price of one dish 11 is 1212, which is less than the set meal price 1414, and the two dishes 66 do not appear in the set meal, the remaining one dish 11 and the two dishes 66 can only be charged separately. Therefore, the total cost is 14+12+3×2=3214+12+3\times 2=32.

[Constraints]

For all testdata, 1k,t201\leqslant k,t\leqslant 20, 1ci2501\leqslant c_i\leqslant 250, 1x<10001\leqslant x<1000, 1pi,sik1\leqslant p_i,s_i\leqslant k.

[Source]

This problem comes from COCI 2014-2015 CONTEST 7 T1 TETA, and uses the original testdata configuration, with a full score of 5050 points.

Translated and organized by Eason_AC.

Translated by ChatGPT 5