CF BUDDY
← Problems·

1936C · Pokémon Arena

2400 · data structures, graphs, greedy

Problem: You are at a dueling arena. You also possess nn Pokémons. Initially, only the 11-st Pokémon is standing in the arena.

Each Pokémon has mm attributes. The jj-th attribute of the ii-th Pokémon is ai,ja_{i,j}. Each Pokémon also has a cost to be hired: the ii-th Pokémon's cost is cic_i.

You want to have the nn-th Pokémon stand in the arena. To do that, you can perform the following two types of operations any number of times in any order:

  • Choose three integers ii, jj, kk (1in1 \le i \le n, 1jm1 \le j \le m, k>0k > 0), increase ai,ja_{i,j} by kk permanently. The cost of this operation is kk.
  • Choose two integers ii, jj (1in1 \le i \le n, 1jm1 \le j \le m) and hire the ii-th Pokémon to duel with the current Pokémon in the arena based on the jj-th attribute. The ii-th Pokémon will win if ai,ja_{i,j} is greater than or equal to the jj-th attribute of the current Pokémon in the arena (otherwise, it will lose). After the duel, only the winner will stand in the arena. The cost of this operation is cic_i.

Find the minimum cost you need to pay to have the nn-th Pokémon stand in the arena.

Input Format: Each test contains multiple test cases. The first line contains the number of test cases tt (1t1051 \le t \le 10^5). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2n41052 \le n \le 4 \cdot 10^5, 1m21051 \le m \le 2 \cdot 10^5, 2nm41052 \leq n \cdot m \leq 4 \cdot 10^5).

The second line of each test case contains nn integers c1,c2,,cnc_1, c_2, \ldots, c_n (1ci1091 \le c_i \le 10^9).

The ii-th of the following nn lines contains mm integers ai,1,ai,2,,ai,ma_{i,1}, a_{i,2}, \ldots, a_{i,m} (1ai,j1091 \le a_{i,j} \le 10^9).

It is guaranteed that the sum of nmn \cdot m over all test cases does not exceed 41054 \cdot 10^5.

Output Format: For each test case, output the minimum cost to make the nn-th Pokémon stand in the arena.

Note: In the first test case, the attribute array of the 11-st Pokémon (which is standing in the arena initially) is [2,9,9][2,9,9].

In the first operation, you can choose i=3i=3, j=1j=1, k=1k=1, and increase a3,1a_{3,1} by 11 permanently. Now the attribute array of the 33-rd Pokémon is [2,2,1][2,2,1]. The cost of this operation is k=1k = 1.

In the second operation, you can choose i=3i=3, j=1j=1, and hire the 33-rd Pokémon to duel with the current Pokémon in the arena based on the 11-st attribute. Since ai,j=a3,1=22=a1,1a_{i,j}=a_{3,1}=2 \ge 2=a_{1,1}, the 33-rd Pokémon will win. The cost of this operation is c3=1c_3 = 1.

Thus, we have made the 33-rd Pokémon stand in the arena within the cost of 22. It can be proven that 22 is minimum possible.

In the second test case, the attribute array of the 11-st Pokémon in the arena is [9,9,9][9,9,9].

In the first operation, you can choose i=2i=2, j=3j=3, k=2k=2, and increase a2,3a_{2,3} by 22 permanently. Now the attribute array of the 22-nd Pokémon is [6,1,9][6,1,9]. The cost of this operation is k=2k = 2.

In the second operation, you can choose i=2i=2, j=3j=3, and hire the 22-nd Pokémon to duel with the current Pokémon in the arena based on the 33-rd attribute. Since ai,j=a2,3=99=a1,3a_{i,j}=a_{2,3}=9 \ge 9=a_{1,3}, the 22-nd Pokémon will win. The cost of this operation is c2=3c_2 = 3.

In the third operation, you can choose i=3i=3, j=2j=2, and hire the 33-rd Pokémon to duel with the current Pokémon in the arena based on the 22-nd attribute. Since ai,j=a1,2=21=a2,2a_{i,j}=a_{1,2}=2 \ge 1=a_{2,2}, the 33-rd Pokémon can win. The cost of this operation is c3=1c_3 = 1.

Thus, we have made the 33-rd Pokémon stand in the arena within the cost of 66. It can be proven that 66 is minimum possible.

Sample Cases

Case 1

Input

4
3 3
2 3 1
2 9 9
6 1 7
1 2 1
3 3
2 3 1
9 9 9
6 1 7
1 2 1
4 2
2 8 3 5
18 24
17 10
1 10
1 1
6 3
21412674 3212925 172015806 250849370 306960171 333018900
950000001 950000001 950000001
821757276 783362401 760000001
570000001 700246226 600757652
380000001 423513575 474035234
315201473 300580025 287023445
1 1 1

Output

2
6
17
1224474550

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