atcoder#AGC030B. [AGC030B] Tree Burning
[AGC030B] Tree Burning
Score : points
Problem Statement
Takahashi Lake has a perimeter of . On the circumference of the lake, there is a residence of the lake's owner, Takahashi. Each point on the circumference of the lake has a coordinate between and (including but not ), which is the distance from the Takahashi's residence, measured counter-clockwise.
There are trees around the lake; the coordinate of the -th tree is . There is no tree at coordinate , the location of Takahashi's residence.
Starting at his residence, Takahashi will repeat the following action:
- If all trees are burnt, terminate the process.
- Specify a direction: clockwise or counter-clockwise.
- Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.
- When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.
Find the longest possible total distance Takahashi walks during the process.
Partial Score
A partial score can be obtained in this problem:
- points will be awarded for passing the input satisfying .
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the longest possible total distance Takahashi walks during the process.
10 3
2
7
9
15
Takahashi walks the distance of if the process goes as follows:
- Walk a distance of counter-clockwise, burn the tree at the coordinate and stay there.
- Walk a distance of counter-clockwise, burn the tree at the coordinate and stay there.
- Walk a distance of clockwise, burn the tree at the coordinate and stay there.
10 6
1
2
3
6
7
9
27
314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265
1204124749