#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; int N, M[15], dp[1 << 15], MD = 1000; int main(){ ios::sync_with_stdio(0); cin.tie(0); while(cin >> N){ rep(i, N)cin >> M[i]; rep(S, 1 << N)dp[S] = INT_MAX; dp[0] = 0; rep(S, 1 << N){ int wari = 0; rep(i, N)if(S >> i & 1)wari += M[i]; wari %= MD; rep(i, N)if(!(S >> i & 1)) smin(dp[S | (1 << i)], dp[S] + max(0, M[i] - wari)); } cout << dp[(1 << N) - 1] << endl; } }