#P15981. [PA 2026] 堆煎饼 / Stosy naleśników
[PA 2026] 堆煎饼 / Stosy naleśników
Problem Description
Bajtek’s dad made many pancakes. He stacked them into stacks, with pancakes in each stack. In every stack, the pancakes are arranged from largest to smallest (that is, the largest pancake is at the bottom of the stack). He allows Bajtek to eat pancakes.
To avoid making a mess in the kitchen, Bajtek can only eat pancakes from the top of a stack (he cannot take the largest pancake from the bottom, because his dad worries that this would cause the pancakes to scatter all over the kitchen).
Bajtek quickly realized that these rules are not good for him—after all, the largest pancakes are at the bottom—so he immediately flipped some of the stacks over. He wanted to flip all of them, but he did not have enough time, and now his dad is watching his every move. Therefore, Bajtek must plan how to eat pancakes so that the total size is as large as possible.
Input Format
The first line contains three integers , , and (; ; ), representing the number of stacks, the number of pancakes in each stack, and the number of pancakes Bajtek is allowed to eat.
The next lines describe the stacks. The -th line contains integers (). The number is the size of the -th pancake from the top in the -th stack. For each , either for all , or for all .
Output Format
Output one integer: the maximum possible total size of the pancakes that Bajtek can eat.
3 3 5
1 2 3
1 2 3
3 2 1
11
2 3 5
999999999999 1000000000000 1000000000000
1000000000000 1000000000000 999999999999
4999999999999
Hint
Explanation of the examples: In the first example, to get a total size of , Bajtek can eat all three pancakes from the first stack (with sizes , , and in this order), and the top two pancakes from the last stack (with sizes and in this order). It can be proven that Bajtek cannot eat pancakes with a total size greater than .
In the second example, Bajtek can eat all pancakes except one. He should leave the bottom pancake of the second stack uneaten.
Translated by ChatGPT 5