시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB26221982.609%

문제

There are $N$ members in the track club of JOI High School, numbered from $1$ to $N$. For the member $i$ ($1 ≤ i ≤ N$), the time of the 100 m sprint is $A_i$ msec, and the ability of controlling the baton is $B_i$.

The track club will participate in a national contest for the 300 m relay. Three members will run in the 300 m relay. Each member will run for 100 m. A runner will pass the baton to the next runner. Precisely, if the first runner is the member $i$ ($1 ≤ i ≤ N$), the second runner is the member $j$ ($1 ≤ j ≤ N$), and the third runner is the member $k$ ($1 ≤ k ≤ N$), then the three members will run the relay in the following steps.

  1. The member $i$ runs for 100 m with a baton. It takes $A_i$ msec.
  2. The member $i$ gives a baton to the member $j$. It takes $\max{(B_i , B_j)}$ msec.
  3. The member $j$ runs for 100 m with a baton. It takes $A_j$ msec.
  4. The member $j$ gives a baton to the member $k$. It takes $\max{(B_j , B_k)}$ msec.
  5. The member $k$ runs for 100 m with a baton. It takes $A_k$ msec.

Therefore, the record for the 300 m relay is $A_i + \max{(B_i , B_j)} + A_j + \max{(B_j , B_k)} + A_k$ msec. Here $\max{(x, y)}$ is the largest value of $x$ and $y$. Since you are the coach of the track club, you want to choose three distinct members for the relay and decide the order of the runners so that the record becomes minimum.

Write a program which, given information of the $N$ members of the track club, calculates the minimum possible record of the 300 m relay.

입력

Read the following data from the standard input. Given values are all integers.

$N$

$A_1$ $B_1$

$A_2$ $B_2$

$\vdots$

$A_N$ $B_N$

출력

Write one line to the standard output. The output should contain an integer which is the minimum possible record of the 300 m relay counted by msec.

제한

  • $3 ≤ N ≤ 200\,000$.
  • $1 ≤ A_i ≤ 100\,000\,000 (= 10^8)$ ($1 ≤ i ≤ N$).
  • $1 ≤ B_i ≤ 100\,000\,000 (= 10^8)$ ($1 ≤ i ≤ N$).

서브태스크

번호배점제한
125

$N ≤ 100$.

233

$N ≤ 3\,000$.

310

$A_1 = A_2 = \cdots = A_N$.

432

No additional constraints.

예제 입력 1

4
1070 90
1080 70
1050 60
1020 100

예제 출력 1

3320

If the first runner is the member 4, the second runner is the member 3, and the third runner is the member 2, then the time for the steps 1, 2, 3, 4, 5 of the relay will be 1020, 100, 1050, 70, 1080 msec, respectively. Thus, the record for the 300 m relay will be 1020 + 100 + 1050 + 70 + 1080 = 3320 msec. Since it is the minimum possible record, output 3320.

This sample input satisfies the constraints of Subtasks 1, 2, 4.

예제 입력 2

5
1000 28
1000 14
1000 21
1000 20
1000 14

예제 출력 2

3034

If the first runner is the member 2, the second runner is the member 5, and the third runner is the member 4, then the time for the steps 1, 2, 3, 4, 5 of the relay will be 1000, 14, 1000, 20, 1000 msec, respectively. Thus, the record for the 300 m relay will be 1000+14+1000+20+1000 = 3034 msec. Since it is the minimum possible record, output 3034.

This sample input satisfies the constraints of all the subtasks.

예제 입력 3

9
3 1
4 1
5 9
2 6
5 3
5 8
9 7
9 3
2 3

예제 출력 3

13

If the first runner is the member 1, the second runner is the member 2, and the third runner is the member 9, then the time for the steps 1, 2, 3, 4, 5 of the relay will be 3, 1, 4, 3, 2 msec, respectively. Thus, the record for the 300 m relay will be 3 + 1 + 4 + 3 + 2 = 13 msec. Since it is the minimum possible record, output 13.

This sample input satisfies the constraints of Subtasks 1, 2, 4.

채점 및 기타 정보

  • 예제는 채점하지 않는다.