Under construction

Still being built. Things may change

Lost Island

Time limit2sMemory limit512 MB

Summary
Given each eye color's true count a_i and the traveler's lower bound b_i, find the last day of suicides and the total number of people who die.
Level

Medium7 of 10

Topics
Sorting, Greedy, Math
Solved
No attempts yet

Problem

An island far away is home to a tribe. The tribe has nn eye colors. There are aia_i people with eye color ii (ai>0a_i > 0), and the tribe knows of no other colors. Strictly speaking, the tribe knows the value of nn but does not know that every ai>0a_i > 0.

The tribe follows a religion in which anyone who can deduce their own eye color commits suicide the next day. The people of the tribe are extremely clever.

On day 0, a traveler arrives on the island, meets the tribe, and makes nn true statements, where bi≥0b_i \ge 0 and at least one bi>0b_i > 0.

  • Wow, there are at least b1b_1 people with eye color 1!
  • Wow, there are at least b2b_2 people with eye color 2!
  • ...
  • Wow, there are at least bnb_n people with eye color nn!

Find the last day on which suicides take place, and the total number of people who commit suicide.

Input

The first line contains an integer nn (2≤n≤2000002 \le n \le 200000), the number of eye colors.

Each of the next nn lines contains two integers aia_i and bib_i (1≤ai≤1091 \le a_i \le 10^9, 0≤bi≤ai0 \le b_i \le a_i, at least one bib_i is greater than 0). Here aia_i is the number of people with eye color ii, and bib_i is the lower bound on that number stated by the traveler.

Output

Print two integers: the number of the last day on which suicides take place, and the total number of people who commit suicide.

Hint

Here is what happens in the first sample. The person with eye color 1 sees no one else with that color but hears that at least one person has it. They work out who that person could be and commit suicide on day 1.

Everyone else knows that only two colors exist and that one person with eye color 1 has died. They conclude that all of them have eye color 2 and commit suicide on day 2.

Examples3

  1. Example 1

    Input
    2
    1 1
    3 0
    
    Expected output
    2 4
    
  2. Example 2

    Input
    2
    3 1
    1 0
    
    Expected output
    4 4
    
  3. Example 3

    Input
    3
    3 1
    1 0
    1 0
    
    Expected output
    3 3