from sys import stdin import itertools N, K = map(int, stdin.readline().split()) A = list(map(int, stdin.readline().split())) C = list(map(int, stdin.readline().split())) dp = [[0] * (2 * N + 1) for _ in range(2 * N)] dp2 = [[0] * (2 * N + 1) for _ in range(2 * N)] s = [0] + list(itertools.accumulate(A + A)) ans = max(A) coef = (1 << (K + 1)) - 1 for i in range(N): C[i] -= 1 dp[i][i + 1] |= 1 << C[i] dp2[i][i + 1] = coef * dp[i][i + 1] dp2[i][i + 1] |= dp2[i][i + 1] >> K dp[i + N][i + 1 + N] = dp[i][i + 1] dp2[i + N][i + 1 + N] = dp2[i][i + 1] for Len in range(2, N + 1): for l in range(2 * N - Len): r = l + Len for m in range(l, r): dp[l][r] |= dp[l][m] & dp2[m][r] dp[l][r] |= dp2[l][m] & dp[m][r] if dp[l][r]: ans = max(ans, s[r] - s[l]) for j in range(K + 1): dp2[l][r] |= dp[l][r] << j dp2[l][r] |= dp2[l][r] >> K print(ans)