Let $a$ and $b$ be two arrays of lengths $n$ and $m$, respectively, with no elements in common. We can define a new array $\mathrm{merge}(a,b)$ of length $n+m$ recursively as follows:  If one of the arrays is empty, the result is the other array. That is, $\mathrm{merge}(\emptyset,b)=b$ and $\mathrm{merge}(a,\emptyset)=a$. In particular, $\mathrm{merge}(\emptyset,\emptyset)=\emptyset$.  If both arrays are non-empty, and $a_1<b_1$, then $\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b)$. That is, we delete the first element $a_1$ of $a$, merge the remaining arrays, then add $a_1$ to the beginning of the result.  If both arrays are non-empty, and $a_1>b_1$, then $\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m])$. That is, we delete the first element $b_1$ of $b$, merge the remaining arrays, then add $b_1$ to the beginning of the result. 

This algorithm has the nice property that if $a$ and $b$ are sorted, then $\mathrm{merge}(a,b)$ will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if $a=[3,1]$ and $b=[2,4]$, then $\mathrm{merge}(a,b)=[2,3,1,4]$.

A permutation is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array) and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

There is a permutation $p$ of length $2n$. Determine if there exist two arrays $a$ and $b$, each of length $n$ and with no elements in common, so that $p=\mathrm{merge}(a,b)$.


-----Input-----

The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $2t$ lines contain descriptions of test cases. 

The first line of each test case contains a single integer $n$ ($1\le n\le 2000$).

The second line of each test case contains $2n$ integers $p_1,\ldots,p_{2n}$ ($1\le p_i\le 2n$). It is guaranteed that $p$ is a permutation.

It is guaranteed that the sum of $n$ across all test cases does not exceed $2000$.


-----Output-----

For each test case, output "YES" if there exist arrays $a$, $b$, each of length $n$ and with no common elements, so that $p=\mathrm{merge}(a,b)$. Otherwise, output "NO".


-----Example-----
Input
6
2
2 3 1 4
2
3 1 2 4
4
3 2 6 1 5 7 8 4
3
1 2 3 4 5 6
4
6 1 3 7 4 5 8 2
6
4 3 2 5 1 11 9 12 8 6 10 7

Output
YES
NO
YES
YES
NO
NO



-----Note-----

In the first test case, $[2,3,1,4]=\mathrm{merge}([3,1],[2,4])$.

In the second test case, we can show that $[3,1,2,4]$ is not the merge of two arrays of length $2$.

In the third test case, $[3,2,6,1,5,7,8,4]=\mathrm{merge}([3,2,8,4],[6,1,5,7])$.

In the fourth test case, $[1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5])$, for example.

Difficulty: competition

Starter code:

Solutions:
t = int(input())

for _ in range(t):
    n = int(input())
    l = [int(x) for x in input().split()]
    cur = l[0]
    cll = 1
    blocks = []
    for x in l[1:]:
        if x > cur:
            blocks.append(cll)
            cur = x
            cll = 1
        else:
            cll += 1
    blocks.append(cll)

    poss = [[False]*(n+1) for _ in range(len(blocks) + 1)]
    poss[0][0] = True
    for i, b in enumerate(blocks):
        for j in range(n+1):
            poss[i+1][j] = poss[i][j]
            if b <= j:
                poss[i+1][j] |= poss[i][j-b]

    # print()
    # print(blocks)
    # for r in poss:
    #     print(r)
    print("YES" if poss[len(blocks)][n] else "NO")


import sys

input=sys.stdin.readline

for _ in range(int(input())):
    n=int(input())
    p=list(map(int,input().split()))
    index={e:i for i,e in enumerate(p)}
    m=2*n
    data=[]
    for i in range(2*n,0,-1):
        temp=index[i]
        if temp<m:
            data.append(m-temp)
            m=temp
    m=len(data)
    dp=[[False for i in range(2*n+1)] for j in range(m+1)]
    dp[0][0]=True
    for i in range(1,m+1):
        for j in range(2*n+1):
            dp[i][j]=dp[i-1][j]|(dp[i-1][j-data[i-1]]&(j>=data[i-1]))
    if dp[m][n]:
        print("YES")
    else:
        print("NO")


t = int(input())
for _ in range(t):
    n = int(input())
    p = list(map(int, input().split()))
    ma = 0
    l = []
    for i in range(n * 2):
        if ma < p[i]:
            l.append(i)
            ma = p[i]
    l.append(n * 2)
    m = len(l) - 1
    a = [0] * m
    for i in range(m):
        a[i] = l[i + 1] - l[i]
    dp = [0] * (m + 1)
    dp[0] = 1
    for i in range(m):
        dp[i + 1] = dp[i] | (dp[i] << a[i])
    if (dp[m] >> n) & 1:
        print('YES')
    else:
        print('NO')


import sys
input = sys.stdin.readline

t=int(input())
for tests in range(t):
    n=int(input())

    P=list(map(int,input().split()))

    MAX=-1
    L=[]
    count=0
    for p in P:
        if p>MAX:
            L.append(count)
            MAX=p
            count=1
        else:
            count+=1
    L.append(count)

    #print(L)

    DP=1

    MAX=(1<<(n+1))-1

    for i in L:
        DP=(DP|DP<<i)&MAX

    #print(bin(DP))

    if DP & (1<<n)!=0:
        print("YES")
    else:
        print("NO")



def solve(L):
    count=[0]*len(L)
    for i in range(len(L)):
        count[L[i]-1]=i
    pL=[]
    pt=len(L)
    for i in range(len(L)-1, -1, -1):
        if pt>count[i]:
            pL.append(pt-count[i])
            pt=count[i]
    countL=[0]*(2001)
    countL[0]=1
    for i in pL:
        permL=[]
        for j in range(len(countL)):
            if countL[j] and j+i<2001:
                permL.append(i+j)
        for j in permL:countL[j]=1
    return countL[len(L)//2]
for i in ' '*int(input()):
    n=input()
    print(['NO','YES'][solve(list(map(int,input().split())))])

import sys
input = sys.stdin.readline


t = int(input())
for _ in range(t):
    n = int(input())
    p = list(map(int, input().split()))
    
    cnt = 0
    max_ = 0
    ans = []
    for val in p:
        if max_ < val:
            if cnt == 0:
                pass
            else:
                ans.append(cnt)
            max_ = val
            cnt = 1
        else:
            cnt += 1
    ans.append(cnt)
    len_ans = len(ans)
    dp = [False] * (n + 1)
    dp[0] = True
    for i in range(len_ans):
        val = ans[i]
        for j in range(n)[::-1]:
            if j + 1 - val < 0:
                break
            dp[j + 1] |= dp[j + 1 - val]
    if dp[-1]:
        print("YES")
    else:
        print("NO")