#P6842. Folding Strip Length

    ID: 20049 Type: Default 1000ms 256MiB

Folding Strip Length

Folding Strip Length

Consider a strip of width $1$ and length $n$, composed of unit squares placed side by side. The vertical lines that separate these squares are labeled with the integers from $0$ to $n$. We are allowed to fold the strip only along these vertical lines. When folding along a vertical line located at $f$, the part of the strip to the right of $f$ is reflected over to the left without altering the labels or the length of that part. That is, any point originally at position $x$ (with $x > f$) moves to position $2f - x$, while positions at or to the left of $f$ remain unchanged.

After a series of folds defined by a sequence of integers (each an element in $[0,n]$ and of length $k$), some vertical lines may have multiple labels (for example, after folding along $3$ and then along $2$, the vertical line at position $1$ might represent the labels $(1;3;5)$). However, your task is not to track these labels but rather to compute the final length of the folded strip. The final length is defined as the distance between the leftmost and rightmost vertical lines present after all folds.

Note: A fold that does not change the configuration (an "empty" fold) is allowed and should be processed normally.

inputFormat

The first line contains two integers $n$ and $k$, where $n$ is the initial length of the strip and $k$ is the number of folds. The second line contains $k$ integers, each representing a fold position $f$ ($0 \le f \le n$). The fold operations are applied sequentially in the order given.

outputFormat

Output a single integer --- the final length of the folded strip, computed as the difference between the maximum and minimum positions of the vertical lines after all folds.

sample

7 1
3
4

</p>