Under construction

Still being built. Things may change

Water Knows the Answers

Time limit3sMemory limit1024 MB

Summary
Given N boxes that may each be rotated, arrange them in a row to maximize the volume of water that stays trapped between them above the ground.
Level

Hard8 of 10

Topics
Array, Sorting, Greedy
Solved
No attempts yet

Problem

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 with the xx-axis as the ground. It rains a lot in the 2D plane, and the rain falls from y=+∞y = +\infty.

Recently, Donghyun read Water Knows the Answers and was impressed by it. 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 them to collect the rainfall. Then the water will give him the answers to life. To do this, he places the boxes in a row. No empty space is allowed between the boxes. He may rotate some of the boxes or leave them as they are, but every box must have an edge touching the ground.

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

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

What is the maximum area of water Donghyun can store? (In the 2D plane, area means volume.) He does not have water yet, so he does not know the answer. Do you know it?

Input

The input consists of N+1N+1 lines.

The first line contains the number of boxes NN.

Each of the next NN lines contains the width wiw_i and height hih_i of a box, separated by a space.

Output

Print a single integer, the maximum area of water Donghyun can store when he arranges the boxes optimally.

Constraints

  • 3≤N≤250,0003 \le N \le 250,000
  • 1≤wi, hi≤1061 \le w_i,\ h_i \le 10^6

Examples1

  1. Example 1

    Input
    3
    4 3
    2 6
    5 1
    Expected output
    15