atcoder#ABC282E. [ABC282E] Choose Two and Eat One
[ABC282E] Choose Two and Eat One
Score : points
Problem Statement
A box contains balls, each with an integer between and written on it. For , the integer written on the -th ball is .
While the box has two or more balls remaining, Takahashi will repeat the following.
- First, choose two balls arbitrarily.
- Then, get a score equal to the remainder when is divided by , where and are the integers written on the two balls.
- Finally, choose one of the two balls arbitrarily, eat it, and return the other to the box.
Print the maximum possible total score Takahashi will get.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
4 10
4 2 3 2
20
Consider the following scenario. Below, denotes the remainder when a non-negative integer is divided by a positive integer .
- Take the first and third balls from the box to score points. Then, eat the first ball and return the third to the box. Now, the box has the second, third, and fourth balls.
- Take the third and fourth balls from the box to score points. Then, eat the third ball and return the fourth to the box. Now, the box has the second and fourth balls.
- Take the second and fourth balls from the box to score points. Then, eat the second ball and return the fourth to the box. Now, the box has just the fourth ball.
Here, Takahashi scores a total of points, which is the maximum possible value.
20 100
29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8
1733