Under construction

Still being built. Things may change

Steel Slicing

Time limit2.5sMemory limit512 MB

Summary
Given each slab's heights above and below the x-axis, find the largest-area axis-aligned rectangle that fits inside the histogon.
Level

Hard8 of 10

Topics
Divide and conquer, Array
Solved
No attempts yet

Problem

ISCO (ICPC Steel Company) buys steel sheets of a certain shape, cuts them into pieces, and sells the pieces to the industry market. Every steel sheet ISCO buys is a polygon without holes, and each side is either horizontal or vertical with respect to the xx-axis. The length of each side is a positive integer. We call such a polygon a histogon. Figure 1(a) shows a histogon.

Figure 1. (a) A steel sheet that is a histogon. (b) An axis-aligned rectangle (red) with maximum area contained in the histogon. (c) The histogon is subdivided along vertical lines into 8 rectangular slabs with width 1.

A piece sells for a much higher price when it is a rectangle with a larger area, so ISCO wants to cut each sheet to get a rectangle of maximum area. The task is to find the axis-aligned rectangle with the largest area that is contained in the steel sheet. The rectangle must have every side horizontal or vertical with respect to the xx-axis. Figure 1(b) shows the maximum-area rectangle for the histogon in Figure 1(a).

A histogon with width nn can be subdivided along vertical lines into nn rectangular slabs with width 1. Figure 1(c) shows the histogon of Figure 1(a) split into 8 slabs. The slabs are numbered from 1 to nn from left to right, so the leftmost slab is number 1. Assume the xx-axis intersects every slab. Slab ii is described by two values, hih_i and lil_i, where hih_i is the vertical length of slab ii above the xx-axis and lil_i is the vertical length of slab ii below the xx-axis.

Given a histogon with width nn, output the maximum area among the axis-aligned rectangles contained in the histogon.

Input

The first line contains one integer nn (1≤n≤200,0001 \le n \le 200{,}000), the width of the histogon. The slabs are numbered from 1 to nn such that the leftmost slab is number 1. Each of the following nn lines contains two nonnegative integers hih_i and lil_i (0≤hi,li≤1,000,000,0000 \le h_i, l_i \le 1{,}000{,}000{,}000) for slab ii.

Output

Print exactly one line containing the maximum area among the axis-aligned rectangles contained in the given histogon.

Examples2

  1. Example 1

    Input
    8
    1 4
    4 2
    3 2
    5 1
    6 4
    4 2
    2 3
    5 0
    
    Expected output
    20
    
  2. Example 2

    Input
    5
    23 15
    23 17
    3 22
    15 3
    5 1
    
    Expected output
    76