atcoder#AGC030B. [AGC030B] Tree Burning

[AGC030B] Tree Burning

Score : 800800 points

Problem Statement

Takahashi Lake has a perimeter of LL. 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 00 and LL (including 00 but not LL), which is the distance from the Takahashi's residence, measured counter-clockwise.

There are NN trees around the lake; the coordinate of the ii-th tree is XiX_i. There is no tree at coordinate 00, 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:

  • 300300 points will be awarded for passing the input satisfying N2000N \leq 2000.

Constraints

  • 2L1092 \leq L \leq 10^9
  • 1N2×1051 \leq N \leq 2\times 10^5
  • 1X1<...<XNL11 \leq X_1 < ... < X_N \leq L-1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

LL NN

X1X_1

::

XNX_N

Output

Print the longest possible total distance Takahashi walks during the process.

10 3
2
7
9
15

Takahashi walks the distance of 1515 if the process goes as follows:

  • Walk a distance of 22 counter-clockwise, burn the tree at the coordinate 22 and stay there.
  • Walk a distance of 55 counter-clockwise, burn the tree at the coordinate 77 and stay there.
  • Walk a distance of 88 clockwise, burn the tree at the coordinate 99 and stay there.
10 6
1
2
3
6
7
9
27
314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265
1204124749