This page is still under construction.

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

Passengers

Interview

Time limit1sMemory limit1024 MB

Summary
Given each request's row and earliest time, find the least total time for the attendant to serve all and return to row 1.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Sorting
Solved
No attempts yet

Problem

Every weekend a plane flies from Bitland to Vilnius. The passengers on this plane are very demanding and constantly trouble the crew: they keep asking for tea, a pillow, and so on.

While the crew tries to satisfy every request, the plane sometimes has to be kept circling above Vilnius before it can land! Naturally, the Bitland airline dislikes this, so from now on it asks its passengers to submit in advance a list of what they will request and when.

Given this list, find the minimum amount of time the flight attendant needs to fulfill all requests, assuming she plans her time optimally.

You also know that:

  • Moving between two adjacent rows takes 1 minute.
  • The flight attendant fulfills a request very quickly, so fulfilling a request is assumed to take no time (0 minutes).
  • The flight attendant starts the flight standing at the first row.
  • The flight attendant must finish the flight standing at the first row.

Input

The first line contains the number of requests NN.

Each of the next NN lines contains two integers aia_i and bib_i describing one passenger request. Here aia_i is the number of the row where the passenger sits, and bib_i is the earliest time at which the ii-th request is submitted (it may be fulfilled at that time or at any later time).

Output

Print a single integer — the minimum number of minutes needed for the flight attendant to fulfill all requests and return to the first row.

Constraints

  • 1≤N≤1051 \le N \le 10^5
  • 1≤ai,bi≤1061 \le a_i, b_i \le 10^6

Examples2

  1. Example 1

    Input
    3
    2 5
    3 3
    6 9
    
    Expected output
    14
    
  2. Example 2

    Input
    3
    1 1
    1 1
    1 5
    
    Expected output
    5