using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int monsterCount = int.Parse(Reader.ReadLine()); this.MonsterList = Reader.GetInt(); int ans = this.GetAns(100, 100, Convert.ToInt32("1".PadLeft(monsterCount, '1'), 2)); Console.WriteLine(ans); } private int[] MonsterList; Dictionary> dic = new Dictionary>(); private int GetAns(int hp, int maxHp, int target) { if (!dic.ContainsKey(hp)) { dic.Add(hp, new Dictionary()); } if (dic[hp].ContainsKey(target)) { return dic[hp][target]; } int ans = 0; if (target == 0) { ans = hp; } else if(hp <= 0) { ans = 0; } else { for (int i = 0; i < MonsterList.Length; i++) { int key = Convert.ToInt32("1".PadRight(i + 1, '0'), 2); if ((target & key) == key) { int nextHP = Math.Min(maxHp, hp + this.MonsterList[i]); int nextMaxHp = maxHp; if (this.MonsterList[i] < 0) { nextMaxHp += 100; } int ret = this.GetAns(nextHP, nextMaxHp, target - key); ans = Math.Max(ans, ret); } } } this.dic[hp].Add(target, ans); return ans; } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 4 -40 -60 210 -110 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ', bool trim = false) { string inptStr = ReadLine(); if (trim) { inptStr = inptStr.Trim(); } string[] inpt = inptStr.Split(delimiter); int[] ret = new int[inpt.Length]; for (int i = 0; i < inpt.Length; i++) { ret[i] = int.Parse(inpt[i]); } return ret; } } static void Main() { Program prg = new Program(); prg.Proc(); } }