using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray(); int itemCount = inpt[0]; int k = inpt[1]; this.Items = new int[itemCount]; for (int i = 0; i < itemCount; i++) { this.Items[i] = int.Parse(Reader.ReadLine()); } int ans = GetAns(0, k); Console.WriteLine(ans); } private int[] Items; private Dictionary> dic = new Dictionary>(); private int GetAns(int idx, int remain) { if(idx>=this.Items.Length) { return 0; } if(!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary()); } if(dic[idx].ContainsKey(remain)) { return dic[idx][remain]; } int ans = 0; if(Items[idx]<=remain) { ans = GetAns(idx + 1, remain - Items[idx]) + Items[idx]; } ans = Math.Max(ans, GetAns(idx + 1, remain)); dic[idx][remain] = ans; return ans; } public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 4 4 1 2 3 5 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }