QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 10

#17387. Pancake Stacks [B]

Statistics

Bajt's father has fried a lot of pancakes. He arranged them into $n$ stacks of $m$ pancakes each. The pancakes in each stack are arranged from largest to smallest (i.e., the largest pancake is at the bottom of the stack). He allowed Bajt to eat $k$ of these pancakes. To avoid a mess in the kitchen, Bajt can only eat pancakes from the top of a stack (he cannot take the largest pancakes from the bottom, as his father is afraid this would result in pancakes scattered all over the kitchen).

Bajt quickly realized that these rules are not in his favor—after all, the largest pancakes are at the bottoms of the stacks—and he quickly turned some of the stacks upside down. He wanted to turn them all over, but he didn't have time, and now his father is vigilantly watching his every move. Bajt must therefore plan how to eat as many pancakes as possible.

Input

The first line of input contains three integers $n$, $m$, and $k$ ($n, m \ge 1$; $n \cdot m \le 300\,000$; $1 \le k \le n \cdot m$), representing the number of pancake stacks, the number of pancakes in each stack, and the number of pancakes Bajt is allowed to eat, respectively.

The next $n$ lines contain descriptions of the stacks; the $i$-th of these lines contains a sequence of $m$ integers $a_{i,1}, \dots, a_{i,m}$ ($1 \le a_{i,j} \le 10^{12}$). The number $a_{i,j}$ represents the size of the $j$-th pancake from the top in the $i$-th stack. For each $i$, either $a_{i,j} \ge a_{i,j+1}$ for all $j$, or $a_{i,j} \le a_{i,j+1}$ for all $j$.

Output

Output a single integer – the maximum total size of $k$ pancakes that Bajt can eat.

Examples

Input 1

3 3 5
1 2 3
1 2 3
3 2 1

Output 1

11

Input 2

2 3 5
999999999999 1000000000000 1000000000000
1000000000000 1000000000000 999999999999

Output 2

4999999999999

Note

In the first example, to eat pancakes with a total size of 11, Bajt can, for example, eat all three pancakes from the first stack (with sizes 1, 2, and 3, respectively) and the two top pancakes from the last stack (with sizes 3 and 2, respectively). It can be proven that Bajt cannot eat pancakes with a total size greater than 11.

In the second example, Bajt can eat all pancakes except one. It is optimal for him to leave the bottom pancake of the second stack uneaten.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#1353EditorialOpen题解Milmon2026-03-31 16:24:40View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.