#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() typedef long long ll; using namespace std; const int inf = INT_MAX / 2; const ll infl = 1LL << 62; const double PI = 2 * acos(0.0); templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; } int N; ll M[20]; ll dp[40000]; int main() { cin >> N; for (int i = 0; i < N; i++) cin >> M[i]; for (int i = 0; i < 1 << N; i++) dp[i] = infl; dp[0] = 0; for (int msk = 0; msk < 1 << N; msk++) { ll total = 0; for (int i = 0; i < N; i++) if (msk & 1 << i) total += M[i]; for (int k = 0; k < N; k++) { if (msk & 1 << k) continue; ll res = max(0LL, M[k] - total % 1000); chmin(dp[msk + (1 << k)], dp[msk] + res); } } //for (int i = 0; i < 1 << N; i++) cout << dp[i] << endl; cout << dp[(1 << N) - 1] << endl; }