n, M = map(int, input().split()) a = list(map(int, input().split())) a.sort(reverse=True) max_oe = M max_index = n # Initialize DP table: dp[i][m] represents the maximum O sum considering first i elements and unfairness m. dp = [[-1 for _ in range(max_oe + 1)] for _ in range(n + 2)] dp[0][0] = 0 for i in range(n + 1): for current_oe in range(max_oe + 1): if dp[i][current_oe] == -1: continue # Option 1: Do not pick the current element (if there is one) if i < n: if dp[i + 1][current_oe] < dp[i][current_oe]: dp[i + 1][current_oe] = dp[i][current_oe] # Option 2: Pick the current and next element as a pair (if possible) if i + 1 < n: ai = a[i] aj = a[i + 1] new_oe = current_oe + (ai - aj) new_o = dp[i][current_oe] + ai if new_oe <= max_oe and new_o > dp[i + 2][new_oe]: dp[i + 2][new_oe] = new_o # Find the maximum O across all possible dp states max_o = 0 for i in range(n + 2): for oe in range(max_oe + 1): if dp[i][oe] > max_o: max_o = dp[i][oe] print(max_o)