The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have 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 will have to pay M$12 to the shop.
Each coupon and each product may be selected at most once. Your task is to get as much money back as possible.
Input Specification:
Each input file contains 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 products NP, followed by a line with NP product values. Here 1<= NC, NP <= 105, and it is guaranteed that all the numbers will not exceed 230.
Output Specification:
For each test case, simply print in a line the maximum amount of money you can get back.
Sample Input:4 1 2 4 -1 4 7 6 -2 -3Sample Output:
43
题目大意:
火星上的魔法商店提供一些魔法优惠券。每张优惠券都有一个整数N,这意味着当你使用这个优惠券时,你可能会得到这个产品的N倍的价值!此外,该商店还提供一些免费的商品奖励。如果你用一个带有正数N的优惠券购买商品,你将必须支付商店N倍于奖励品的价值。。。。。。但是,嘿,神奇的是,他们有一些负N的优惠券!
例如,给定一组优惠券{1 2 4 -1},以及一组产品值{7 6 -2 -3}(在火星美元M$),其中负值对应一个奖励品。你可以将优惠券3(N为4)应用到产品1(价值为7$)来得到28$,将优惠券2应用到商品2得到12$;将优惠券4应用到商品4得到3$.另一方面,如果你将优惠券3应用到产品4上,你将必须向商店支付12$.
每张优惠券和每个产品最多可以选择一次。你的任务是尽可能的多得到钱。
输出规格:
每个输入文件包含一个测试用例。对于每种情况,第一行都包含了优惠券NC的数量,然后是NC个优惠券。然后下一行包含了产品NP的数量,然后是NP个产品值。1<= NC, NP <= 105,保证所有数字不超过2的30次方。
输出规范:
对于每个测试用例,只需在一行中打印返回的最大金额。
代码:
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
vector<long long> arrp1;
vector<long long> arrp2;
vector<long long> arrn1;
vector<long long> arrn2;
bool cmpp(long long a,long long b)
{
return a>b;
}
bool cmpn(long long a,long long b)
{
return a<b;
}
int main()
{
int i,j;
long long n,m,k,t,flag;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
scanf("%lld",&k);
if(k>=0)
arrp1.push_back(k);
else
arrn1.push_back(k);
}
scanf("%lld",&m);
for(i=1;i<=m;i++)
{
scanf("%lld",&k);
if(k>=0)
arrp2.push_back(k);
else
arrn2.push_back(k);
}
sort(arrp1.begin(),arrp1.end(),cmpp);
sort(arrp2.begin(),arrp2.end(),cmpp);
sort(arrn1.begin(),arrn1.end(),cmpn);
sort(arrn2.begin(),arrn2.end(),cmpn);
t=0;
for(i=0;i<arrp1.size()&&i<arrp2.size();i++)
{
t+=arrp1[i]*arrp2[i];
}
for(i=0;i<arrn1.size()&&i<arrn2.size();i++)
{
t+=arrn1[i]*arrn2[i];
}
printf("%lld\n",t);
return 0;
}