B. Olya and Game with Arrays | CodeForces Solution | Codeforces Round 892 (Div. 2)


Artem suggested a game to the girl Olya. There is a list of š‘› arrays, where the š‘– -th array contains š‘šš‘–ā‰„2 positive integers š‘Žš‘–,1,š‘Žš‘–,2,ā€¦,š‘Žš‘–,š‘šš‘– . Olya can move at most one (possibly 0 ) 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.

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 n arrays, where the i-th array contains mi ā‰„ 2 positive integers ai,1, ai,2, ā€¦, ai,mi.

Olya can move at most one (possibly 0) 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=1 minmij=1 ai,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 t (1 ā‰¤ t ā‰¤ 25000) ā€” the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (1 ā‰¤ 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 mi (2 ā‰¤ mi ā‰¤ 50000) ā€” the number of elements in the i-th array.

The next line contains mi integers ai,1, ai,2, ā€¦, ai,mi (1 ā‰¤ ai,j ā‰¤ 109) ā€” the elements of the i-th array.

It is guaranteed that the sum of mi over all test cases does not exceed 50000.

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

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

5
1
19

Note

In the first test case, we can move the integer 3 from the second array to the first array. Then the beauty is min(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) = 1.

void solve()
{

  int n;
  cin >> n;
  vector<vi> v;
  int mn = 1e10;
  int idx = 0;
  for (int i = 0; i < n; i++)
  {
    int k;
    cin >> k;
    vi temp(k);
    for (int j = 0; j < k; j++)
    {
      cin >> temp[j];
      if (mn > temp[j])
      {
        mn = temp[j];
        idx = i;
      }
    }
    v.pb(temp);
  }

  vpi ans;
  for (int i = 0; i < n; i++)
  {
    sort(v[i].begin(), v[i].end());
    ans.pb({v[i][1], i});
  }

  sort(ans.begin(), ans.end());
  int res = 0;
  for (int i = 1; i < ans.size(); i++)
    res += ans[i].first;

  for (int i = 0; i < n; i++)
  {
    mn = min(mn, v[i][0]);
  }

  cout << res + mn << endl;

  // 5 7
  //  6 8
  //  2 9
}
TagsB. Olya and Game with ArraysCodeforces Round 892 (Div. 2)Codeforces
Raunit Verma-picture
Raunit Verma

Technical Writer at CodingKaro

Share this on
CodingKaro Poster
CodingKaro
4.7

3K+ Downloads

One of its unique features is an automated coding contest reminder, which sets alarms for upcoming coding contests on popular platforms like CodeChef, CodeForces, and LeetCode, without the need for user input. This feature ensures that users never miss a coding contest and can stay updated with the latest coding challenges.

Download CodingKaro
Other Blogs in Codeforces