Under construction

Still being built. Things may change

3D Histogram

Time limit2.5sMemory limit512 MB

Summary
Given n blocks with heights a_i and lengths b_i, find the largest volume of a box over any run of adjacent blocks, where the box's height and length are the minimums of a and b over that run.
Level

Hard8 of 10

Topics
Array, Divide and conquer, Segment tree
Solved
No attempts yet

Problem

Mr. Malnar (over the phone): Listen, I had to put up some posters around Zagreb in the middle of the night. I came across a fence made of planks of varying heights, and I was thinking about how to calculate the largest area of a poster that can fit on the fence. Wouldn't that make a nice COCI problem?

Associate: You were doing what?! Anyway, the problem is no good. It's a standard trick with a monotone queue. We even teach it to elementary school students at our camps.

Mr. Malnar: What if you twist it a bit and ask for the answer for every prefix or something like that? That should be hard enough.

Associate: That exact problem appeared in our CERC qualification contest last year. It was tough, and it boils down to the Harbingers trick, but now everyone has seen it.

Mr. Malnar: Are you sure there is nothing we can do?

Associate: I think we have exhausted all the histogram problems. COCI 2010/2011 (Tabovi), COCI 2015/2016 (Poplava), COCI 2017/2018 (Krov), IOI selection test 2018 (Histogram)... You get the point.

Mr. Malnar: What if the histogram is three-dimensional?

Associate: Umm...

You are given a 3D histogram that consists of nn blocks placed next to each other. The ii-th block is 1 meter wide, aia_i meters tall, and bib_i meters long. From the front, the histogram looks like bars of heights a1,a2,…,ana_1, a_2, \ldots, a_n. From the top, it looks like bars of heights b1,b2,…,bnb_1, b_2, \ldots, b_n.

Find the block with the maximum volume that can be placed inside the given 3D histogram. The sides of that block must be parallel to the sides of the blocks that make up the histogram.

Input

The first line contains the integer nn. The ii-th of the following nn lines contains integers aia_i and bib_i (1≤ai,bi≤1061 \le a_i, b_i \le 10^6).

Output

Print the volume in cubic meters.

Hint

Clarification of the first example:

The largest block uses parts of the first two blocks. It is 2 meters wide, 4 meters tall, and 3 meters long. Its volume is 2×4×3=242 \times 4 \times 3 = 24 cubic meters.

Examples3

  1. Example 1

    Input
    5
    5 3
    4 4
    2 1
    3 2
    1 5
    
    Expected output
    24
    
  2. Example 2

    Input
    6
    3 1
    2 1
    2 2
    2 3
    1 1
    2 2
    
    Expected output
    8
    
  3. Example 3

    Input
    5
    15 19
    5 6
    1 13
    3 7
    1 2
    
    Expected output
    285