#include using namespace std; const int kINF = 1 << 28; const int kMAX_N = 15; int dp[1 << kMAX_N][1000]; int N, M[kMAX_N]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; for (int i = 0; i < N; i++) { cin >> M[i]; } fill((int* )dp, (int* )(dp + (1 << N)), kINF); dp[0][0] = 0; int ans = kINF; for (int mask = 0; mask < (1 << N); mask++) { for (int mod1000 = 0; mod1000 < 1000; mod1000++) { if (dp[mask][mod1000] >= kINF) continue; for (int i = 0; i < N; i++) { if ((mask >> i) & 1) continue; dp[mask | (1 << i)][(mod1000 + M[i]) % 1000] = min(dp[mask | (1 << i)][(mod1000 + M[i]) % 1000], dp[mask][mod1000] + max(0, M[i] - mod1000)); } if (mask == (1 << N) - 1) ans = min(ans, dp[mask][mod1000]); } } cout << ans << endl; return 0; }