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 negative 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 <=5, and it's guaranteed that all the numbers would not exceed 2.
Output Specification:
For each test case, simply print in a line the maximum amount of money can get back.
#include <stdio.h> #include <stdlib.h> #include <vector> #include <algorithm>using namespace std;vector<long> cp;vector<long> cn;vector<long> pp;vector<long> pn;int main () {int cup,pro,i ; Long temp,sum=0; scanf ("%d", &cup); for (i=0;i<cup;i++) {scanf ("%ld", &temp); if (temp>=0) cp.push_back (temp); if (temp<0) cn.push_back (temp); } scanf ("%d", &pro); for (i=0;i<pro;i++) {scanf ("%ld", &temp); if (temp>=0) pp.push_back (temp); if (temp<0) pn.push_back (temp); } sort (Cp.begin (), Cp.end ()); Reverse (Cp.begin (), Cp.end ()); Sort (Cn.begin (), Cn.end ()); Sort (Pp.begin (), Pp.end ()); Reverse (Pp.begin (), Pp.end ()); Sort (Pn.begin (), Pn.end ()); int cps=cp.size (), Cns=cn.size (), Pps=pp.size (), pns=pn.size (); int Pmin=min (CPS,PPS), Nmin=min (Cns,pns); for (i=0;i<pmin;i++) sum+=cp[i]*pp[i]; for (i=0;i<nmin;i++) sum+=cn[i]*Pn[i]; printf ("%ld\n", sum); System ("pause"); return 0; }
1037. Magic Coupon