import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); final int MOD = 1000; int[] items = new int[N]; for(int i = 0; i < N; i++){ items[i] = sc.nextInt(); } final int bit_size = 1 << N; int[] mods = new int[bit_size]; for(int bit = 0; bit < bit_size; bit++){ for(int not_buy = 0; not_buy < N; not_buy++){ if((bit & (1 << not_buy)) == 0){ mods[bit | (1 << not_buy)] = (mods[bit] + items[not_buy]) % MOD; } } } int[][] DP = new int[bit_size][N]; for(int i = 0; i < bit_size; i++){ for(int j = 0; j < N; j++){ DP[i][j] = Integer.MAX_VALUE; } } for(int i = 0; i < N; i++){ DP[1 << i][i] = items[i]; } for(int bit = 1; bit < bit_size; bit++){ for(int last_buy = 0; last_buy < N; last_buy++){ if(DP[bit][last_buy] == Integer.MAX_VALUE){ continue; } for(int new_buy = 0; new_buy < N; new_buy++){ if((bit & (1 << new_buy)) != 0){ continue; } final int next_bit = bit | (1 << new_buy); DP[next_bit][new_buy] = Math.min(DP[next_bit][new_buy], DP[bit][last_buy] + items[new_buy] - Math.min(items[new_buy], mods[bit])); } } } System.out.println(Arrays.stream(DP[bit_size - 1]).min().getAsInt()); } }