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