XrkArul With Elden Ring (Hard Version)

TimeLimit:1000MS  MemoryLimit:256MB
64-bit integer IO format:%lld
未提交 | 登录后收藏
Problem Description

e7073b2a83f1eff7da86dfa14bccadc9ec3d43dd.jpg@942w_progressive.jpg

The only difference with the easy version is that when XrkArul reaches one(left or right) end of the line , he can continue in the same direction from the other end of the line until he reaches the boss he  initially choose.(That means you can consider the world a ring ,not a line)


XrkArul is getting addicted to the game called Elden Ring,in which the world is on a line .There are n bosses on this line ,which XrkArul can get aexp if he beat the i th boss.


Now XrkArul can choose a boss among n bosses , and select a direction he moves (left or right) , in every second he will beat a boss.When XrkArul is on position i , he will beat the i th boss and get ai exp . Note that once he chooses a direction to move,when XrkArul reaches one(left or right) end of the line , he can continue in the same direction from the other end of the line until he reaches the boss he  initially choose.


For every second, the other boss which are not beated will lose their exp by 1 (minimum to 0). Because XrkArul is a skilled gamer, he can always get the most exp in the end. 


Can you calculate the exp that XrkArul eventually gets? 


Note that once a boss is beated, he will no longer exist.

Input

The first line contains only integer n (1≤ n ≤1e6).


The second line contains n integers a1,a2,...,an  (0≤ ai ≤1e9).

Output

An integer , the exp that XrkArul eventually gets.

SampleInput
5
1 2 8 2 3
SampleOutput
10

Note: In this case,XrkArul can start at position 3 and choose to move right,this way the exp he can get is 8+1+1+0+0=10.
Submit
题目统计信息详细
总AC数6
通过人数2
尝试人数3
总提交量16
AC率12.50%
AC该题后可以添加标签
贴完标签可以获得20ACB。
并且可以获得本题所有提交代码查看权限。
点击标题可以显示标签。
如果你还没认真思考过这题,请不要查看标签
如果您已经通过了该题,请务为该题贴上标签

T^T Online Judge

[BUG反馈] [FAQ] [闽ICP备17026590号-1]
当前版本:3.24 系统时间: