Strength gives you the confidence within yourself to overcome any fears, challenges or doubts. Feel the fear and do it anyway! If you have been going through a rough time and feel burnt out or stressed, the Strength card encourages you to find the strength within yourself and keep going. You have got what it takes to see this situation through to its eventual end. You might also feel compelled to hold space for someone else who is going through a difficult period and needs your strength and support. 

Alice and Bob are playing “Yu-Gi-Oh!”, a famous turn-based trading card game, in which two players perform their turns alternatively. After several turns, Alice and Bob have many monsters respectively. 
Alice has n and Bob has m monsters under their own control. Each monster’s strength is measured by a non-negative integer sisi . To be specific, the larger sisi is, the more power the monster has. 
During each turn, for every single monster under control, the player can give a command to it at most once, driving it to battle with an enemy monster (given that opposite player has no monsters as a shield, the monster can directly attack him). 
Additionally, the process of the battle is also quite simple. When two monsters battle with each other, the stronger one (i.e. the one with larger sisi) will overwhelm the other and destroy it and the winner’s strength will remain unchanged. Meanwhile, the difference of their strength will produce equivalent damage to the player who loses the battle. If the player is directly attacked by a monster, he will suffer from the damage equal to the monster’s strength. Notice that when two monsters have the same strength, both of them will vanish and no damage will be dealt. 
Right now it is Alice’s turn to play, having known the strength of all monsters, she wants to calculate the maximal damage she can deal towards Bob in one turn. Unfortunately, Bob has great foresight and is well-prepared for the upcoming attack. Bob has converted several of his monsters into defense position, 
in which even if the monster is destroyed, he wouldn’t get any damage. 
Now you are informed of the strength of all the monsters and whether it is in defense position for each Bob’s monster, you are expected to figure out the maximal damage that could be dealt in this turn.

Input

The first line contains a single integer TT ≤ 20 indicating the number of test cases. 
For each test case, the first line includes two integer 0 ≤ nn, mm ≤ 100000, representing the number of monsters owned by Alice and Bob. 
In next three lines, the first two lines include nn and mm integers 0 ≤ sisi ≤ 109109indicating the strength of the ii-th monster, separated by spaces. The last line contains m integers 0 or 1 indicating the position of Bob’s ii-th monsters.In other words, 0 represents the normal position and 1 represents the defense position.

Output

For the ith test, output a single line in beginning of “Case ii:”, followed by an integer indicating the answer, separated by a single space.

Sample Input

2
4 2
10 10 10 20
5 15
0 1
4 2
10 10 10 20
5 25
0 1

Sample Output

Case 1: 25
Case 2: 15

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
struct node{
	int fl;
	int gong;
}x[100001];
int a[100001];
int b[100001];
bool cmp(node p,node q)
{
	if(p.fl!=q.fl)
	return p.gong<q.gong;
	else
	return p.fl<q.fl;
}
bool cnp(node p,node q)
{
	return p.gong>q.gong;
}
bool ccp(int p,int q)
{
	return p>q;
}
int main()
{
	int t;
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		long long n,m,ans1=0;
		cin>>n>>m;
		for(int j=1;j<=n;j++)
		{
			cin>>a[j];
		}
		sort(a+1,a+1+n);
		for(int j=1;j<=m;j++)
		{
			cin>>x[j].gong;
		}
		for(int j=1;j<=m;j++)
		{
			cin>>x[j].fl;
		}
		sort(x+1,x+1+m,cnp);
		int flag=0,k=1;
		if(n>=m)
		{
			for(int j=n;j>=1;j--)
			{
				if(a[j]>=x[k].gong)
				{
					k++;
					continue;
				}
				else
				{
					flag=1;
					break;
				}
			}
		}
		else
		{
			flag=1;
		}
		if(flag==1)
		{
			sort(x+1,x+1+m,cmp);
			sort(a+1,a+1+n,ccp);
			for(int j=1;j<=m;j++)
			{
				if(x[j].fl==1)
				break;
				ans1+=a[j]-x[j].gong;
			}
			cout<<"Case "<<i<<": "<<ans1<<endl;
		}
		else
		{
			sort(x+1,x+1+m,cnp);
			sort(a+1,a+1+n,ccp);
			int u=1;
			ans1=0;
			for(int j=1;j<=m;j++)
			{
				if(x[j].fl==0)
				ans1=ans1-x[j].gong;
				else
				{
					while(a[u]>=x[j].gong)
					{
						u++;
					}
					u--;
					while(b[u]!=0)
					{
						u--;
					}
					b[u]=1;
				}
			}
			for(int j=1;j<=n;j++)
			{
				if(b[j]==0)
				ans1=ans1+a[j];
			} 
			cout<<"Case "<<i<<": "<<ans1<<endl;
		}
	}
}