## https://yukicoder.me/problems/no/1522 def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) A.sort(reverse=True) dp = [[0] * (M + 1 ) for _ in range(N)] for i in range(1, N): for m in range(M + 1): a = A[i - 1] b = A[i - 1] - A[i] # 選ばない場合 dp[i][m] = max(dp[i][m], dp[i - 1][m]) # 選ぶ場合 if m + b <= M: dp[i][m + b] = max(dp[i][m + b], dp[i - 2][m] + a) answer = 0 for m in range(M + 1): answer = max(answer, dp[-1][m]) print(answer) if __name__ == '__main__': main()