E. El Juego del Calamar
time limit per test
5 s
memory limit per test
256 megabytes
input
standard input
output
standard output

It's time for the first game of the global edition of the Squid Games.

The game is played in a tower with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$, each floor containing $$$k$$$ rooms numbered from $$$1$$$ to $$$k$$$. There are $$$m$$$ stairs; the $$$i$$$-th stair connects room $$$u_i$$$ on floor $$$f_i$$$ with room $$$v_i$$$ on floor $$$f_i + 1$$$.

There are $$$2\cdot10^9$$$ players invited to play the game. At the beginning, each of the $$$2 \cdot 10^9$$$ players must choose any room on the first floor. The game lasts for $$$n$$$ turns. In turn $$$t$$$ ($$$1 \le t \le n$$$), the following actions occur in order:

  1. For every room $$$j$$$ on floor $$$t$$$, if there are more than $$$C_{t,j}$$$ players, random players are eliminated until exactly $$$C_{t,j}$$$ remain in that room.
  2. If $$$t \lt n$$$, each surviving player in a room on floor $$$t$$$ must move to a room on floor $$$t+1$$$ using one of the available stairs connected to their current room. Players unable to move are eliminated. Moving down is not allowed.
  3. If $$$t = n$$$, all surviving players are declared winners of the first game, and the game ends.

Player 456 wants to maximize the number of players who survive. For each turn $$$t$$$, compute the maximum number of players that can be alive at the end of that turn, assuming optimal choices for the initial distribution and movements. Note that for each $$$t$$$, these values are calculated independently.

Input

The first line contains three integers $$$n$$$, $$$k$$$, and $$$m$$$ ($$$1 \le n \le 1000$$$, $$$1 \le k \le 15$$$, $$$0 \le m \le (n-1) \cdot k^2$$$).

The next $$$n$$$ lines each contain $$$k$$$ integers $$$C_{i,1}, C_{i,2}, \dots, C_{i,k}$$$ ($$$0 \le C_{i,j} \le 10^8$$$), representing the capacity of each room.

Then $$$m$$$ lines follow. Each contains three integers $$$f_i$$$, $$$u_i$$$, and $$$v_i$$$ — describing a stair from room $$$u_i$$$ on floor $$$f_i$$$ to room $$$v_i$$$ on floor $$$f_i+1$$$.

Output

Print a single line with $$$n$$$ integers — the answer for each turn.

Examples
Input
2 2 1
3 7
4 2
1 1 2
Output
10 2 
Input
3 3 5
8 2 4
3 4 8
4 5 0
1 1 1
1 2 2
1 3 3
2 1 1
2 2 2
Output
14 9 5