PAT 甲级 1037 Magic Coupon
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n_coupons, n_product;
cin >> n_coupons;
vector<int> coupons(n_coupons);
for (auto& i : coupons) cin >> i;
cin >> n_product;
vector<int> product(n_product);
for (auto& i : product) cin >> i;
sort(coupons.rbegin(), coupons.rend());
sort(product.rbegin(), product.rend());
int i = 0;
int ans = 0;
for (i = 0; i < n_coupons && i < n_product && coupons[i] * product[i] > 0; ++i) {
ans += coupons[i] * product[i];
}
int itr2 = n_product - 1, itr1 = n_coupons - 1;
for (; itr2 >= i && itr1 >= i && coupons[itr1] * product[itr2] > 0; --itr1, --itr2) {
ans += coupons[itr1] * product[itr2];
}
cout << ans;
}