n, m = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) # Initialize DP: dp[i][cost] = max O dp = [{} for _ in range(n+1)] dp[0][0] = 0 for i in range(n): current_dp = dp[i] if not current_dp: continue # Option 1: skip the current element for cost in current_dp: o_val = current_dp[cost] next_i = i + 1 if next_i <= n: if cost in dp[next_i]: if o_val > dp[next_i][cost]: dp[next_i][cost] = o_val else: dp[next_i][cost] = o_val # Option 2: pair current with next element if i + 1 >= n: continue pair_cost = a[i] - a[i+1] for cost in current_dp: o_val = current_dp[cost] new_cost = cost + pair_cost if new_cost > m: continue new_o = o_val + a[i] next_i_paired = i + 2 if next_i_paired > n: continue if new_cost in dp[next_i_paired]: if new_o > dp[next_i_paired][new_cost]: dp[next_i_paired][new_cost] = new_o else: dp[next_i_paired][new_cost] = new_o # Find the maximum O where cost <= m max_o = 0 for i in range(n+1): for cost in dp[i]: if cost <= m and dp[i][cost] > max_o: max_o = dp[i][cost] print(max_o)