Problem J
Trading Card Game
At the Trading Card Shop, players are gathering to duel and
improve their decks.
There are $N$ players. The
$i_{th}$ player is
characterized by two values:
-
$C_i$: the number of coins they have
-
$P_i$: their power level
After each duel, the player with the higher power level wins
(if both players have the same power level, the player with the
lower index wins). The winner takes all of the loser’s coins
(i.e. $C_{\text{winner}}' =
C_{\text{winner}} + C_{\text{loser}}$). The loser is
eliminated and may not compete in any more duels.
The shop has another rule stating that only players with
similar power levels can duel each other. Specifically, two
players may duel only if the difference in their power levels
is at most $M$.
As the shop owner, you get to decide how many duels occur, who
is involved in each duel, and in what order the duels happen.
You like the idea of seeing someone go home with a huge bag of
coins. Out of all possible orderings of the duels, what is the
maximum number of coins held by a single player after all duels
have finished?
Input
The first line of input contains two integers $N$ and $M$ ($1 \leq N \leq 1000$, $0 \leq M \leq 10^9$). For the next $N$ lines, the $i_{th}$ line contains $C_i$ and $P_i$ ($0 \leq C_i \leq 10^5$), ($0 \leq P_i \leq 10^9$).
Output
Print the maximum number of coins held by a single player after all duels have finished.
| Sample Input 1 | Sample Output 1 |
|---|---|
3 100 10 5 20 6 30 7 |
60 |
| Sample Input 2 | Sample Output 2 |
|---|---|
3 1 10 5 20 10 30 11 |
50 |
