Codeforces Round 968 (Div. 2) D1. Turtle and a MEX Problem (Easy Version)

D1. Turtle and a MEX Problem (Easy Version)

time limit per test: 2 seconds

memory limit per test: 256 megabytes

The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved.

One day, Turtle was playing with n sequences. Let the length of the i-th sequence be li. Then the i-th sequence was a_{i,1},a_{i,2},...,a_{i,l_{i}}.

Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was:

There was a non-negative integer x at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer.In each operation, Turtle could choose an integer i such that 1≤i≤n, and set x to mex^{\dagger }(x,a_{i,1},a_{i,2},...,a_{i,l_{i}}).

Turtle was asked to find the answer, which was the maximum value of x after performing an arbitrary number of operations.

Turtle solved the above problem without difficulty. He defined f(k) as the answer to the above problem when the initial value of x was k.

Then Piggy gave Turtle a non-negative integer m and asked Turtle to find the value of \sum_{i=0}^{m}f(i) (i.e., the value of f(0)+f(1)+…+f(m)). Unfortunately, he couldn't solve this problem. Please help him!

†mex(c1,c2,…,ck) is defined as the smallest non-negative integer x which does not occur in the sequence c. For example, mex(2,2,0,3) is 1, mex(1,2) is 0.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10^4). The description of the test cases follows.

The first line of each test case contains two integers n,m (1≤n≤2⋅10^5,0≤m≤10^9).

Each of the following n lines contains several integers. The first integer li (1≤li≤2⋅10^5) represents the length of the i-th sequence, and the following li integers ai,1,ai,2,…,ai,li (0≤ai,j≤10^9) represent the elements of the i-th sequence.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5, and the sum of ∑li over all test cases does not exceed 2⋅10^5.

Output

For each test case, output a single integer — the value of \sum_{i=0}^{m}f(i).

Example

Input

6

3 4

2 0 2

3 2 3 3

4 7 0 1 5

3 4

5 0 2 0 4 11

1 1

5 1 3 0 3 3

2 50

2 1 2

2 1 2

1 1

7 1 2 4 1 4 9 5

4 114514

2 2 2

5 7 3 6 0 3

3 0 1 1

5 0 9 2 1 5

5 1919810

1 2

2 324003 0

3 1416324 2 1460728

4 1312631 2 0 1415195

5 1223554 192248 2 1492515 725556

Output

16

20

1281

6

6556785365

1842836177961

Note

In the first test case, when x is initially 2, Turtle can choose i=3 and set x to mex(x,a3,1,a3,2,a3,3,a3,4)=mex(2,7,0,1,5)=3. It can be proved that Turtle can't make the value of x greater than 3, so f(2)=3.

It can be seen that f(0)=3, f(1)=3, f(2)=3, f(3)=3, and f(4)=4. So f(0)+f(1)+f(2)+f(3)+f(4)=3+3+3+3+4=16.

In the second test case, when x is initially 1, Turtle can choose i=3 and set x to mex(x,a3,1,a3,2,a3,3,a3,4,a3,5)=mex(1,1,3,0,3,3)=2, and choose i=3 and set x to mex(x,a3,1,a3,2,a3,3,a3,4,a3,5)=mex(2,1,3,0,3,3)=4. It can be proved that Turtle can't make the value of x greater than 4, so f(1)=4.

It can be seen that f(0)=4, f(1)=4, f(2)=4, f(3)=4, and f(4)=4. So f(0)+f(1)+f(2)+f(3)+f(4)=4+4+4+4+4=20.

In the fourth test case, it can be seen that f(0)=3 and f(1)=3. So f(0)+f(1)=3+3=6.

【思路分析】

贪心。由于可以操作任意次,只需要找所有序列中第一个最小的非负整数即可,将该数贪心地取最大,可以保证任意一个初始值进行操作后都能取到该值。对于大于该数的初始值,不需要进行操作。

#include<bits/stdc++.h>

#define i64 long long

using namespace std;

void solve() {
    i64 n, m;
    cin >> n >> m;
    i64 minn = 0, maxn = 0;
    for (int i = 0; i < n; ++i) {
        i64 l;
        cin >> l;
        i64 a[l];
        for (int j = 0; j < l; ++j) {
            cin >> a[j];
        }
        sort(a, a + l);
        bool ok = false, flag = false, minnf = false;
        if (a[0] == 1) {
            ok = true;
            minnf = true;
        }

        i64 res;
        if (a[0] >= 2) {
            res = 1;
            maxn = max(maxn, res);
            continue;
        }
        for (int j = 1; j < l; ++j) {
            if (a[j] >= a[j - 1] + 3) {
                if (!minnf) {
                    minn = max(minn, a[j - 1] + 1);
                    minnf = true;
                }
                if (!ok) res = a[j - 1] + 2;
                else res = a[j - 1] + 1;
                flag = true;
                break;
            } else if (a[j] == a[j - 1] + 2) {
                if (!minnf) {
                    minn = max(minn, a[j - 1] + 1);
                    minnf = true;
                }
                if (!ok) ok = true;
                else {
                    res = a[j] - 1;
                    flag = true;
                    break;
                }
            }
        }
        if (!minnf) minn = max(minn, a[l - 1] + 1);
        if (!flag) {
            if (ok) res = a[l - 1] + 1;
            else res = a[l - 1] + 2;
        }
        maxn = max(maxn, res);
    }
    if (m<=maxn) cout<<(m+1)*maxn<<endl;
    else cout<<(maxn+1)*maxn+(maxn+1+m) * (m-maxn)/2<<endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值