#include using namespace std; #include #include using namespace atcoder; //基本的にこれがなくてもうまくいくコードを心がけるが、念の為。時間ギリギリアウトの時は消してみる #define int long long typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair pi; typedef pair pl; typedef vector vi; typedef vector vl; typedef vector > vvi; typedef vector > vvl; typedef priority_queue pq; typedef priority_queue, greater > pqg; typedef priority_queue pql; typedef priority_queue, greater > pqgl; #define pb push_back #define eb emplace_back #define mp make_pair #define all(obj) (obj).begin(), (obj).end() #define reps(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define cbit(x) __builtin_popcountll(x) #define minv(v) *min_element(v.begin(), v.end()) #define maxv(v) *max_element(v.begin(), v.end()) #define print(v) \ for (auto k : v) cout << k << " "; \ cout << endl; #define yesno(bool) \ if (bool) { \ cout << "Yes" << endl; \ } else { \ cout << "No" << endl; \ } //既存値aより新規値bが大きければ更新 template bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } //既存値aより新規値bが小さければ更新 template bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define ednl endl //打ち間違え用 #define enld endl #define Endl endl int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; // int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}; // int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; // const int MOD = 998244353; //素数(223*119+1) // const int MOD = 1000000007; //素数 // using mint = modint998244353; // using mint = modint1000000007; // using mint = static_modint<1000000009>; //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// signed main() { int n; cin >> n; vi m(n); rep(i, n) cin >> m[i]; int inf = 1e8; vi d(1 << n, inf); d[0] = 0; rep(msk, 1 << n) { int sum = 0; rep(i, n) { if (msk & (1 << i)) { sum += m[i]; } } rep(i, n) { if (msk & (1 << i)) { d[msk] = min(d[msk], d[msk & ~(1 << i)] + m[i] - min(m[i], (sum - m[i]) % 1000)); } } } cout << d[(1 << n) - 1] << endl; }