1738A. Glory Addicts(Codeforces Global Round 22)

A. Glory Addicts

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

The hero is addicted to glory, and is fighting against a monster.

The hero has nn skills. The ii-th skill is of type aiai (either fire or frost) and has initial damage bibi.

The hero can perform all of the nn skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic as follows:

  • If the current skill immediately follows another skill of a different type, then its damage is doubled.

In other words,

  1. If a skill of type fire and with initial damage cc is performed immediately after a skill of type fire, then it will deal cc damage;
  2. If a skill of type fire and with initial damage cc is performed immediately after a skill of type frost, then it will deal 2c2c damage;
  3. If a skill of type frost and with initial damage cc is performed immediately after a skill of type fire, then it will deal 2c2c damage;
  4. If a skill of type frost and with initial damage cc is performed immediately after a skill of type frost , then it will deal cc damage.

Your task is to find the maximum damage the hero can deal.

Input

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤1051≤t≤105) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (1≤n≤1051≤n≤105), indicating the number of skills.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1), where aiai indicates the type of the ii-th skill. Specifically, the ii-th skill is of type fire if ai=0ai=0, and of type frost if ai=1ai=1.

The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109), where bibi indicates the initial damage of the ii-th skill.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case, output the maximum damage the hero can deal.

Example

input

Copy

 

4

4

0 1 1 1

1 10 100 1000

6

0 0 0 1 1 1

3 4 5 6 7 8

3

1 1 1

1000000000 1000000000 1000000000

1

1

1

output

Copy

2112
63
3000000000
1

Note

In the first test case, we can order the skills by [3,1,4,2][3,1,4,2], and the total damage is 100+2×1+2×1000+10=2112100+2×1+2×1000+10=2112.

In the second test case, we can order the skills by [1,4,2,5,3,6][1,4,2,5,3,6], and the total damage is 3+2×6+2×4+2×7+2×5+2×8=633+2×6+2×4+2×7+2×5+2×8=63.

In the third test case, we can order the skills by [1,2,3][1,2,3], and the total damage is 1000000000+1000000000+1000000000=30000000001000000000+1000000000+1000000000=3000000000.

In the fourth test case, there is only one skill with initial damage 11, so the total damage is 11.

挺有趣的一个题

题意就是有两种技能,如果两个技能间隔释放那么下一个技能伤害翻倍,比如第一次使用火焰技能下一次使用冻结技能伤害翻倍,但是如果下一次使用的是火焰技能伤害不变,求怎样放技能伤害最高。

我们可以对两个技能伤害分别进行排序,哪个技能最多就先释放技能最多的最小伤害,这样就能保证技能伤害高的技能吃到增幅从而打出成吨伤害。

这样操作完后剩下的技能就可以把伤害全部乘以2了。



#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int manx = 2e5 + 10;
int a[manx];
int b[manx];
bool cmp(int x, int y)
{
	return x>y;
}
void solve()
{
	vector<int>x, y;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		cin >> b[i];
	for (int i = 0; i < n; i++)
	{
		if (a[i])
		{
			x.push_back(b[i]);
		}
		else
		{
			y.push_back(b[i]);
		}
	}
	sort(x.begin(), x.end(),cmp);
	sort(y.begin(), y.end(),cmp);
	bool flag = false;
	long long sum = 0;
	while (x.size()>y.size())
	{
		sum += x.back(); x.pop_back();
		flag = true;
	}
	while (x.size()<y.size())
	{
		sum += y.back(); y.pop_back();
		flag = true;
	}
	if (!flag)//如果长度相等就先释放技能伤害最低的
	{
		if (x.back() <= y.back())
		{
			sum += x.back(); x.pop_back();
		}
		else
		{
			sum += y.back(); y.pop_back();
		}
	}
	while (x.size())
	{
		sum += 2 * x.back(); x.pop_back();
	}
	while (y.size())
	{
		sum += 2 * y.back(); y.pop_back();
	}
	cout << sum << endl;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		solve();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值