#include using namespace std; const int INF = 1 << 30; template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); 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 b = 0; b < (1 << N); b++) { int sum = 0; for (int i = 0; i < N; i++) { if ((b >> i) & 1) sum += M[i]; } for (int i = 0; i < N; i++) { if (((b >> i) & 1) == 0) chmin(dp[b | (1 << i)], dp[b] + max(0, M[i] - (sum % 1000))); } } cout << dp[(1 << N) - 1] << '\n'; return 0; }