|
<p> |
|
Some pies are sweet, full of fruit and jam and sugar. |
|
</p> |
|
|
|
<p> |
|
Some pies are savory, full of meat and potatoes and spices. |
|
</p> |
|
|
|
<p> |
|
Some pies are in fact not pies at all but tarts or galettes. This probably won't stop you from eating them. |
|
</p> |
|
|
|
<p> |
|
Every single day for <strong>N</strong> days, you're determined to eat a pie for dinner. |
|
Every morning, you'll take a trip to your local pie shop, and buy 0 or more of their pies. |
|
Every night, you'll eat one pie that you've bought. |
|
Pies never go bad, so you don't need to eat a pie on the same day that you bought it. You may instead eat one that you purchased on an earlier day. |
|
</p> |
|
|
|
<p> |
|
On the <em>i</em>th day, the shop has <strong>M</strong> pies for sale, with the <em>j</em>th of these pies costing <strong>C<sub>i,j</sub></strong> dollars. |
|
You can choose to buy any (possibly empty) subset of them. |
|
However, this shop has measures in place to protect itself against crazy pie fanatics buying out its products too quickly. |
|
In particular, if you buy <strong>p</strong> pies on a single day, you must pay an additional tax of <strong>p</strong><sup>2</sup> dollars. |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of times you go on a pie-eating spree. |
|
For each case, there is first a line containing two space-separated integers, <strong>N</strong> and <strong>M</strong>. |
|
Then, <strong>N</strong> lines follow, each containing <strong>M</strong> space-separated integers. |
|
The <em>j</em>th integer on the <em>i</em>th line is <strong>C<sub>i,j</sub></strong>. |
|
</p> |
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th case, print a line containing "Case #<strong>i</strong>: " followed by the minimum you need to pay |
|
to eat a pie every day. |
|
</p> |
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 100 <br /> |
|
1 ≤ <strong>N</strong>, <strong>M</strong> ≤ 300 <br /> |
|
1 ≤ <strong>C<sub>i,j</sub></strong> ≤ 1,000,000 <br /> |
|
</p> |
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, you should buy both pies on the first day, for a total cost of 1 + 1 + 2<sup>2</sup> = 6. On the second day you should buy one pie for 100 + 1<sup>2</sup> = 101. On the third day you can eat one of the spare pies you bought on the first day. |
|
</p> |
|
|
|
<p> |
|
In the third case, you should buy and eat the cheapest pie every day, for a daily cost of 1 + 1<sup>2</sup> = 2, and a total cost of 10. |
|
</p> |
|
|
|
<p> |
|
In the fourth case, one possible solution is to buy two pies on the first day (1 + 1 + 2<sup>2</sup> = 6), two pies on the second day (2 + 2 + 2<sup>2</sup> = 8), and one pie on the third day (3 + 1<sup>2</sup> = 4) for a total cost of 18. On the fourth and fifth days you can eat your two spare pies from the first and second days. |
|
</p> |
|
|