Under construction

Still being built. Things may change

Hyper Dice Rolling

Time limit2.5sMemory limit1024 MB

Summary
Track the die center through M rolls on an N-dimensional die and print its squared distance from the origin.
Level

Medium7 of 10

Topics
Simulation, Math, Implementation
Solved
No attempts yet

Problem

In NN-dimensional Euclidean space, an NN-dimensional Cartesian coordinate system is defined. It has NN coordinate axes through the origin. In this problem, the ii-th axis is called the xix_i axis. The direction in which xix_i increases is the +xi+x_i direction, and the direction in which xix_i decreases is the −xi-x_i direction.

All points with x1≤−12x_1 \leq -\tfrac{1}{2} are filled. For convenience, the part with x1<−12x_1 < -\tfrac{1}{2} is called the floor, and the hyperplane x1=−12x_1 = -\tfrac{1}{2} is called the floor surface. A die of size 1×1×⋯×11 \times 1 \times \cdots \times 1 is centered at the origin. That is, the die occupies [−12,12]×[−12,12]×⋯×[−12,12][-\tfrac{1}{2}, \tfrac{1}{2}] \times [-\tfrac{1}{2}, \tfrac{1}{2}] \times \cdots \times [-\tfrac{1}{2}, \tfrac{1}{2}]. The die has 2N2N faces, each an (N−1)(N-1)-dimensional figure. For each integer ii with 1≤i≤N1 \leq i \leq N, the face in the +xi+x_i direction is labeled 2N+1−i2N+1-i, and the face in the −xi-x_i direction is labeled ii.

You are given MM commands. After performing all commands in order, find the squared Euclidean distance between the origin and the center of the die. Each command is an integer vv with 1≤v≤2N1 \leq v \leq 2N. It means to roll the die so that the face labeled vv lies on the floor surface. It is guaranteed that vv is not labeled on the −x1-x_1 or +x1+x_1 face before the roll.

Input

The first line contains two positive integers NN and MM. NN is the number of dimensions of the die, and MM is the number of rolling commands. (3≤N≤1093 \leq N \leq 10^9, 1≤M≤500 0001 \leq M \leq 500\,000)

The next line contains MM positive integers v1,v2,⋯ ,vMv_1, v_2, \cdots, v_M. viv_i is the integer for the ii-th command. (1≤vi≤2N1 \leq v_i \leq 2N)

Output

On the first line, print the squared Euclidean distance between the origin and the center of the die after performing all commands in order, as an integer.

Hint

The squared Euclidean distance between two points (x1,x2,⋯ ,xN)(x_1, x_2, \cdots, x_N) and (y1,y2,⋯ ,yN)(y_1, y_2, \cdots, y_N) in NN-dimensional Euclidean space is

(x1−y1)2+(x2−y2)2+⋯+(xN−yN)2(x_1-y_1)^2 + (x_2-y_2)^2 + \cdots + (x_N-y_N)^2

The exact definition of rolling the die is as follows. Let VV be the face of the die labeled vv, and let WW be the face of the die touching the floor. The intersection ZZ of VV and WW is an (N−2)(N-2)-dimensional figure. Rolling the die so that VV lies on the floor surface means rotating the die 90 degrees clockwise or counterclockwise about ZZ. The rotation direction must be one in which the die does not go into the floor.

Rotating the die by θ\theta degrees about the (N−2)(N-2)-dimensional figure ZZ is defined as follows. Let xx be any point inside the die. Let AA be the space that passes through xx and is linearly independent of ZZ. Since ZZ is (N−2)(N-2)-dimensional, AA is always a 2-dimensional infinite plane. Let yy be the intersection of AA and ZZ. Since AA and ZZ are linearly independent, yy is a single point. Within the plane AA, rotate xx by θ\theta degrees about yy. Doing this for every xx is defined as rotating the die by θ\theta degrees.

Examples1

  1. Example 1

    Input
    3 6
    2 3 6 5 3 1
    
    Expected output
    10