Water Knows The Answers

N개의 직사각형을 회전 여부를 정해 지면에 나란히 배치하고, 상자 사이에 고이는 빗물의 최대 넓이를 구한다. 총 N+1 줄: 첫 줄에 N, 다음 N줄에 각 상자의 너비 w_i와 높이 h_i가 주어진다. 최대 저수 면적을 정수로 출력한다. N은 최대 250,000, w_i와 h_i는 최대 10^6이다.

어려움8그리디정렬스택아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Can water think? Can it learn? After reading the book Water Knows the Answers — the Hidden Messages in Water Crystals, you may believe that water can reflect human emotions and has the power to listen, see, and know the answers to life.

— Review of the book Water Knows the Answers, by Masaru Emoto

Figure: The cover of the book Water Knows the Answers — the Hidden Messages in Water Crystals

Donghyun lives in a 2D plane, having xx-axis as ground. It rains a lot in the 2D plane, and the rain falls from y=+y = +\infty.

Recently, Donghyun read a book called Water Knows the Answers — the Hidden Messages in Water Crystals, and got impressed by the book. He now believes that water will tell him the answers to life.

Donghyun has NN rectangular boxes with (possibly) different heights and widths. He is going to rearrange those boxes in order to collect the rainfall. Then, the water will give him the answers to life. To achieve this, he is going to place the box in a row. No empty space is allowed between the boxes. He may rotate some of the boxes or not, but every box must have an edge touching the ground.

The water can flow left or right until it has no space to flow to. Formally, water in certain point can stay in its place if that point is not inside the box and has a box both somewhere to its left and somewhere to its right at the same height.

The figure below shows a possible example. The gray rectangles represent boxes and the blue regions represent water.

Donghyun wants to know the maximum area of water he can store. (In the 2D plane, area means volume.) But, he doesn't have water yet, so he doesn't know the answer. Do you know the answer?

입력

Input consists of N+1N+1 lines.

In the first line, the number of boxes NN is given.

Next NN lines contains the width w_iw\_i and height h_ih\_i of each boxes, space-separated.

출력

Print a single integer, denoting the maximum area of water Donghyun can store if he arrange the boxes optimally.

제한

  • 3N250,0003 \le N \le 250,000
  • 1w_i, h_i1061 \le w\_i,\ h\_i \le 10^6