import java.util.Arrays; import java.util.Scanner; public class Main286 { public static void main(String[] args) { new Main286(); } public Main286() { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] m = new int[n]; for (int i = 0; i < n; i++) m[i] = sc.nextInt(); solve(m); } private void solve(int[] m) { int n = m.length; int[] total = new int[1 << n]; int[] min = new int[1 << n]; Arrays.fill(min, Integer.MAX_VALUE); min[0]= 0; for (int i = 1; i < (1 << n); i++) { int cur = 1; for (int j = 0; j < n; j++) { if (i==cur || (i & cur) > 0) { min[i] = Math.min(min[i], min[i ^ cur] + Math.max(0, m[j] - total[i^cur] % 1000)); total[i] = total[i^cur] + m[j]; } cur <<= 1; } } System.out.println(min[(1 << n) - 1]); } }