Problem list
Log in
#coci00352 · COCI 2014/2015 Contest 7

Teta

Silver IV 1.0 s 64 MB

You have found yourself in the role of a nice lady working as a cashier in a canteen. One of the multiple reasons why the lady is considered nice by all students is because of her concern that you spend as less as possible when visiting the canteen. How does she do that? Well, the strategy is really simple. Various meals can be bought in the canteen and their prices are known. Each day, a menu is offered. A menu includes 4 meals (usually it’s soup, main course, side dish and dessert), but it’s price is less than or equal to the sum of prices of its components. When the lady notices that you’d spend less money if she charged you with an entire menu instead of individual things from the menu you took, then she will do so, and you will leave full and with more money in your pocket. You are standing in front of the cash register with your tray and want to know how much you have to pay. Write a programme to determine it! Please note: The lady can charge you with multiple menus if that comes out cheaper.

Input

The first line of input contains the integer $K$ (1 ⩽$K$ ⩽20), the number of meals that can be bought in the canteen. For simplicity’s sake, we will denote the meals with integers from 1 to $K$. The second line of input contains $K$ integers, the $i$^{th} number representing the price of the meal denoted with $i$. The prices will be in range [1, 250]. The third line of input contains the integer $X$ (1 ⩽$X < 1000$), the menu price. The following line of input contains 4 integers, the labels of the meals from the menu, different from each other. The fourth line of input contains the integer $T$ (1 ⩽$T$ ⩽20), the number of meals on your tray. The following line contains the list of meals you’ve taken. Not all meals on the tray have to be distinct, it is possible to take multiple portions of a meal.

Output

The first and only line of output must contain an integer, the cost of your meal.

Example 1

Input
7
10 6 8 9 4 5 3
14
1 2 3 4
5
1 3 4 6 7
Output
22

Example 2

Input
6
12 5 7 8 9 3
14
4 3 1 2
5
1 2 1 6 6
Output
32
Source
COCI 2014/2015 Contest 7
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code