codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1561C.md (2822B)


      1 Source: https://codeforces.com/problemset/problem/1561/C
      2 
      3 C. Deep Down Below
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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}$.
     24 
     25 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.
     26 
     27 Each time the hero beats a monster, the hero's power increases by $1$.
     28 
     29 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.
     30 
     31 Input
     32 
     33 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.
     34 
     35 The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of caves.
     36 
     37 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.
     38 
     39 It is guaranteed that the sum of $k_i$ over all test cases does not exceed $10^5$.
     40 
     41 Output
     42 
     43 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.
     44 
     45 Example
     46 
     47 Input
     48 
     49 ```
     50 2
     51 1
     52 1 42
     53 2
     54 3 10 15 8
     55 2 12 11
     56 ```
     57 
     58 Output
     59 
     60 ```
     61 43
     62 13
     63 ```
     64 
     65 Note
     66 
     67 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.
     68 
     69 In the second test case, the hero can pass the level with initial power $13$ as follows:
     70 
     71 * enter cave $2$:
     72   + beat a monster with armor $12$, power increases to $14$;
     73   + beat a monster with armor $11$, power increases to $15$;
     74 * enter cave $1$:
     75   + beat a monster with armor $10$, power increases to $16$;
     76   + beat a monster with armor $15$, power increases to $17$;
     77   + beat a monster with armor $8$, power increases to $18$.