#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define int long long int N, M[15]; signed main() { cin >> N; for (int i = 0; i < N; i++) { cin >> M[i]; } int dp[1 << 15]; fill_n(dp, 1 << 15, LLONG_MAX / 2); dp[0] = 0; for (int i = 0; i <= N; i++) { for (int j = (1 << N) - 1; j >= 0; j--) { int sum = 0; for (int k = 0; k < N; k++) { sum += M[k] * ((j >> k) & 1); } if (dp[j] == LLONG_MAX / 2) continue; for (int k = 0; k < N; k++) { if (((j >> k) & 1)) continue; dp[j | (1 << k)] = min(dp[j | (1 << k)], dp[j] + max(0ll, M[k] - sum % 1000)); } } } printf("%lld\n", dp[(1 << N) - 1]); return 0; }