#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using uint = unsigned; using ull = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max() / 32 * 15 + 208; ll dp[600][601]; int main() { int n, k; cin >> n >> k; vector A(2*n), C(2*n); for (int i = 0; i < n; ++i) { scanf("%d", &A[i]); A[i+n] = A[i]; } vector S(2*n+1); for (int i = 0; i < 2*n; ++i) { S[i+1] = S[i] + A[i]; } for (int i = 0; i < n; ++i) { scanf("%d", &C[i]); C[i+n] = C[i]; } n *= 2; for (int i = 0; i < n; ++i) { for (int j = i+1; j <= n; ++j) { dp[i][j] = 0; } dp[i][i+1] = (1LL << C[i]); } int half = n/2; for (int len = 2; len <= half; ++len) { for (int l = 0; l+len <= n; ++l) { int r = l+len; for (int mid = l+1; mid < r; ++mid) { ll lval = dp[l][mid], rval = dp[mid][r]; if(lval == 0 || rval == 0) continue; dp[l][r] |= lval & rval; for (int i = 1; i <= k; ++i) { dp[l][r] |= lval & (rval << i); dp[l][r] |= lval & (rval >> i); dp[l][r] |= lval << i & rval; dp[l][r] |= lval >> i & rval; } } } } ll ans = 0; for (int i = 0; i < n; ++i) { for (int j = i+1; j <= n && j-i <= half; ++j) { if(dp[i][j] > 0) ans = max(ans, S[j]-S[i]); } } cout << ans << "\n"; return 0; }