Card Exchange
Time limit1sMemory limit1024 MB
Given N card values with point values A_i and M turns of trading cards with a dealer, find the maximum score the player can end with.
- Level
Medium6 of 10
- Topics
- Greedy, Dynamic programming, Sorting
- Solved
- No attempts yet
Problem
When the Inha University festival opens, Inha Casino, the largest casino in Incheon, opens too. Hoping to draw more guests, Inha Casino created a new card game that everyone can enjoy. The game works as follows.
The dealer and the player each receive one card for every integer from to , with that integer written on it. The dealer also receives one blank dummy card. The player starts with a score of and takes turns. Each turn proceeds in this order.
- The player takes any one card they want from the dealer's hand.
- If the player's hand now contains a pair of cards with the same value, the pair is removed from the hand. If the value on the pair is , the player gains points. The gained points can be negative, and the player's score can also become negative.
- The player gives the dealer any one card they want from their hand.
- If the dealer's hand now contains a pair of cards with the same value, the pair is removed from the hand. In this case the player gains no points.
The game ends right after the -th turn, or as soon as every pair except the dummy card is gone. If the player cannot give a card to the dealer during a turn, the game ends immediately.
Write a program that finds the maximum score the player can have when the game ends.
Input
The first line contains the number of cards and the maximum number of turns , separated by a space.
The second line contains the points that the player gains when a pair with that value is formed, separated by spaces.
Output
Print the maximum score the player can earn.
Constraints
- is an integer.
Hint
The example below differs from the situation in the sample input and output, so take care with it.
With , the player picks a card with a number written on it, not the dummy card, on the first turn.





If the player takes the dummy card from the dealer's hand, or gives the dummy card to the dealer, no pair forms on the receiving side, so nothing happens.
The example below shows . On the first turn, the player receives the dummy card from the dealer and then gives that same dummy card back.


