Why Hobanwoo was late for school 5
Time limit1sMemory limit1024 MB
Given N cards with (a, b), find the order of use that maximizes the sum of meteor heights measured from the lowest point of the flight path.
- Level
Medium7 of 10
- Topics
- Sorting, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
Hobanwoo finally reached the Demon King's castle. He decided to wipe out the Demon King's soldiers in one blow with a meteor, made with magic cards that summon the meteorite he got earlier in the Village of Beginnings.
At first, Hobanwoo flies as high into the sky as he can. Starting from that position, he repeats the following action times using magic cards.
- Choose one of the remaining magic cards.
- If the positive integer pair on the chosen card is , rise by to summon a meteorite, then descend by .
- A used card burns away, so it cannot be used again.
The power of a meteor made from cards is the sum of the heights of its meteorites above the ground. However, Hobanwoo rose so high at the start that he cannot calculate the power.
So he takes the lowest point along the path after all cards are used as the ground, and calculates the power from there. No matter what order the cards are used in, he rises high enough that he cannot reach the ground.
Help Hobanwoo make the meteor with the greatest power from the cards.
Input
The first line contains , the number of magic cards.
From the second line, the next lines each contain a positive integer pair written on a magic card, separated by a space.
Output
Print the maximum power of a meteor that can be made using all cards.