n,k=map(int,input().split()) a=list(map(int,input().split())) c=list(map(lambda x:int(x)-1,input().split())) def check(l0,r0,l1,r1): if l0==-1 or l1==-1: return False return min(r0,r1)+k>=max(l0,l1) ans=0 inf=1<<60 dp=[[-inf]*n for i in range(n)] color=[[-1,-1]*n for i in range(n)] for d in range(n): for l in range(n): r=(l+d)%n if l==r: dp[l][r]=a[l] color[l][r]=[c[l],c[l]] continue for m in range(l,l+d): m%=n if dp[l][m]==-inf or dp[(m+1)%n][r]==-inf: continue l0,r0=color[l][m] l1,r1=color[(m+1)%n][r] if check(l0,r0,l1,r1): dp[l][r]=max(dp[l][r],dp[l][m]+dp[(m+1)%n][r]) color[l][r]=[min(l0,l1),max(r0,r1)] # print(l,m,r,l0,r0,l1,r1) for l in range(n): for r in range(n): ans=max(ans,dp[l][r]) print(ans)