CF BUDDY
← Problems·

1893C · Freedom of Choice

2000 · brute force, greedy, implementation

Problem: Let's define the anti-beauty of a multiset {b1,b2,,blen}\{b_1, b_2, \ldots, b_{len}\} as the number of occurrences of the number lenlen in the multiset.

You are given mm multisets, where the ii-th multiset contains nin_i distinct elements, specifically: ci,1c_{i, 1} copies of the number ai,1a_{i,1}, ci,2c_{i, 2} copies of the number ai,2,,ci,nia_{i,2}, \ldots, c_{i, n_i} copies of the number ai,nia_{i, n_i}. It is guaranteed that ai,1<ai,2<<ai,nia_{i, 1} < a_{i, 2} < \ldots < a_{i, n_i}. You are also given numbers l1,l2,,lml_1, l_2, \ldots, l_m and r1,r2,,rmr_1, r_2, \ldots, r_m such that 1lirici,1++ci,ni1 \le l_i \le r_i \le c_{i, 1} + \ldots + c_{i, n_i}.

Let's create a multiset XX, initially empty. Then, for each ii from 11 to mm, you must perform the following action exactly once:

  1. Choose some viv_i such that liviril_i \le v_i \le r_i
  2. Choose any viv_i numbers from the ii-th multiset and add them to the multiset XX.

You need to choose v1,,vmv_1, \ldots, v_m and the added numbers in such a way that the resulting multiset XX has the minimum possible anti-beauty.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer mm (1m1051 \le m \le 10^5) — the number of given multisets.

Then, for each ii from 11 to mm, a data block consisting of three lines is entered.

The first line of each block contains three integers ni,li,rin_i, l_i, r_i (1ni105,1lirici,1++ci,ni10171 \le n_i \le 10^5, 1 \le l_i \le r_i \le c_{i, 1} + \ldots + c_{i, n_i} \le 10^{17}) — the number of distinct numbers in the ii-th multiset and the limits on the number of elements to be added to XX from the ii-th multiset.

The second line of the block contains nin_i integers ai,1,,ai,nia_{i, 1}, \ldots, a_{i, n_i} (1ai,1<<ai,ni10171 \le a_{i, 1} < \ldots < a_{i, n_i} \le 10^{17}) — the distinct elements of the ii-th multiset.

The third line of the block contains nin_i integers ci,1,,ci,nic_{i, 1}, \ldots, c_{i, n_i} (1ci,j10121 \le c_{i, j} \le 10^{12}) — the number of copies of the elements in the ii-th multiset.

It is guaranteed that the sum of the values of mm for all test cases does not exceed 10510^5, and also the sum of nin_i for all blocks of all test cases does not exceed 10510^5.

Output Format: For each test case, output the minimum possible anti-beauty of the multiset XX that you can achieve.

Note: In the first test case, the multisets have the following form:

  1. {10,10,10,11,11,11,12}\{10, 10, 10, 11, 11, 11, 12\}. From this multiset, you need to select between 55 and 66 numbers.
  2. {12,12,12,12}\{12, 12, 12, 12\}. From this multiset, you need to select between 11 and 33 numbers.
  3. {12,13,13,13,13,13}\{12, 13, 13, 13, 13, 13\}. From this multiset, you need to select 44 numbers.

You can select the elements {10,11,11,11,12}\{10, 11, 11, 11, 12\} from the first multiset, {12}\{12\} from the second multiset, and {13,13,13,13}\{13, 13, 13, 13\} from the third multiset. Thus, X={10,11,11,11,12,12,13,13,13,13}X = \{10, 11, 11, 11, 12, 12, 13, 13, 13, 13\}. The size of XX is 1010, the number 1010 appears exactly 11 time in XX, so the anti-beauty of XX is 11. It can be shown that it is not possible to achieve an anti-beauty less than 11.

Sample Cases

Case 1

Input

7
3
3 5 6
10 11 12
3 3 1
1 1 3
12
4
2 4 4
12 13
1 5
1
7 1000 1006
1000 1001 1002 1003 1004 1005 1006
147 145 143 143 143 143 142
1
2 48 50
48 50
25 25
2
1 1 1
1
1
1 1 1
2
1
1
1 1 1
1
2
2
1 1 1
1
1
2 1 1
1 2
1 1
2
4 8 10
11 12 13 14
3 3 3 3
2 3 4
11 12
2 2

Output

1
139
0
1
1
0
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback