Deep Down Below (Codeforces 1561C)
Tags: Greedy, binary search

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: 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: 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.

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$$$.  

Sample Inputs:
2
1
1 42
2
3 10 15 8
2 12 11

Sample Outputs:
43
13

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    n = readInt()
    (arr, h) = ([readList() for _ in range(n)], [])
    for i in range(n):
        k = arr[i][0] + 1
        val = 0
        for j in range(1, k):
            val = max(val, arr[i][j] + 1 - (j - 1))
        h.append((val, k - 1))
    h.sort()
    (ans, curr, m) = (h[0][0], h[0][0], len(h))
    for i in range(m):
        if curr < h[i][0]:
            ans += h[i][0] - curr
        curr = max(curr, h[i][0]) + h[i][1]
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
import pprint

def main():
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    inf = 1e+17
    mod = 10 ** 9 + 7

    def factorial(n):
        f = 1
        for i in range(1, n + 1):
            f = f * i % mod
        return f

    def ncr(n, r):
        num = den = 1
        for i in range(r):
            num = num * (n - i) % mod
            den = den * (i + 1) % mod
        return num * pow(den, mod - 2, mod) % mod

    def solve(n, grid):
        maxes = []
        for i in range(n):
            Max = -1
            for j in range(len(grid[i])):
                Max = max(Max, grid[i][j] - j)
            maxes.append([Max, i])
        maxes.sort(reverse=True)
        answer = maxes[0][0] + 1
        for i in range(1, n):
            answer = max(answer - len(grid[maxes[i][1]]), maxes[i][0] + 1)
        return answer
        pass
    t = int(input())
    ans = []
    for _ in range(t):
        n = int(input())
        grid = []
        for i in range(n):
            grid.append([int(x) for x in input().split()][1:])
        ans.append(solve(n, grid))
    for answer in ans:
        print(answer)
if __name__ == '__main__':
    import sys, threading
    import bisect
    import math
    import itertools
    from sys import stdout
    import heapq
    input = sys.stdin.readline
    thread = threading.Thread(target=main)
    thread.start()
    thread.join()

Solution (Python):
import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
import heapq
I = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'
mod = 10 ** 9 + 7
''

def ii():
    return int(I().strip())

def li():
    return list(map(int, I().strip().split()))

def mi():
    return map(int, I().strip().split())

def ncr(n, r):
    r = min(r, n - r)
    numer = reduce(op.mul, range(n, n - r, -1), 1)
    denom = reduce(op.mul, range(1, r + 1), 1)
    return numer // denom

def ispali(s):
    i = 0
    j = len(s) - 1
    while i < j:
        if s[i] != s[j]:
            return False
        i += 1
        j -= 1
    return True

def isPrime(n):
    if n <= 1:
        return False
    elif n <= 2:
        return True
    else:
        for i in range(2, int(n ** 0.5) + 1):
            if n % i == 0:
                return False
        return True

def f(all_mons):
    min_power = 0
    curr_power = 0
    for i in all_mons:
        if curr_power <= i:
            min_power += i - curr_power + 1
            curr_power = i + 1
        curr_power += 1
    return min_power

def main():
    ans = ''
    for _ in range(ii()):
        n = ii()
        caves = []
        arr = []
        for i in range(n):
            mons = li()
            arr.append([f(mons[1:]), i])
            caves.append(mons[1:])
        arr.sort()
        all_mons = []
        for i in arr:
            idx = i[1]
            all_mons += caves[idx]
        min_power = f(all_mons)
        ans += str(min_power) + '\n'
    print(ans)
if __name__ == '__main__':
    main()