The Magic shop in Mars is offering some magic coupons. Each coupon have an integer n printed on it, meaning if you use this coupon with a product, you may get n times the Value of that product back! What's more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive n to this bonus product, and you'll have the. The shop N times the value of The bonus product ... but hey, magically, they has some coupons with negative N ' s!
For example, given a set of coupons {1 2 4-1}, and a set of product values {7 6-2-3} (in Mars dollars m$) where a Negat Ive value corresponds to a bonus product. You can apply coupon 3 (with N being 4) to product 1 (with value m$7) to get m$28 back; Coupon 2 to Product 2 to get m$12 back; and coupon 4 to product 4 to get m$3 back. On the other hand, if you apply coupon 3 to product 4, you'll have a to pay m$12 to the shop.
Each coupon and each product is selected at the most once. Your task is to get as much money back as possible.
Input Specification:
Each input file contains the one test case. For each case, the first line contains the number of coupons NC, followed by a line with NC coupon integers. Then the next line contains the number of product NP, followed by a line with NP product values. Here 1<= NC, NP <=, and it's guaranteed that all the numbers would not exceed 230.
Output Specification:
For each test case, simply print in a line the maximum amount of money can get back.
Sample Input:
4
1 2 4-1
4
7 6-2-3
Sample Output:
43
#include <iostream> #include <algorithm> #include <vector>using namespace std;vector<int> nc_p ;vector<int> nc_n;vector<int> np_p;vector<int> np_n;bool cmp1 (int a, int b) {return a > B;} BOOL Cmp2 (int a, int b) {return a < b;} int main () {int NC, NP, I, TMP; while (CIN>>NC) {for (i=0; i<nc; i++) {cin>>tmp; if (tmp >= 0) {nc_p.push_back (TMP); } else {nc_n.push_back (TMP); }} cin>>np; for (i=0; i<np; i++) {cin>>tmp; if (tmp >= 0) {np_p.push_back (TMP); } else {np_n.push_back (TMP); }} sort (Nc_p.begin (), Nc_p.end (), CMP1); Sort (Nc_n.begin (), Nc_n.end (), CMP2); Sort (Np_p.begin (), Np_p.end (), CMP1); Sort (Np_n.begin (), Np_n.end (), CMP2); int sum = 0; for (i=0; i≪nc_p.size () && i<np_p.size (); i++) {sum + = nc_p[i] * Np_p[i]; } for (i=0; I<nc_n.size () && i<np_n.size (); i++) {sum + = nc_n[i] * Np_n[i]; } cout<<sum<<endl; Nc_p.clear (); Nc_n.clear (); Np_p.clear (); Np_n.clear (); } return 0;}
1037. Magic Coupon (25)