Indian National Olympiad in Informatics 2016
	There are k types of brackets each with its own opening bracket and closing bracket. We assume that the first pair is denoted by the numbers 1 and k+1, the second by 2 and k+2 and so on. Thus the opening brackets are denoted by 1,2,.., k, and the corresponding closing brackets are denoted by k+1,k+2,..., 2*k respectively.

	Some sequences with elements from 1,2, ... 2*k form well-bracketed sequences while others don't. A sequence is well-bracketed, if we can match or pair up opening brackets and closing brackets of the same type in such a way that the following holds:

	 1) every bracket is paired up

	 2) in each matched pair, the opening bracket occurs before the closing bracket

	 3) for a matched pair, any other matched pair lies either completely between them or outside them.

	For the examples discussed below, let us assume that k = 2. The sequence 1,1,3 is not well-bracketed as one of the two 1's cannot be paired. The sequence 3,1,3,1 is not well-bracketed as there is no way to match the second 1 to a closing bracket occurring after it. The sequence 1,2,3,4 is not well-bracketed as the matched pair 2,4 is neither completely between the matched pair 1,3 nor completely outside it. That is, the matched pairs cannot overlap. The sequence 1,2,4,3,1,3 is well-bracketed. We match the first 1 with the first 3, the 2 with the 4 and the second 1 with the second 3, satisfying all the 3 conditions. If you rewrite these sequences using [,{,],} instead of 1,2,3,4 respectively, this will be quite clear.

	In this problem you are given a sequence of brackets, of length N: B[1], .., B[N], where each B[i] is one of the brackets. You are also given an array of Values: V[1],.., V[N].

	Among all the subsequences in the Values array, such that the corresponding bracket subsequence in the B Array is a well-bracketed sequence, you need to find the maximum sum. Suppose N = 6, k = 3 and the values of V and B are as follows:

i	1	2	3	4	5	6
V[i]	4	5	-2	1	1	6
B[i]	1	3	4	2	5	6

	Then, the brackets in positions 1,3 form a well-bracketed sequence (1,4) and the sum of the values in these positions is 2 (4 + -2 = 2). The brackets in positions 1,3,4,5 form a well-bracketed sequence (1,4,2,5) and the sum of the values in these positions is 4. Finally, the brackets in positions 2,4,5,6 forms a well-bracketed sequence (3,2,5,6) and the sum of the values in these positions is 13. The sum of the values in positions 1,2,5,6 is 16 but the brackets in these positions (1,3,5,6) do not form a well-bracketed sequence. You can check the best sum from positions whose brackets form a well-bracketed sequence is 13.


-----Input format-----
	One line, which contains (2*N + 2) space separate integers. The first integer denotes N. The next integer is k. The next N integers are V[1],..., V[N]. The last N integers are B[1],.., B[N].


-----Output format-----
	One integer, which is the maximum sum possible satisfying the requirement mentioned above. 


-----Test data-----

		1 ≤ k ≤ 7

		-106 ≤ V[i] ≤ 106, for all i

		1 ≤ B[i] ≤ 2*k, for all i.
	
Subtask 1 (40 Marks) 1 ≤ n ≤ 10.
Subtask 2 (60 Marks) 1 ≤ n ≤ 700.


-----Sample Input-----
6 3 4 5 -2 1 1 6 1 3 4 2 5 6

-----Sample Output-----
13

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '# cook your dish here\nimport bisect\ndef recur(s_i, e_i, end_i):\n    # if end_i in dp[s_i] and e_i in dp[s_i][end_i]:\n    #     return dp[s_i][end_i][e_i]\n        \n    inside = 0\n    for i in range(s_i+1, e_i):\n        if b_l[i] < k:\n            if e_i not in dp[i]:\n                dp[i][e_i] = 0\n                bi_i = bisect.bisect_left(b_inv[b_l[i]+k], i)\n                for ind in b_inv[b_l[i]+k][bi_i:]:\n                    if i < ind < e_i:\n                        dp[i][e_i] = max(dp[i][e_i], recur(i, ind, e_i))\n            inside = max(inside, dp[i][e_i])\n    outside = 0\n    for i in range(e_i+1, min(n, end_i)):\n        if b_l[i] < k:\n            if end_i not in dp[i]:\n                dp[i][end_i] = 0\n                bi_i = bisect.bisect_left(b_inv[b_l[i]+k], i)\n                for ind in b_inv[b_l[i]+k][bi_i:]:\n                    if i < ind < end_i:\n                        dp[i][end_i] = max(dp[i][end_i], recur(i, ind, end_i))\n            outside = max(outside, dp[i][end_i])\n                    \n    # if end_i not in dp[s_i]:\n    #     dp[s_i][end_i] = {}\n    # dp[s_i][end_i].update({e_i: v_l[s_i] + v_l[e_i] + outside + inside})\n    # return dp[s_i][end_i][e_i]\n    return v_l[s_i] + v_l[e_i] + outside + inside\n    \nn, k, *l = map(int, input().split())\nv_l, b_l = l[:n], l[n:]\n\nb_inv = {key:[] for key in range(2*k)}\nfor i in range(n):\n    b_l[i] -= 1\n    b_inv[b_l[i]].append(i)\n\ndp = [{} for _ in range(n)]\nans = 0\nfor i in range(n):\n    if b_l[i] < k:\n        if n+1 not in dp[i]:\n            dp[i][n+1] = 0\n            bi_i = bisect.bisect_left(b_inv[b_l[i]+k], i)\n            for ind in b_inv[b_l[i]+k][bi_i:]:\n                if i < ind:\n                    dp[i][n+1] = max(dp[i][n+1], recur(i, ind, n+1))\n        ans = max(ans, dp[i][n+1])\n# print(dp)\nprint(ans)'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 13693