atcoder#ABC277D. [ABC277D] Takahashi's Solitaire
[ABC277D] Takahashi's Solitaire
Score : points
Problem Statement
Takahashi has cards in his hand. For , the -th card has an non-negative integer written on it.
First, Takahashi will freely choose a card from his hand and put it on a table. Then, he will repeat the following operation as many times as he wants (possibly zero).
- Let be the integer written on the last card put on the table. If his hand contains cards with the integer or the integer written on them, freely choose one of those cards and put it on the table. Here, denotes the remainder when is divided by .
Print the smallest possible sum of the integers written on the cards that end up remaining in his hand.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
9 7
3 0 2 5 5 3 0 6 3
11
Assume that he first puts the fourth card ( is written) on the table and then performs the following.
- Put the fifth card ( is written) on the table.
- Put the eighth card ( is written) on the table.
- Put the second card ( is written) on the table.
- Put the seventh card ( is written) on the table.
Then, the first, third, sixth, and ninth cards will end up remaining in his hand, and the sum of the integers on those cards is . This is the minimum possible sum of the integers written on the cards that end up remaining in his hand.
1 10
4
0
20 20
18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12
99