Mouse Meets Cheese

All submissions for this problem are available.
###Read problems statements [Mandarin](http://www.codechef.com/download/translated/LTIME68/mandarin/MMC.pdf) , [Bengali](http://www.codechef.com/download/translated/LTIME68/bengali/MMC.pdf) , [Hindi](http://www.codechef.com/download/translated/LTIME68/hindi/MMC.pdf) , [Russian](http://www.codechef.com/download/translated/LTIME68/russian/MMC.pdf) and [Vietnamese](http://www.codechef.com/download/translated/LTIME68/vietnamese/MMC.pdf) as well. Arya has a pet mouse named Jack. She put Jack in a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$). There are $K$ cheese wedges scattered over the grid; for each wedge, we know how long it takes Jack to eat it. Let's denote the cell in row $r$ and column $c$ by $(r, c)$. Initially, Jack is in the cell $(1, 1)$. When Jack is in a cell $(r, c)$, he can move to either of the cells $(r, c1)$ or $(r, c+1)$ in $1$ second. If he is in the first or last column of the grid ($c=1$ or $c=M$), he can also move to the cell $(r+1, c)$ in $1$ second. Jack can only eat a cheese wedge if he is in the same cell as the wedge. Your task is to help Jack find out, for each possible $i$, the minimum number of seconds Jack needs to eat $i$ cheese wedges. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains three spaceseparated integers $N$, $M$ and $K$.  $K$ lines follow. Each of these lines contains three spaceseparated integers $r$, $c$ and $t$, denoting that there is a cheese wedge in cell $(r, c)$ and it takes Jack $t$ seconds to eat it. ### Output For each test case, print a single line containing $K$ spaceseparated integers. For each $i$ ($1 \le i \le K$), the $i$th of them should denote the minimum number of seconds Jack needs to eat $i$ cheese wedges. ### Constraints  $1 \le T \le 1,000$  $3 \le N, M \le 10^9$  $1 \le K \le 10^4$  $1 \le r \le N$  $2 \le c \le M1$  $1 \le t \le 10^9$  no two cheese wedges are in the same cell  the sum of $K$ over all test cases does not exceed $10^4$ ### Subtasks **Subtask #1 (25 points):**  $1 \le T \le 10$  $ N, M, K \le 300$ **Subtask #2 (75 points):** original constraints ### Example Input ``` 1 3 10 5 1 2 10 2 2 1 1 3 1 1 9 1 2 9 1 ``` ### Example Output ``` 3 8 14 22 32 ```Author:  watcher 
Editorial  https://discuss.codechef.com/problems/MMC 
Tags  dynamicprogramming, ltime68, watcher 
Date Added:  22012019 
Time Limit:  3 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 