#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i, n) for(int i = 0;i < n;i++) #define REPR(i, n) for(int i = n;i >= 0;i--) #define FOR(i, m, n) for(ll i = m;i < n;i++) #define FORR(i, m, n) for(ll i = m;i >= n;i--) #define REPO(i, n) for(int i = 1;i <= n;i++) #define ll long long #define INF 1999999999 #define MINF -1999999999 #define INF64 1999999999999999999 #define ALL(n) n.begin(),n.end() #define MOD 1000000007 ll n, s[20], dp[110000]; int main() { REPO(i, 100000) dp[i] = INF; cin >> n; REP(i, n) cin >> s[i]; REP(i, 1 << n) { ll now = 0; REP(j, n) if (i & 1 << j) now += s[j]; REP(j, n) { if (!(i & 1 << j)) { dp[i + (1 << j)] = min(dp[i + (1 << j)], (dp[i] + max(0ll, s[j] - now % 1000))); } } } cout << dp[(1 << n) - 1] << endl; }