import java.util.Arrays; import java.util.Scanner; public class Main_yukicoder286 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = new int[n]; for (int i = 0; i < n; i++) { m[i] = sc.nextInt(); } int[] dp = new int[0x1 << n]; dp[0] = 0; for (int i = 0; i < 0x1 << n; i++) { for (int j = 0; j < n; j++) { if ((i & (0x1 << j)) != 0) { continue; } dp[i | (0x1 << j)] = Math.max(dp[i | (0x1 << j)], dp[i] + Math.min(sum(i) % 1000, m[j])); } } System.out.println(sum((0x1 << n) - 1) - dp[(0x1 << n) - 1]); sc.close(); } private static int n; private static int[] m; private static int sum(int i) { int ret = 0; for (int j = 0; j < n; j++) { if ((i & (0x1 << j)) != 0) { ret += m[j]; } } return ret; } }