C. Mr. Perfectly Fine

time limit per test

2 seconds

memory limit per test

256 megabytes

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

Victor has nn books. Reading book ii takes him mimi minutes and will give him some (possibly none) of the required two skills, represented by a binary string of length 22.

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 tt (1≤t≤10001≤t≤1000) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) --- the number of books available.

Then nn lines follow. Line ii contains a positive integer mimi (1≤mi≤2⋅1051≤mi≤2⋅105) and a binary string of length 22, where si1=1si1=1 if reading book ii acquires Victor skill 11, and si1=0si1=0 otherwise, and si2=1si2=1 if reading book ii acquires Victor skill 22, and si2=0si2=0 otherwise.

It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

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 in case it's impossible to obtain the two skills after reading any amount of books.

Example

Input

Copy

复制代码

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

Output

Copy

复制代码
7
5
5
9
-1
8

Note

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

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

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

解题说明:此题是一道模拟题,可以采用贪心算法,找出包含1和10的耗费最小时间组合或者是直接包含11的最小时间,两者进行比较即可。

cpp 复制代码
#include <stdio.h>

int main()
{
	int t = 0, n = 0, a = 0, s = 0;
	scanf("%d", &t);
	while (t--) 
	{
		scanf("%d", &n);
		int min_1 = 200005, min_2 = 200005, min_3 = 2000005;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &a, &s);
			if (1 == s && min_1 > a)
			{
				min_1 = a;
			}
			else if (10 == s && min_2 > a)
			{
				min_2 = a;
			}
			else if (11 == s && min_3 > a)
			{
				min_3 = a;
			}
		}
		if ((min_1 == 200005 || min_2 == 200005) && min_3 == 2000005)
		{
			printf("-1\n");
		}
		else if (min_1 + min_2 >= min_3)
		{
			printf("%d\n", min_3);
		}
		else
		{
			printf("%d\n", min_1 + min_2);
		}
	}
	return 0;
}
相关推荐
机器视觉的发动机8 分钟前
AI算力中心的能耗挑战与未来破局之路
开发语言·人工智能·自动化·视觉检测·机器视觉
HyperAI超神经15 分钟前
在线教程|DeepSeek-OCR 2公式/表格解析同步改善,以低视觉token成本实现近4%的性能跃迁
开发语言·人工智能·深度学习·神经网络·机器学习·ocr·创业创新
R_.L26 分钟前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
Zach_yuan34 分钟前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
云姜.40 分钟前
java多态
java·开发语言·c++
CoderCodingNo1 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
陳10301 小时前
C++:红黑树
开发语言·c++
VekiSon1 小时前
Linux内核驱动——杂项设备驱动与内核模块编译
linux·c语言·arm开发·嵌入式硬件
一切尽在,你来1 小时前
C++ 零基础教程 - 第 6 讲 常用运算符教程
开发语言·c++
泉-java1 小时前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言