Dividing the Treasures
Time limit10sMemory limit1024 MB
Give each treasure to Anna, to Bruno, or to nobody so the two market totals differ by at most D and Bruno's preciousness minus Anna's is maximal.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Brute force, Sorting
- Solved
- No attempts yet
Problem
The thieves Anna and Bruno sneak into a rich man's mansion and find treasures, numbered treasure 1 through treasure . The two of them decide to divide the treasures. Anna takes some of the treasures, then Bruno takes some of the ones that are left. The two of them cannot take the same treasure. Anna and Bruno may each take no treasure at all. Whatever nobody takes stays in the mansion, so a treasure that neither of them takes is allowed.
Every treasure has two values, a market value and a preciousness. If the absolute difference between the total market value of the treasures Anna takes and the total market value of the treasures Bruno takes is at most , Anna considers the division fair and is satisfied. Bruno, on his side, wants treasures of greater preciousness than Anna's.
Divide the treasures so that Anna is satisfied. Find the maximum value of the total preciousness of the treasures Bruno takes minus the total preciousness of the treasures Anna takes.
Input
The input consists of lines.
The first line contains two integers and separated by a space (, ). There are treasures, and Anna is satisfied when the absolute difference between the total market value she takes and the total market value Bruno takes is at most .
Line of the following lines () contains two integers and separated by a space (, ). Treasure has market value and preciousness .
Output
Print in one line the maximum value of the total preciousness of the treasures Bruno takes minus the total preciousness of the treasures Anna takes, over all divisions that satisfy Anna.
Hint
In the first example, suppose Anna takes treasures 2, 3 and 5, and Bruno takes treasures 1 and 6. The total market value is 130 for Anna and 120 for Bruno. The absolute difference 10 is at most , so Anna is satisfied. The total preciousness is 400 for Anna and 1600 for Bruno, so the total preciousness Bruno takes minus the total preciousness Anna takes is 1200. That is the maximum.