#P6842. [BalkanOI 2009] Strip

[BalkanOI 2009] Strip

Description

Let us consider a strip with width 11, length nn, and negligible thickness, made of unit squares. As shown in the figure (where n=7n=7), starting from the left side of the strip, we label each vertical line with the non-negative integers from 00 to nn. We are only allowed to fold the rectangular strip along these vertical lines, and after that the two parts are glued together and will never be straightened again.

Naturally, after folding, some labels will overlap and form a vertical line with more labels. The figure shows the situation after folding along vertical line 33. After this operation, some lines will have two labels. For example, we can say either 11 or 55; they refer to the same vertical line. If we then fold along 22 (we can also say 44, it is the same), there will be a vertical line with even three names, as we can see: (1;3;5)(1;3;5). For example, if we fold the rectangular strip along line 33, we only rotate the strip without changing its labels or length. We call such a fold “empty”: it is not illegal, it just does not make any significant change.

In this way, a sequence of kk integers (each integer from 00 to nn) uniquely defines how the strip is folded. Write a program to find the length of the strip after all folds are completed.

Input Format

Read from standard input.

  • The first line contains two positive integers n,kn,k, representing the length of the strip and the number of folds.
  • The second line contains kk non-negative integers, representing the folding order, and each number is in the range [0,n]\left[0,n\right].

Output Format

Write to standard output.

One line with one integer, representing the length after folding.

7 2
3 2
3
9 5
5 9 2 8 3
2

Hint

Constraints

nn has at most 1818 digits, and k10000k\le 10000.


Explanation of Sample 22

The folding steps are shown in the figure:

Initial state: {0123456789}\{0\,1\,2\,3\,4\,5\,6\,7\,8\,9\}.

Folding steps: $\rightarrow \{0\,(1;9)\,(2;8)\,(3;7)\,(4;6)\,5\}\rightarrow \{(1;9)\,(0;2;8)\,(3;7)\,(4;6)\,5\}\rightarrow \{(0;2;8)\,(1;3;7;9)\,(4;6)\,5\}\rightarrow \{(1;3;7;9)\,(0;2;4;6;8)\,5\}$.

(Note: Sample 11 is the same as the picture.).

Translated by ChatGPT 5