using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Text.RegularExpressions; using System.Linq; public class Program { static private int N; static private int[] M; static private void Scan() { N = int.Parse(Console.ReadLine()); M = new int[N]; for (int i = 0; i < N; i++) { M[i] = int.Parse(Console.ReadLine()); } } static public void Main(string[] args) { Scan(); int[] dp = (new int[1 << N]).Select(i => int.MaxValue).ToArray(); int[] teika = (new int[1 << N]).Select(i => int.MaxValue).ToArray(); dp[0] = 0; teika[0] = 0; for (int i = 0; i < 1 << N; i++) { for (int j = 0; j < N; j++) { int k = 1 << j; if ((i & k) > 0) continue; teika[i + k] = teika[i] + M[j]; dp[i + k] = Math.Min(dp[i + k], dp[i] + Math.Max(0, M[j] - teika[i] % 1000)); } } Console.WriteLine(dp[(1 << N) - 1]); } }