当前位置: 首页 > news >正文

B. Olya and Game with Arrays

time limit per test

1 second

memory limit per test

256 megabytes

Artem suggested a game to the girl Olya. There is a list of nn arrays, where the ii-th array contains mi≥2mi≥2 positive integers ai,1,ai,2,…,ai,miai,1,ai,2,…,ai,mi.

Olya can move at most one (possibly 00) integer from each array to another array. Note that integers can be moved from one array only once, but integers can be added to one array multiple times, and all the movements are done at the same time.

The beauty of the list of arrays is defined as the sum ∑ni=1minmij=1ai,j∑i=1nminj=1miai,j. In other words, for each array, we find the minimum value in it and then sum up these values.

The goal of the game is to maximize the beauty of the list of arrays. Help Olya win this challenging game!

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤250001≤t≤25000) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤250001≤n≤25000) — the number of arrays in the list.

This is followed by descriptions of the arrays. Each array description consists of two lines.

The first line contains a single integer mimi (2≤mi≤500002≤mi≤50000) — the number of elements in the ii-th array.

The next line contains mimi integers ai,1,ai,2,…,ai,miai,1,ai,2,…,ai,mi (1≤ai,j≤1091≤ai,j≤109) — the elements of the ii-th array.

It is guaranteed that the sum of mimi over all test cases does not exceed 5000050000.

Output

For each test case, output a single line containing a single integer — the maximum beauty of the list of arrays that Olya can achieve.

Example

Input

Copy

 

3

2

2

1 2

2

4 3

1

3

100 1 6

3

4

1001 7 1007 5

3

8 11 6

2

2 9

Output

Copy

5
1
19

Note

In the first test case, we can move the integer 33 from the second array to the first array. Then the beauty is min(1,2,3)+min(4)=5min(1,2,3)+min(4)=5. It can be shown that this is the maximum possible beauty.

In the second test case, there is only one array, so regardless of the movements, the beauty will be min(100,1,6)=1min(100,1,6)=1.

解题说明:此题是一道模拟题,采用贪心算法,移动每行最小的到一起去,然后每行都保留倒数第二小的,最小的那个倒数第二小元素所在的行用来存放所有行中的最小数。

#include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		vector<int> b, c;
		for (int i = 0; i < n; i++)
		{
			int m;
			cin >> m;
			vector<int> a(m);
			for (int j = 0; j < m; j++)
			{
				cin >> a[j];
			}
			sort(a.begin(), a.end());
			b.push_back(a[0]);
			c.push_back(a[1]);
		}
		sort(c.begin(), c.end());
		long long sum = 0, cnt = 0;
		for (int i = 0; i < n; i++)
		{
			sum += b[i];
			if (i != 0)
			{
				cnt += c[i];
			}
		}
		sort(b.begin(), b.end());
		cnt += b[0];

		if (n == 1)
		{
			cout << b[0] << endl;
		}
		else
		{
			cout << cnt<<endl;
		}
	}
	return 0;
}

相关文章:

  • LDR6500 PD芯片:智能充电与数据传输
  • CAS单点登录(第7版)3.安装
  • 家里WiFi信号穿墙后信号太差怎么处理?
  • 【愚公系列】《Python网络爬虫从入门到精通》012-字符串处理
  • 「软件设计模式」装饰者模式(Decorator)
  • SpringBoot 与 SpringCloud的版本对应详细版
  • 3-初始化项目
  • GMSL 实例1:当 MAX96717 遇上 MAX96724,打通 Camera 视频数据传输
  • 【设计模式】【行为型模式】解释器模式(Interpreter)
  • Golang 的字符编码与 regexp
  • 论文设置页码
  • 【PCIe XDMA开发】主机相关设置
  • 【JavaEE进阶】验证码案例
  • 滚动弹幕JS
  • 阿里云视频点播,基于thinkphp8上传视频
  • 性能测试流程、主流性能工具
  • Golang internals
  • Gateway中的Filter机制
  • 友好的教育
  • Golang并发编程最佳实践:协程与通道
  • 广西壮族自治区党委常委会:坚决拥护党中央对蓝天立进行审查调查的决定
  • 媒体评欧阳娜娜遭民进党当局威胁:艺人表达国家认同是民族大义
  • 再现五千多年前“古国时代”:凌家滩遗址博物馆今开馆
  • “免签圈”扩容,旅游平台:今年以来巴西等国入境游订单显著增加
  • 当番茄霸总遇上晋江古言,短剧IP小变局
  • 商务部新闻发言人就出口管制管控名单答记者问