#include #define rep(i,n,m) for(int i = (n); i <(m); i++) #define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--) #define chmin(a,b) a = min((a),(b)) using namespace std; using ll = long long; const int MAX_S = 1 << 17; const int INF = 1000000000; int main() { int n; cin >> n; vector money(n); rep(i, 0, n) cin >> money[i]; vector> dp(n+1, vector(MAX_S, INF)); dp[0][0] = 0; rep(i, 0, n) rep(prev, 0, MAX_S) rep(j, 0, n) if ((prev & (1 << j)) == 0 and dp[i][prev] != INF) { int next = prev | (1 << j); int prev_cost = 0; rep(k, 0, n) if ((1 << k) & prev) prev_cost += money[k]; chmin(dp[i+1][next], dp[i][prev] + max(0, money[j] - (prev_cost % 1000))); // cout << i << ' ' << next << ' ' << prev << ' ' << dp[i+1][next] << endl; } int state = (1 << n) - 1; cout << dp[n][state] << endl; // cout << cost << endl; return 0; }