每日一题 第五十一期 Codeforces Round 871 (Div. 4)

C. Mr. Perfectly Fine

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Victor wants to become “Mr. Perfectly Fine”. For that, he needs to acquire a certain set of skills. More precisely, he has 2 2 2 skills he needs to acquire.

Victor has n n n books. Reading book i i i takes him m i m_i mi minutes and will give him some (possibly none) of the required two skills, represented by a binary string of length 2 2 2.

What is the minimum amount of time required so that Victor acquires all of the two skills?

Input

The input consists of multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the number of books available.

Then n n n lines follow. Line i i i contains a positive integer m i m_i mi ( 1 ≤ m i ≤ 2 ⋅ 1 0 5 1 \leq m_i \leq 2 \cdot 10^5 1mi2105) and a binary string of length 2 2 2, where s i 1 = 1 s_{i1} = 1 si1=1 if reading book i i i acquires Victor skill 1 1 1, and s i 1 = 0 s_{i1} = 0 si1=0 otherwise, and s i 2 = 1 s_{i2} = 1 si2=1 if reading book i i i acquires Victor skill 2 2 2, and s i 2 = 0 s_{i2} = 0 si2=0 otherwise.

It is guaranteed that the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer denoting the minimum amount of minutes required for Victor to obtain both needed skills and − 1 -1 1 in case it’s impossible to obtain the two skills after reading any amount of books.

Example
inputCopy
6
4
2 00
3 10
4 01
4 00
5
3 01
3 01
5 01
2 10
9 10
1
5 11
3
9 11
8 01
7 10
6
4 01
6 01
7 01
8 00
9 01
1 00
4
8 00
9 10
9 11
8 11
outputCopy
7
5
5
9
-1
8

Note

In the first test case, we can use books 2 2 2 and 3 3 3, with a total amount of minutes spent equal to 3 + 4 = 7 3 + 4 = 7 3+4=7.

In the second test case, we can use the books 1 1 1 and 4 4 4, with a total amount of minutes spent equal to 3 + 2 = 5 3 + 2 = 5 3+2=5.

In the third test case, we have only one option and that is reading book 1 1 1 for a total amount of minutes spent equal to 5 5 5.

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
//#define endl '\n'
using namespace std;

typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e8 + 10;

int t;

int main()
{
	cin >> t;
	while(t --){
		int n, ans = INF;
		string a;
		cin >> n;
		int w, max1 = INF, max2 = INF, max3 = INF;
		while(n --){
			cin >> w >> a;
			if(a[0] == '1' && a[1] != '1') 
			{
				if(w < max1) max1 = w;
			}
			if(a[0] != '1' && a[1] == '1') 
			{
				if(w < max2) max2 = w;
			}
			if(a[0] == '1' && a[1] == '1')
			{
				if(w < max3) max3 = w;
			}
		}
		if((max1 != INF && max2 != INF) || max3 != INF)
		{
			ans = min(max1 + max2, max3);
			cout << ans << endl;
		}
		else puts("-1");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值