1037. Magic Coupon (25)
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:
41 2 4-147 6-2-3
Sample Output:
43
Apply a sort inequality. The number is divided into integers and negative numbers, and the maximum (positive) coupon matches the product with the maximum (positive) value and the minimum (negative) value of the minimum (negative) match.
1#include <iostream>2#include <cstdio>3#include <vector>4#include <algorithm>5 6 using namespacestd;7 8 BOOLcmpintInteger1,intinteger2)9 {Ten returnInteger1 >Integer2; One } A - intMain () - { the intNC, NP; -vector<int>positivecoupons, Negativecoupons, Positiveproduct, negativeproduct; -CIN >>NC; - for(inti =0; i < NC; i++) + { - intcoupon; +scanf"%d", &coupon); A if(Coupon >0) at positivecoupons.push_back (coupon); - Else if(Coupon <0) - negativecoupons.push_back (coupon); - - } - sort (Positivecoupons.begin (), Positivecoupons.end (), CMP); in sort (Negativecoupons.begin (), Negativecoupons.end ()); -CIN >>NP; to for(inti =0; i < NP; i++) + { - intproduct; thescanf"%d", &product); * if(Product >0) $ positiveproduct.push_back (product);Panax Notoginseng Else if(Product <0) - negativeproduct.push_back (product); the + } A sort (Positiveproduct.begin (), Positiveproduct.end (), CMP); the sort (Negativeproduct.begin (), Negativeproduct.end ()); + - intMaxmonmey =0; $ for(inti =0; I < positivecoupons.size () && i < positiveproduct.size (); i++) $Maxmonmey + = positivecoupons[i] *Positiveproduct[i]; - for(inti =0; I < Negativecoupons.size () && negativeproduct.size (); i++) -Maxmonmey + = negativecoupons[i] *Negativeproduct[i]; the -cout <<Maxmonmey;Wuyi}
PAT 1037. Magic Coupon (25)