Domain: Algorithms & Data Structures

Let `a` and `b` be two arrays of lengths `n` and `m`, with no elements in common. Define `merge(a, b)` recursively:

- `merge(∅, b) = b` and `merge(a, ∅) = a`.
- If both are non-empty and `a_1 < b_1`, then
  `merge(a, b) = [a_1] + merge([a_2, ..., a_n], b)`.
- If both are non-empty and `a_1 > b_1`, then
  `merge(a, b) = [b_1] + merge(a, [b_2, ..., b_m])`.

You are given a permutation `p` of length `2n` (it contains each integer `1` to `2n` exactly once). Determine if there exist two arrays `a` and `b`, each of length `n` and with no common elements, such that:
`p = merge(a, b)`

### Input
- `n` (`1 ≤ n ≤ 2000`)
- `p_1, ..., p_{2n}`: a permutation of `1, ..., 2n`

### Output
Print **YES** if such arrays `a, b` exist, otherwise print **NO**.

Grading rubric:
criterion: Does the answer state that merge outputs the smaller of the two current first elements (a1 vs b1) when both arrays are non-empty?
weight: 10
criterion: Does the answer define the block starts as indices i where p[i] is a new prefix maximum (p[i] > all previous p[1..i-1])?
weight: 10
criterion: Does the answer state that these prefix-maximum indices partition p into contiguous blocks (segments)?
weight: 10
criterion: Does the answer describe how to compute each block length from consecutive block-start indices (and the last block to the end)?
weight: 10
criterion: Does the answer explicitly state that the block lengths add up to 2n?
weight: 10
criterion: Does the answer state the check to perform as: whether some subset of block lengths sums to n?
weight: 10
criterion: Does the answer define dp[s] as a boolean meaning “some subset of processed blocks has total length s”?
weight: 10
criterion: Does the answer include the DP initialization dp[0] = true?
weight: 10
criterion: Does the answer give the DP update for each block length L as setting dp[s+L] = true when dp[s] is true (i.e., subset-sum transition)?
weight: 10
criterion: Does the answer state the final decision rule: print YES iff dp[n] is true, else NO, and state time complexity O(n^2)?
weight: 10

Answer:
### Expert Solution

In `merge(a, b)`, whenever both arrays are non-empty, the next output element is `min(a_1, b_1)`, and that element is removed from its array.

---

## 1) Key lemma (using the maximum)

Let `M` be the **maximum** value among the elements that have not yet been output.
Assume `M` is currently the first element of one array (say it belongs to `a`).

Since all values are distinct, `M` is larger than every remaining element of `b`.
So while `b` is non-empty, `min(a_1, b_1)` cannot be `M`; merge must keep outputting elements from `b` until `b` becomes empty, and **only then** can it output `M`.

Therefore, in the final permutation `p`, the position where the current maximum `M` appears begins a **contiguous suffix** that must come entirely from the array containing `M`.

---

## 2) Blocks from prefix maxima

Repeatedly removing such suffixes partitions `p` into contiguous blocks.

Equivalently, the **block starts** are exactly the indices `i` where `p_i` is a new prefix maximum:
`p_i > max(p_1, ..., p_{i-1})`

Let these indices be `i_1 < i_2 < ... < i_k` (with `i_1 = 1`).
Then the block lengths are:
- `L_1 = i_2 - i_1`
- `L_2 = i_3 - i_2`
- ...
- `L_{k-1} = i_k - i_{k-1}`
- `L_k = 2n - i_k + 1`

They sum to `2n`, and each block must be assigned wholly to either `a` or `b`.

---

## 3) Reduction to subset sum

Since `|a| = |b| = n`, we need to choose a subset of block lengths whose sum is exactly `n`
(the remaining blocks then sum to `n` automatically).

- This is **necessary** because blocks cannot be split between arrays.
- This is **sufficient** because assigning whole blocks to `a` and `b` makes merge reproduce `p` block-by-block by the lemma above.

---

## 4) DP

Subset-sum DP:

- `dp[s]` = whether we can reach total length `s`
- Initialize `dp[0] = true`
- For each block length `L`, for `s` from `n - L` down to `0`:
  `dp[s + L] |= dp[s]`

Output **YES** iff `dp[n]` is true, else **NO**.

---

## 5) Complexity

At most `2n` blocks and `n ≤ 2000`, so the DP is:
`O(n^2)`
time and `O(n)` memory.