#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using i64 = long long int; using ui64 = unsigned long long int; using P = pair; constexpr int INF = (1 << 30); constexpr i64 INF64 = (1LL << 60); constexpr int MOD = 1000000000 + 7; int dx[] = { -1, 0, 1, 0 }; int dy[] = { 0, 1, 0, -1 }; template static inline std::vector makeNdVector(T n, U val) noexcept { static_assert(std::is_integral::value, "[makeNdVector] 1st argument must be an integer type"); return std::vector(std::forward(n), std::forward(val)); } template static inline decltype(auto) makeNdVector(T n, Args... args) noexcept { static_assert(std::is_integral::value, "[makeNdVector] 1st argument must be an integer type"); return std::vector(args)...))>(std::forward(n), makeNdVector(std::forward(args)...)); } template void print(vector vec) { for (auto &e : vec) cout << e << " "; cout << endl; } template T gcd(T x, T y) { if (x == 0 || y == 0) return 0; T r; while ((r = x % y) != 0) { x = y; y = r; } return y; } template T lcm(T x, T y) { if (x == 0 || y == 0) return 0; return (x / gcd(x, y) * y); } int next_combination(int sub) { int x = sub & -sub, y = sub + x; return (((sub & ~y) / x) >> 1) | y; } std::uint64_t combination(int n, int r) { if (n < 0 || r < 0) return 0; if (n < r) return 0; std::uint64_t k = 1; for (std::uint64_t d = 1; d <= r; ++d) { k *= n--; k /= d; } return k; } bool check(string str) { for (int i = 0; i < str.length() / 2; ++i) { if (str[i] != str[str.length() - 1 - i]) { return false; } } return true; } signed main() { cin.tie(0); ios::sync_with_stdio(false); using P = pair; int N; cin >> N; vector M(N); for (int i = 0; i < N; ++i) cin >> M[i]; vector dp(1 << N, INF); dp[0] = 0; for (int i = 0; i < (1 << N); ++i) { i64 sum = 0; for (int j = 0; j < N; ++j) { if ((i&(1 << j))) sum += M[j]; } sum %= 1000; for (int j = 0; j < N; ++j) { if ((i&(1 << j)) == 0) { i64 cost = M[j] - sum; if (cost < 0) cost = 0; dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + cost); } } } cout << dp[(1 << N) - 1] << endl; }