#include #include using namespace std; #define FOR(i, a, n) for(int i=a; i=a; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) FOR(i, 0, n) #define INF 100000000 int dp[(1 << 15)]; int main() { int N; int M[16]; cin >> N; REP(i, N) cin >> M[i]; REP(i, 1 << N) dp[i] = INF; dp[0] = 0; REP(mask, 1 << N) { REP(i, N) { if (mask&(1 << i)) continue; int sum = 0; REP(j, N) { if (mask&(1 << j)) sum += M[j]; } dp[mask | (1 << i)] = min(dp[mask | (1 << i)], dp[mask] + max(0, M[i] - (sum % 1000))); } } cout << dp[(1 << N) - 1] << endl; return 0; }