#P11727. Card Matching Game – Maximizing Score
Card Matching Game – Maximizing Score
Card Matching Game – Maximizing Score
There are \(2N\) cards laid out in order from left to right with indices \(1,2,\ldots,2N\). Each card \(i\) has an integer \(A_i\) written on it. For every \(x=1,2,\ldots,N\) there exist exactly two cards with the integer \(x\).
Bitaro and Beaver decide to play a game called "Nerve Weakness" using these cards. The game is played as follows. For \(i=1,2,\ldots,2N\) (considering the cards in order):
- Bitaro decides whether to pick up card \(i\). If he decides not to pick it, the card is skipped; otherwise, the following steps are executed.
- If Bitaro currently holds a card with the integer \(A_i\) (in either hand), then that card and card \(i\) vanish simultaneously, and he gains \(V_{A_i}\) points.
- If there is a card in his left hand, he discards it.
- If there is a card in his right hand, he transfers it to his left hand.
- If card \(i\) did not vanish in step 2, he places it in his right hand.
The sum of the points obtained in the game is Bitaro's final score. Determine the maximum score Bitaro can achieve by choosing optimally which cards to pick and which to skip.
Note: All formulas are presented in \(\LaTeX\) format.
inputFormat
The input is given as follows:
- The first line contains a single integer \(N\).
- The second line contains \(2N\) integers \(A_1, A_2, \ldots, A_{2N}\) representing the numbers on the cards in order.
- The third line contains \(N\) integers \(V_1, V_2, \ldots, V_N\) where \(V_x\) is the score gained when the pair of cards with integer \(x\) is matched.
outputFormat
Output a single integer: the maximum score Bitaro can achieve.
sample
1
1 1
10
10