# 区間DPで、区間ごとに最後に残すことができる石を考える n, k = map(int, input().split()) a_list = list(map(int, input().split())) c_list = list(map(int, input().split())) sum_a = sum(a_list) dp = [[[0] * 50 for _1 in range(2 * n)] for _2 in range(2 * n)] # 初期化 for i in range(2 * n): c = c_list[i % n] - 1 dp[i][i][c] = 1 # 区間DP for d in range(1, n): for i in range(2 * n - d): for j in range(d): for c in range(50): if dp[i][i + d][c] == 0 and dp[i][i + j][c] == 1: if sum(dp[i + j + 1][i + d][max(c - k, 0) : min(c + k + 1, 50)]) > 0: dp[i][i + d][c] = 1 # print(i, i + d, c) for c in range(50): if dp[i][i + d][c] == 0 and dp[i + j + 1][i + d][c] == 1: if sum(dp[i][i + j][max(c - k, 0) : min(c + k + 1, 50)]) > 0: dp[i][i + d][c] = 1 # print(i, i + d, c) res = 0 a_list_dup = a_list + a_list for i in range(n): for d in range(n): if sum(dp[i][i + d]) > 0: res = max(res, sum(a_list_dup[i:i + d + 1])) print(res)