n, k = map(int, input().split()) a = list(map(int, input().split())) c = list(map(lambda x: int(x)-1, input().split())) dp = [[0] * 2 * n for _ in range(2 * n)] for i in range(n): dp[i][i] = 1 << c[i] dp[i + n][i + n] = 1 << c[i] for len in range(2, n + 1): for l in range(2 * n - len + 1): r = l + len - 1 for i in range(len - 1): m = l + i bitl, bitr = dp[l][m], dp[m + 1][r] for p in range(k + 1): if dp[l][m] == 0 or dp[m + 1][r] == 0: continue dp[l][r] |= ((bitl << p) & bitr) dp[l][r] |= ((bitl >> p) & bitr) dp[l][r] |= (bitl & (bitr << p)) dp[l][r] |= (bitl & (bitr >> p)) s = [0] for i in range(2 * n): s.append(s[i] + a[i % n]) ans = 0 for i in range(2 * n): for j in range(i, 2 * n): if dp[i][j]: ans = max(ans, s[j + 1] - s[i]) print(ans)