[Solution] Two Posters solution codeforces

Two Posters solution codeforces – You want to advertise your new business, so you are going to place two posters on a billboard in the city center. The billboard consists of 𝑛n vertical panels of width 11 and varying integer heights, held together by a horizontal bar. The 𝑖i-th of the 𝑛n panels has height 𝑖hi.

Two Posters solution codeforces

Two Posters solution codeforces

Initially, all panels hang down from the bar (their top edges lie on it), but before placing the two posters, you are allowed to move each panel up by any integer length, as long as it is still connected to the bar (its bottom edge lies below or on it).

After the moves are done, you will place two posters: one below the bar and one above it. They are not allowed to go over the bar and they must be positioned completely inside of the panels.

What is the maximum total area the two posters can cover together if you make the optimal moves? Note that you can also place a poster of 00 area. This case is equivalent to placing a single poster.

Input

The first line of input contains one integer 𝑛n (1𝑛1041≤n≤104) — the number of vertical panels.

The second line of input contains 𝑛n integers 1,2,...,𝑛h1,h2,…,hn (1𝑖10121≤hi≤1012) — the heights of the 𝑛n vertical panels.

Output

Print a single integer — the maximum total area the two posters can cover together.

Two Posters solution codeforces

6
2 2 3 5 4 5

output

Copy
18

input

Copy
1
1

output

Copy
1

Two Posters solution codeforces

In the first sample test, we can choose an upper poster with area 1212 and a lower poster of area 66 as in the image below.

Two Posters solution codeforces
In the second sample test, we can cover the whole billboard using a single poster.

Leave a Comment