using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int itemCount = int.Parse(Reader.ReadLine()); for (int i = 0; i < itemCount; i++) { this.priceList.Add(int.Parse(Reader.ReadLine())); } int ans = this.GetAns(this.priceList, 0); Console.WriteLine(ans); } private Dictionary dic = new Dictionary(); private int GetAns(List target, int nebiki) { string key = string.Join("#", target); if (dic.ContainsKey(key)) { return dic[key]; } if (target.Count == 1) { return Math.Max(0, target[0] - nebiki); } int ans = int.MaxValue ; for (int i = 0; i < target.Count; i++) { List subList = new List(target); subList.RemoveAt(i); ans = Math.Min(ans, this.GetAns(subList, (nebiki + target[i]) % 1000) + (Math.Max(0, target[i] - nebiki))); } dic.Add(key, ans); return ans; } public List priceList = new List(); public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 5 10000 1000 100 10 1 "; 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(); } } } static void Main() { Program prg = new Program(); prg.Proc(); } }