def main(): import sys input = sys.stdin.read().split() idx = 0 N = int(input[idx]) idx += 1 K = int(input[idx]) idx += 1 A = list(map(int, input[idx:idx+N])) idx += N C = list(map(int, input[idx:idx+N])) idx += N if N == 1: print(A[0]) return duplicated_A = A * 2 duplicated_C = C * 2 max_color = 50 # Precompute prefix sums prefix_sum = [0] * (2 * N + 1) for i in range(2 * N): prefix_sum[i+1] = prefix_sum[i] + duplicated_A[i] # Precompute allowed masks for each color allowed_masks = [0] * (max_color + 1) # 1-based for c in range(1, max_color + 1): low = max(1, c - K) high = min(max_color, c + K) mask = 0 for neighbor in range(low, high + 1): mask |= 1 << (neighbor - 1) # neighbor is 1-based, stored as bit (neighbor-1) allowed_masks[c] = mask # Initialize DP table DP = [[0] * (2 * N) for _ in range(2 * N)] for i in range(2 * N): color = duplicated_C[i] DP[i][i] = 1 << (color - 1) # color 1 is bit 0 max_sum = max(A) for length in range(2, N + 1): for i in range(2 * N - length + 1): j = i + length - 1 current_mask = 0 for k in range(i, j): left_mask = DP[i][k] right_mask = DP[k+1][j] left_compatible = 0 for c in range(1, max_color + 1): if (left_mask & (1 << (c-1))) == 0: continue if (allowed_masks[c] & right_mask) != 0: left_compatible |= (1 << (c-1)) right_compatible = 0 for c in range(1, max_color + 1): if (right_mask & (1 << (c-1))) == 0: continue if (allowed_masks[c] & left_mask) != 0: right_compatible |= (1 << (c-1)) merged = left_compatible | right_compatible current_mask |= merged DP[i][j] = current_mask if current_mask != 0: current_sum = prefix_sum[j + 1] - prefix_sum[i] if current_sum > max_sum: max_sum = current_sum # Check for all possible intervals in the original circular arrangement by considering all starts # Also check all lengths up to N print(max_sum) if __name__ == '__main__': main()