Domain: Algorithms & Data Structures

You have a strip of `1 × 10^9` cells numbered `1` to `10^9`.

There are `n` snakes (numbered `1` to `n`). Initially, each snake occupies exactly one cell, and no two snakes can be placed in the same cell. After placement, the game runs for `q` seconds. Each second exactly one event occurs:

- **Snake `s_i` enlarges:** if it currently occupies `[l, r]`, it becomes `[l, r+1]`.
- **Snake `s_i` shrinks:** if it currently occupies `[l, r]`, it becomes `[l+1, r]`.

If at any time any snake hits an obstacle (another snake or the end of the strip), you lose. Otherwise, you win with a score equal to the maximum cell index occupied by any snake at any time.

Given the sequence of `q` events (guaranteed valid: a snake of length 1 never shrinks), find the minimum possible score over all initial placements.

## Input
- First line: integers `n, q` (`1 ≤ n ≤ 20`, `1 ≤ q ≤ 2 · 10^5`).
- Next `q` lines: either `"s_i +"` or `"s_i -"`.

## Output
Output one integer — the minimum possible score.

Grading rubric:
criterion: Does the solution state the final answer as min over last of ( d[(1<<n)-1][last] + plusCount[last] ), where plusCount[k] is the number of '+' events of snake k?
weight: 20
criterion: Does the solution state the score for a fixed permutation p1..pn as 1 + (sum of gaps between consecutive snakes) + (number of '+' events of the last snake pn)?
weight: 10
criterion: Does the solution state that choosing an order of snakes on the strip corresponds to choosing a permutation of the n snakes?
weight: 10
criterion: Does the solution define (for an ordered pair (i,j) with j immediately to the right of i) that an event contributes e=−1 when snake i enlarges and e=+1 when snake j shrinks (and e=0 otherwise) to the evolution of the gap?
weight: 10
criterion: Does the solution state the DP base case d[1<<i][i] = 1 for every snake i?
weight: 10
criterion: Does the solution state that for a fixed permutation, it is optimal to place snakes greedily as close together as possible (i.e., using the minimum feasible gaps between consecutive snakes)?
weight: 10
criterion: Does the solution give the exact formula for minDist[i][j] as 1 − min_prefix_sum of the cumulative e-values over all events (equivalently, minDist[i][j] = -min_{1..q})?
weight: 10
criterion: Does the solution define the DP state d[mask][last] as the minimum possible starting cell (position) of snake 'last' after placing exactly the set 'mask' in some order, with the first placed snake starting at cell 1?
weight: 10

Answer:
## Expert Answer

Key idea: placing snakes left-to-right defines a permutation; for any fixed order the best placement is greedy (minimum feasible gaps), and we can compute the required minimum gaps from the event log.

### 1) Reduce to an order (permutation)
In any valid placement, the snakes appear left-to-right in some permutation `p_1, p_2, ..., p_n`. If this permutation is fixed, it is optimal to place them as close as possible (use the smallest initial gaps that still avoid collisions during all events), because any extra initial spacing only increases the maximum occupied cell.

### 2) Score for a fixed permutation
Let the initial gap between consecutive snakes `p_k` and `p_{k+1}` be `g_k`. Then:
`score(p) = 1 + ∑_{k=1..n-1} g_k + plusCount(p_n)`
because the first snake can start at cell 1, the last snake starts at `1 + ∑ g_k`, and only enlargements of the last snake can push the global maximum further right.

### 3) Compute `minDist[i][j]` for "`j` immediately right of `i`"
Assume snake `j` is immediately to the right of snake `i` with initial gap `x`. The gap changes only when:
- snake `i` enlarges (gap `-1`),
- snake `j` shrinks (gap `+1`).

Define per-event contribution `e_t` for ordered pair `(i, j)`:
- `e_t = -1` if event `t` is "`i` +",
- `e_t = +1` if event `t` is "`j` -",
- `e_t = 0` otherwise.

After `t` events the gap is `x + ∑_{u=1..t} e_u`. We need this `≥ 0` for all `t`, so:
`minDist[i][j] = 1 - min_{1 ≤ t ≤ q} (∑_{u=1..t} e_u)`
(This equals `-min(...) + 1`).

### 4) Choose the best permutation via bitmask DP
Let `d[mask][last]` be the minimum possible starting cell of snake `last` after placing exactly the set `mask` in some order, with the first placed snake starting at cell 1.

Base:
`d[1 << i][i] = 1` for all `i`.

Transition:
`d[mask | (1 << j)][j] = min_{i ∈ mask} (d[mask][i] + minDist[i][j])`

Final answer:
`min_{1 ≤ last ≤ n} ( d[(1 << n) - 1][last] + plusCount(last) )`