fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=1e9+7;
  5. const ll INF = 10000000000000;
  6. const int N = 1e6+7;
  7.  
  8. void solve() {
  9. ll n,m;
  10. cin >> n >> m;
  11. ll chmax = 0 ;
  12. for(ll i=0;i<n;i++){
  13. ll k;cin >> k;
  14. set<int> st;
  15. for(ll j=0;j<k;j++) {
  16. ll b;cin >> b;
  17. st.insert(b);
  18. }
  19. ll p=0;
  20. for(p=0;st.find(p)!=st.end();p++);
  21. st.insert(p);
  22. ll q=0;
  23. for(q=0;st.find(q)!=st.end();q++);
  24. chmax=max(chmax,q);
  25. }
  26. if(chmax<m) cout << chmax*(chmax+1)+(m-chmax)*(m+chmax+1)/2 << '\n';
  27. else cout << chmax*(m+1) << '\n';
  28. }
  29. int main(){
  30. ios::sync_with_stdio(false);
  31. cin.tie(nullptr);
  32.  
  33. int t;
  34. cin >> t;
  35. while (t--) solve();
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5320KB
stdin
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
stdout
16
20
1281
6
6556785365
1842836177961