N, K = list(map(int, input().split())) A = [] for n in range(N): A.append(int(input())) # 以下は全探索のアルゴリズム(TLEだった) """ def bin_search(item_index, rest_weight): if item_index == N: return 0 elif rest_weight < A[item_index]: return bin_search(item_index + 1, rest_weight) else: res1 = bin_search(item_index + 1, rest_weight) res2 = bin_search(item_index + 1, rest_weight - A[item_index]) + A[item_index] return max(res1, res2) print(bin_search(0, K)) """ # 以下は動的計画法で書いたアルゴリズム import numpy as np dp = np.zeros((N + 1, K + 1)) def do_dp(): for item_index in range(N - 1, -1, -1): for rest_weight in range(K + 1): if rest_weight < A[item_index]: dp[item_index][rest_weight] = dp[item_index + 1][rest_weight] else: dp[item_index][rest_weight] = \ max(dp[item_index + 1][rest_weight], dp[item_index + 1][rest_weight - A[item_index]] + A[item_index]) return dp[0][K] ans_weight = do_dp() print(int(ans_weight))