import sys from collections import defaultdict def main(): N, C = map(int, sys.stdin.readline().split()) L = list(map(int, sys.stdin.readline().split())) W = list(map(int, sys.stdin.readline().split())) def is_kadomatsu(a, b, c): if a == b or b == c or a == c: return False if (b > a and b > c) or (b < a and b < c): return True return False # Initialize DP: dp[cost] -> dictionary of (a, b, flag) : max_total dp = [defaultdict(int) for _ in range(C + 1)] dp[0][(None, None, False)] = 0 for current_cost in range(C + 1): current_states = list(dp[current_cost].items()) for state_info in current_states: (a_prev_prev, a_prev, flag), current_total = state_info for k in range(N): Lk = L[k] Wk = W[k] new_cost = current_cost + Wk if new_cost > C: continue # Transition if a_prev_prev is None: # Previous state has length 0 or 1 new_a_prev_prev = a_prev new_a_prev = Lk new_flag = flag # No kadomatsu formed yet new_state = (new_a_prev_prev, new_a_prev, new_flag) new_total = current_total + Lk if new_total > dp[new_cost].get(new_state, 0): dp[new_cost][new_state] = new_total else: # Check if a_prev_prev, a_prev, Lk form a kadomatsu if is_kadomatsu(a_prev_prev, a_prev, Lk): new_a_prev_prev = a_prev new_a_prev = Lk new_flag = flag or True # Once flag is True, it remains True new_state = (new_a_prev_prev, new_a_prev, new_flag) new_total = current_total + Lk if new_total > dp[new_cost].get(new_state, 0): dp[new_cost][new_state] = new_total max_total = 0 for cost in range(C + 1): for state in dp[cost]: a, b, flag = state if flag and dp[cost][state] > max_total: max_total = dp[cost][state] print(max_total) if __name__ == "__main__": main()