Under construction

Still being built. Things may change

Why Hobanwoo was late for school 5

Time limit1sMemory limit1024 MB

Summary
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 NN times using NN magic cards.

  • Choose one of the remaining magic cards.
  • If the positive integer pair on the chosen card is a, ba,\,b, rise by aa to summon a meteorite, then descend by bb.
  • A used card burns away, so it cannot be used again.

The power of a meteor made from NN 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 NN cards are used as the ground, and calculates the power from there. No matter what order the NN 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 NN, the number of magic cards. (1≤N≤100 000)(1 ≤ N ≤ 100\,000)

From the second line, the next NN lines each contain a positive integer pair a, ba,\,b written on a magic card, separated by a space. (1≤a, b≤109)(1 ≤ a,\,b ≤ 10^{9})

Output

Print the maximum power of a meteor that can be made using all NN cards.

Examples2

  1. Example 1

    Input
    3
    3 6
    4 1
    2 3
    
    Expected output
    17
    
  2. Example 2

    Input
    9
    5 4
    13 19
    8 1
    2 6
    7 3
    3 6
    5 5
    9 2
    11 8
    
    Expected output
    201