Fortress Defense
InterviewTime limit2sMemory limit512 MB
Distribute exactly s defenders among wall sections, where each defender in section i repels k_i attackers, to minimize the total attackers that break through.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Math, Binary search
- Solved
- No attempts yet
Problem
The wall of a besieged fortress consists of sections numbered from 1 to . Reconnaissance reports that in the next assault the enemy will send soldiers to attack section . To defend the fortress, defenders will be sent to the sections of the wall.
The sections differ in the quality of their fortifications, which makes defense effectiveness differ as well. One defender in section can repel the attack of attackers.
Suppose defenders are sent to section . If the number of attackers does not exceed , then no attacker breaks into the fortress through this section. Otherwise, attackers break into the fortress.
Write a program that distributes the defenders among the sections so that their total number equals and as few attackers as possible break into the fortress.
Input
The first line contains integers , the number of sections of the wall, and , the number of defenders of the fortress (; ).
The next lines contain two integers each, and , the total number of attackers on section of the wall and the number of attackers that one defender of this section can repel ().
Output
The output must contain a single integer: the minimum number of attackers that break into the fortress.
Hint
In the first test, if all 10 defenders are placed on the only section, they can repel all the attackers and nobody gets into the fortress. In the second sample, one can, for example, send two defenders to the first section and one to the third.