#include #define mp make_pair #define mt make_tuple #define pb push_back #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair pll; const int INF=1<<29; const double EPS=1e-9; const int MOD = 100000007; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; int N; vector M; ll dp[1 << 16]; ll memo(int bit){ if (__builtin_popcount(bit) == N){ return 0; } ll &res = dp[bit]; if (res >= 0){ return res; } res = INF; ll tmp = 0; vector next; for (int j = 0; j < N; j++){ if (bit >> j & 1){ tmp += M[j]; }else{ next.push_back(j); } } tmp = tmp % 1000; for (int i = 0; i < next.size(); i++){ res = min(res, memo(bit | 1 << next[i]) + max(M[next[i]] - tmp, 0LL)); } return res; } int main(){ cin >> N; M.resize(N); for (int i = 0; i < N; i++){ cin >> M[i]; } memset(dp, -1, sizeof(dp)); cout << memo(0) << endl; }