Deep Down Below
Contest: Codeforces Round 740 (Div. 2, based on VK Cup 2021 - Final (Engine))
Difficulty rating: 1300
Tags: binary search, greedy, sortings

In a certain video game, the player controls a hero characterized by a single integer value: power. The hero will have to beat monsters that are also characterized by a single integer value: armor.

On the current level, the hero is facing $$$n$$$ caves. To pass the level, the hero must enter all the caves in some order, each cave exactly once, and exit every cave safe and sound. When the hero enters cave $$$i$$$, he will have to fight $$$k_i$$$ monsters in a row: first a monster with armor $$$a_{i, 1}$$$, then a monster with armor $$$a_{i, 2}$$$ and so on, finally, a monster with armor $$$a_{i, k_i}$$$.

The hero can beat a monster if and only if the hero's power is strictly greater than the monster's armor. If the hero can't beat the monster he's fighting, the game ends and the player loses. Note that once the hero enters a cave, he can't exit it before he fights all the monsters in it, strictly in the given order.

Each time the hero beats a monster, the hero's power increases by $$$1$$$.

Find the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters.

Input format:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). Description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of caves.

The $$$i$$$-th of the next $$$n$$$ lines contains an integer $$$k_i$$$ ($$$1 \le k_i \le 10^5$$$) — the number of monsters in the $$$i$$$-th cave, followed by $$$k_i$$$ integers $$$a_{i, 1}, a_{i, 2}, \ldots, a_{i, k_i}$$$ ($$$1 \le a_{i, j} \le 10^9$$$) — armor levels of the monsters in cave $$$i$$$ in order the hero has to fight them.

It is guaranteed that the sum of $$$k_i$$$ over all test cases does not exceed $$$10^5$$$.

Output format:
For each test case print a single integer — the smallest possible power the hero must start the level with to be able to enter all the caves in some order and beat all the monsters.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first test case, the hero has to beat a single monster with armor $$$42$$$, it's enough to have power $$$43$$$ to achieve that.

In the second test case, the hero can pass the level with initial power $$$13$$$ as follows:

- enter cave $$$2$$$:   beat a monster with armor $$$12$$$, power increases to $$$14$$$;  beat a monster with armor $$$11$$$, power increases to $$$15$$$;
- enter cave $$$1$$$:   beat a monster with armor $$$10$$$, power increases to $$$16$$$;  beat a monster with armor $$$15$$$, power increases to $$$17$$$;  beat a monster with armor $$$8$$$, power increases to $$$18$$$.

Example input:
2
1
1 42
2
3 10 15 8
2 12 11

Example output:
43
13

Editorial:
Consider a single cave $$$i$$$. Suppose that the hero enters the cave with power $$$x$$$. To beat the first monster, $$$x$$$ has to be greater than $$$a_{i, 1}$$$. After that, the hero's power will increase to $$$x+1$$$, and to beat the second monster, $$$x+1$$$ has to be greater than $$$a_{i, 2}$$$. Continuing this reasoning, we can write down $$$k_i$$$ inequalities:
• $$$x > a_{i, 1}$$$;
• $$$x + 1 > a_{i, 2}$$$;
• $$$x + 2 > a_{i, 3}$$$;
• ...
• $$$x + (k_i - 1) > a_{i, k_i}$$$.
Let $$$b_i = \max(a_{i, 1}, a_{i, 2} - 1, a_{i, 3} - 2, \ldots, a_{i, k_i} - (k_i - 1))$$$. The system of inequalities above is equivalent to a single inequality: $$$x > b_i$$$.
Thus, the hero can enter cave $$$i$$$ with power $$$x$$$ if and only if $$$x > b_i$$$, and the hero's power will increase by $$$k_i$$$.
Armed with this knowledge, can we determine the best order to visit the caves for the hero?
It turns out it's always best to enter the caves in non-decreasing order of $$$b_i$$$. Indeed, if the hero can enter cave $$$i$$$, he should always do that because entering a cave never makes things worse. If the hero enters a cave with greater $$$b$$$ right before a cave with smaller $$$b$$$, he might enter these caves in reverse order as well.
Let's sort the caves accordingly and assume $$$b_1 \le b_2 \le \ldots \le b_n$$$. What is the smallest power the hero can start the level with?
We can use the same reasoning that we used for a single cave.
Suppose the hero starts the level with power $$$x$$$. To enter the first cave, $$$x$$$ has to be greater than $$$b_1$$$. After that, the hero's power will increase to $$$x+k_1$$$, and to enter the second cave, $$$x+k_1$$$ has to be greater than $$$b_2$$$. Continuing this reasoning, we can write down $$$n$$$ inequalities:
• $$$x > b_1$$$;
• $$$x + k_1 > b_2$$$;
• $$$x + k_1 + k_2 > b_3$$$;
• ...
• $$$x + \sum \limits_{i=1}^{n-1} k_i > b_n$$$.
Let $$$p = \max(b_1, b_2 - k_1, b_3 - (k_1 + k_2), \ldots, b_n - \sum \limits_{i=1}^{n-1} k_i)$$$. The system of inequalities above is equivalent to a single inequality: $$$x > p$$$. Thus, the answer to the problem is $$$p + 1$$$.
Alternatively, instead of solving the inequalities, one can use binary search on $$$x$$$.