import java.util.*; public class Main { public static void main(String[] args) { 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(); } int p = (int)Math.pow(2, n); int[] sum = new int[p]; int[] dp = new int[p]; for(int i = 1; i < p; i++) { for(int j = 0; j < n; j++) { if((i & (1 << j)) != 0) sum[i] = (sum[i] + m[j]) % 1000; } } for(int i = 0; i < p; i++) { dp[i] = Integer.MAX_VALUE; } for(int i = 0; i < n; i++) { dp[(int)(1 << i)] = m[i]; } for(int i = 1; i < p; i++) { if(dp[i] == Integer.MAX_VALUE) { for(int j = 0; j < n; j++) { if((i & (1 << j)) != 0) { int i1 = i - (1 << j); int m1 = (int)Math.max(m[j] - sum[i1], 0); dp[i] = Math.min(dp[i], dp[i1] + m1); } } } } System.out.println(dp[p - 1]); } }