#include #include #include typedef std::int_fast32_t s32; typedef std::uint_fast32_t u32; typedef std::int_fast64_t s64; typedef std::uint_fast64_t u64; const unsigned long mod = 1000000007; const double EPS = 0.00000001; const int INF = (1 << 29); typedef std::pair pii; template void print_array(T *A_, s32 size) { //printf("%d\n", size); if( size != 0 ) { for(int i = 0; i < size - 1; ++i) { printf("%d ", A_[i]); } printf("%d\n", A_[size - 1]); } } auto solve(int n, std::vector &m) -> int { int mod = 0; int res = 0; for(int i = 0; i < n; ++i) { res += std::max(0, m[i] - mod); mod += m[i]; mod %= 1000; } return res; } auto main() -> int { int n; std::vector m; std::cin >> n; for(int i = 0; i < n; ++i) { int temp; std::cin >> temp; m.push_back(temp); } std::sort(m.begin(), m.end()); int res = INF; do { res = std::min(res, solve(n, m)); } while( next_permutation(m.begin(), m.end()) ); std::cout << res << std::endl; return 0; }