#P7286. 「EZEC-5」人赢

「EZEC-5」人赢

Description

Xiao has an array kk indexed from 11 to nn.

Xiao defines $f(x,y)=\begin{cases} \min(k_x,k_y) \times (x + y) &x \ne y \\ k_x\times x&x=y \end{cases}$.

Xiao wants to know, for any 1x,yn1 \le x,y \le n, what the maximum value of f(x,y)f(x,y) is. But she cannot solve it, so she asked the kind Xiao Z. However, Xiao Z, who really wanted to impress the girl, found that he could not solve it either, so he can only ask for help from kind you.

Input Format

The first line contains an integer nn.

The second line contains nn integers kk, where the ii-th integer is kik_i. The meaning is as described above.

Output Format

Output one integer in one line, representing the maximum value of f(x,y)f(x,y) over all 1x,yn1 \le x,y \le n.

3
3 2 1
6
5
3 4 5 4 3
28

Hint

Constraints

This problem uses bundled testdata.

  • Subtask 1 (20 points): 1n50001 \le n \le 5000.

  • Subtask 2 (10 points): It is guaranteed that all kik_{i} are equal.

  • Subtask 3 (20 points): ki103k_i \le 10^3.

  • Subtask 4 (50 points): No special properties.

For 100%100\% of the testdata, 1n1061 \le n \le 10^6, 1ki1091 \le k_{i} \le 10^9.


Enhanced version of this problem: link

Translated by ChatGPT 5