NOI Contest Archive

Archive / 2020 / Selection Test

COVID-19 and Cards 1

Hard · National Olympiad in Informatics Sri Lanka - Screening Test 2020

Feeling lonely staying at home for a long time, Sumudu invented an interesting card game that she can play alone. The game is as follows:

Calculating the score:

Each card carries some points. The final score is the sum of points of the cards, or zero if the stack does not obey the following rules.

Rules:

  1. The ordering precedence of the cards is J > 9 > A > 10 > K > Q > 8 > 7 > 6 > 5 > 4 > 3 > 2
  2. A card of lower precedence cannot be placed on top of a card of higher precedence
  3. A red card cannot be followed by another red card, but a black card can be followed by another black card

Example points table:

image

Task:

Given 52 - T cards and the points awarded for each card, determine the maximum score that Sumudu can get.

Note: Order of precedence of cards is fixed (described above). But the points awarded for different cards will vary (given as input), per test case.

Input Format

The first line contains 13 integers ( Pi ), denoting the number of points for cards, from highest precedence to lowest precedence order.
Second-line contains an integer T, denoting the number of cards removed from the original deck.
The third line contains two space-separated integers R and B ; R is the number of remaining RED cards, B is the number of remaining BLACK cards.
The fourth line contains R space-separated strings, with the rank of each RED card.
The fifth line contains B space-separated strings, with the rank of each BLACK card.

Output Format

Print a single integer S, denoting the maximum score that Sumudu can get.

Constraints

Limits

Sample Input 0

60 55 50 40 35 25 20 18 14 10 8 5 2
47
3 2
A 9 4
J 10

Sample Output 0

163