import java.util.*; import java.io.*; public class Main { static int n; static int[] prices; static int[] dp; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); prices = new int[n]; for (int i = 0; i < n; i++) { prices[i] = Integer.parseInt(br.readLine()); } dp = new int[1 << n]; System.out.println(dfw((1 << n) - 1)); } static int dfw(int key) { if (key == 0) { return 0; } if (dp[key] != 0) { return dp[key]; } int sum = 0; for (int i = 0; i < n; i++) { if (((1 << i) & key) != 0) { sum += prices[i]; } } int min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { if (((1 << i) & key) == 0) { continue; } min = Math.min(min, dfw(key ^ (1 << i)) + prices[i] - Math.min(prices[i], (sum - prices[i]) % 1000)); } dp[key] = min; return min; } }