using System; using System.Collections.Generic; using System.Text; public class Program { public void Proc(){ Reader.IsDebug = false; int ieCount = int.Parse(Reader.ReadLine()); this.OkasiList = new int[ieCount]; this.LimitList = new int[ieCount]; List targetList = new List(); for(int i=0; i> dic = new Dictionary>(); private long GetAns(int havOkashi, List target) { string key = string.Join("#", target); if(!dic.ContainsKey(havOkashi)) { dic.Add(havOkashi, new Dictionary()); } if(dic[havOkashi].ContainsKey(key)) { return dic[havOkashi][key]; } if(target.Count == 1) { if(this.LimitList[target[0]] > havOkashi) { return this.OkasiList[target[0]]; } else { return 0; } } long ans = 0; for(int i=0; i havOkashi) { List subList = new List(); subList.AddRange(target); subList.RemoveAt(i); ans = Math.Max(ans, this.GetAns(havOkashi + OkasiList[idx], subList) + OkasiList[idx]); } } dic[havOkashi].Add(key, ans); return ans; } public int[] OkasiList; public int[] LimitList; public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } } class Reader { public static bool IsDebug = true; private static System.IO.StringReader sr; public static string ReadLine() { if(IsDebug) { if(sr == null) { sr = new System.IO.StringReader(initStr.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ') { string[] inpt = ReadLine().Split(delimiter); int[] ret = new int[inpt.Length]; for(int i=0; i