This page is still under construction.

Parts of this page are still being built. What you see may change.

Elevator

Interview

Time limit1sMemory limit512 MB

Summary
Given passenger arrival times and destination floors, choose when to dispatch the elevator to minimize the time it finally returns to floor 0.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Binary search, Array
Solved
No attempts yet

Problem

You have a very important job: you are responsible for an elevator in a new skyscraper.

There are nn people who will come to the underground parking garage on floor 00 and wait for an elevator to take them to some upper floor. Formally, the ii-th person comes to the elevator at moment tit_i and wants to reach floor aia_i. The elevator has infinite capacity, so there is no limit on the number of people using the elevator at any moment. All tit_i are distinct. Passengers always enter the elevator while it is at floor 00.

The elevator uses the following algorithm: it stays open on floor 00 until you send it to deliver passengers, then it moves to the highest floor it needs to reach (the maximum aia_i among all passengers currently in the elevator), dropping passengers off along the way, and returns to the parking garage. The elevator spends 11 unit of time to move to the next floor (or to the previous floor). The time spent opening and closing the elevator doors and letting passengers enter and leave is negligible. At moment 00, the elevator is at floor 00.

You want to minimize the moment of time when the elevator will return to floor 00 after delivering everyone.

Input

The input contains one or more test cases.

The first line of each test case contains one integer nn: the number of passengers (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5).

Each of the following nn lines contains two space-separated integers tit_i and aia_i: the moment of time when the ii-th passenger comes to the elevator, and the destination floor of the ii-th passenger (1≤ti,ai≤1091 \le t_i, a_i \le 10^9).

All tit_i in one test case are distinct, and passengers appear in the input in ascending order of tit_i.

The sum of the values of nn over all test cases does not exceed 2⋅1052 \cdot 10^5. The test cases just follow one another without any special separators.

Output

For each test case, print one integer: the minimum possible moment of time when the elevator will return after delivering all passengers.

Examples1

  1. Example 1

    Input
    3
    1 9
    2 6
    15 6
    3
    1 9
    2 6
    15 8
    
    Expected output
    31
    33