#include //#define int long long using namespace std; using LL = long long; using P = pair; using Tapris = tuple; #define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define pb(a) push_back(a) #define all(x) (x).begin(),(x).end() const int INF = (int)1e9; const LL INFL = (LL)1e15; const int MOD = 1e9 + 7; int dy[]={0, 0, 1, -1, 0}; int dx[]={1, -1, 0, 0, 0}; /*************** using variables ***************/ int n; vector m; int dp[1 << 16]; /**********************************************/ bool contain(int mask, int pos){ return ((mask & (1 << pos)) != 0); } signed main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> n; m.assign(n, 0); REP(i, n) cin >> m[i]; REP(i, 1 << 16) dp[i] = INF; dp[0] = 0; for(int mask = 1; mask < (1 << 16); mask++){ REP(i, n){ if(contain(mask, i)){ int sum = 0; REP(j, n){ if(contain(mask, j) && j != i){ sum += m[j]; } } dp[mask] = min(dp[mask], dp[mask ^ (1 << i)] + max(m[i] - (sum % 1000), 0)); } } } cout << dp[(1 << n) - 1] << endl; }