Under construction

Still being built. Things may change

Relay

Interview

Time limit1sMemory limit1024 MB

Summary
Choose three distinct members and their running order to minimize the relay time, where each runner has a sprint time A and a baton time B.
Level

Medium6 of 10

Topics
Sorting, Dynamic programming
Solved
No attempts yet

Problem

JOI High School's track club has NN members, numbered from 1 to NN. Member ii (1≤i≤N1 \le i \le N) runs the 100 m sprint in AiA_i msec, and has baton control ability BiB_i.

The club will enter the 300 m relay at a national contest. Three members each run 100 m, and each runner passes the baton to the next. If the first runner is ii, the second is jj, and the third is kk (1≤i,j,k≤N1 \le i, j, k \le N), the relay goes as follows.

  1. Runner ii runs 100 m, taking AiA_i msec.
  2. Runner ii passes the baton to runner jj, taking max⁡(Bi,Bj)\max(B_i, B_j) msec.
  3. Runner jj runs 100 m, taking AjA_j msec.
  4. Runner jj passes the baton to runner kk, taking max⁡(Bj,Bk)\max(B_j, B_k) msec.
  5. Runner kk runs 100 m, taking AkA_k msec.

The record is therefore

Ai+max⁡(Bi,Bj)+Aj+max⁡(Bj,Bk)+AkA_i + \max(B_i, B_j) + A_j + \max(B_j, B_k) + A_k

As the coach, you choose three distinct members and decide their running order so that the record is as small as possible. Write a program that finds the minimum possible record for the 300 m relay.

Input

The first line contains NN. Each of the next NN lines contains AiA_i and BiB_i, separated by a space.

Output

Print one integer on a single line: the minimum possible record of the 300 m relay, in msec.

Constraints

  • 3≤N≤200 0003 \le N \le 200\,000
  • 1≤Ai≤1081 \le A_i \le 10^8 (1≤i≤N1 \le i \le N)
  • 1≤Bi≤1081 \le B_i \le 10^8 (1≤i≤N1 \le i \le N)

Examples3

  1. Example 1

    Input
    4
    1070 90
    1080 70
    1050 60
    1020 100
    
    Expected output
    3320
    
  2. Example 2

    Input
    5
    1000 28
    1000 14
    1000 21
    1000 20
    1000 14
    
    Expected output
    3034
    
  3. Example 3

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