# 区間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(n)] for _2 in range(2 * n)] dp_s = [[[0] * 51 for _1 in range(n)] for _2 in range(2 * n)] # 初期化 for i in range(2 * n): c = c_list[i % n] - 1 dp[i][0][c] = 1 for c_ in range(c + 1, 51): dp_s[i][0][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][d][c] == 0 and dp[i][j][c] == 1: if dp_s[i + j + 1][d - j - 1][min(c + k + 1, 50)] > dp_s[i + j + 1][d - j - 1][max(c - k, 0)]: dp[i][d][c] = 1 # print(i, d, c) for c in range(50): if dp[i][d][c] == 0 and dp[i + j + 1][d - j - 1][c] == 1: if dp_s[i][j][min(c + k + 1, 50)] > dp_s[i][j][max(c - k, 0)]: dp[i][d][c] = 1 # print(i, d, c) ss = 0 dp_s[i][d][0] = 0 for c in range(50): ss += dp[i][d][c] dp_s[i][d][c + 1] = ss res = 0 a_list_dup = a_list + a_list for i in range(n): for d in range(n): if sum(dp[i][d]) > 0: res = max(res, sum(a_list_dup[i:i + d + 1])) print(res)