/* -*- coding: utf-8 -*- * * 286.cc: No.286 Modulo Discount Store - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 15; const int NBITS = 1 << MAX_N; const int INF = 1 << 30; /* typedef */ int ms[MAX_N], sums[NBITS], dp[NBITS]; /* global variables */ /* subroutines */ /* main */ int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> ms[i]; int nbits = 1 << n; for (int bits = 0; bits < nbits; bits++) { sums[bits] = 0; for (int i = 0, bi = 1; i < n; i++, bi <<= 1) if (bits & bi) sums[bits] += ms[i]; } for (int bits = 0; bits < nbits; bits++) dp[bits] = INF; dp[0] = 0; for (int bits = 0; bits < nbits; bits++) { int dc = sums[bits] % 1000; for (int i = 0, bi = 1; i < n; i++, bi <<= 1) if (! (bits & bi)) { int bits0 = bits | bi; int d = dp[bits] + ((ms[i] >= dc) ? ms[i] - dc : 0); if (dp[bits0] > d) dp[bits0] = d; } } printf("%d\n", dp[nbits - 1]); return 0; }