Under construction

Still being built. Things may change

Strip

Time limit1sMemory limit128 MB

Summary
Given a strip of length n, fold it along k given creases in order and report the final length, where n can have up to 18 digits.
Level

Medium5 of 10

Topics
Implementation, Simulation, Math
Solved
No attempts yet

Problem

Consider a strip of width 11, length nn, and negligible thickness, divided into unit squares. Starting from its left end, label each of the vertical creases with the integers 00 to nn, as shown in the picture (where n=7n = 7). The strip may be folded only along these creases; once folded, the two overlapping parts are glued together and never straightened again.

After a fold, some labelled creases come to lie exactly on top of one another, forming a single position that therefore carries several labels. For example, folding the n=7n = 7 strip along crease 33 makes crease 11 and crease 55 coincide, so from then on "11" and "55" denote the same crease. Folding again along crease 22 (equivalently, along crease 44, which is now the same position) produces a position with three labels, {1,3,5}\{1, 3, 5\}.

If the fold happens to lie exactly on an end of the current strip, it changes neither the labelling nor the length. Such a fold is not forbidden; it is simply an empty fold.

A sequence of kk integers, each between 00 and nn, therefore defines a sequence of folds. Determine the length of the strip after all kk folds have been applied in order.

Input

The input is given on standard input.

  • Line 11: two space-separated positive integers nn and kk.
  • Line 22: kk space-separated non-negative integers, each at most nn — the creases to fold along, in order.

Output

Print a single line containing one integer: the length of the strip after all kk folds have been applied consecutively.

Constraints

  • nn is a positive integer with at most 1818 decimal digits.
  • 1≤k≤100001 \le k \le 10000.
  • Each fold value is an integer in the range [0,n][0, n].

Hint

For the second test case (n=9n = 9, folds 5 9 2 8 35\ 9\ 2\ 8\ 3), the positions and their labels evolve as shown below. Labels grouped in parentheses share a single position.

Start: {0 1 2 3 4 5 6 7 8 9}\{0\ 1\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 9\}

  • fold 55: {0 (1;9) (2;8) (3;7) (4;6) 5}\{0\ (1;9)\ (2;8)\ (3;7)\ (4;6)\ 5\}
  • fold 99: {(1;9) (0;2;8) (3;7) (4;6) 5}\{(1;9)\ (0;2;8)\ (3;7)\ (4;6)\ 5\}
  • fold 22: {(0;2;8) (1;3;7;9) (4;6) 5}\{(0;2;8)\ (1;3;7;9)\ (4;6)\ 5\}
  • fold 88: {(0;2;8) (1;3;7;9) (4;6) 5}\{(0;2;8)\ (1;3;7;9)\ (4;6)\ 5\} (an empty fold — crease 88 already sits at an end)
  • fold 33: {(1;3;7;9) (0;2;4;6;8) 5}\{(1;3;7;9)\ (0;2;4;6;8)\ 5\}

Three positions remain, so the final length is 22.

Examples2

  1. Example 1

    Input
    7 2
    3 2
    
    Expected output
    3
    
  2. Example 2

    Input
    9 5
    5 9 2 8 3
    
    Expected output
    2