#include #include #include using namespace std; int main() { int n; cin >> n; vector product_prices(n); for (int i = 0; i < n; i++) { cin >> product_prices[i]; } vector total_prices(1 << n, numeric_limits::max()); total_prices[0] = 0; for (int i = 0; i < total_prices.size() - 1; i++) { int total_price = 0; for (int j = 0; j < n; j++) { if ((i >> j) & 1 == 1) total_price += product_prices[j]; } for (int j = 0; j < n; j++) { if ((i >> j) & 1 == 1) continue; int price = max(0, product_prices[j] - (total_price % 1000)); total_prices[i | (1 << j)] = min(total_prices[i | (1 << j)], total_prices[i] + price); } } cout << total_prices[total_prices.size() - 1] << endl; return 0; }