1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Author: Bartek Knapik

#include <cstdio>
#include <vector>

using namespace std;

const int MAXN = 300000+9;

vector<unsigned long long> pancakes[MAXN], pre[MAXN], ans[2];

inline int _m(int a, int b) { return a < b ? a : b; }
inline unsigned long long _M(unsigned long long a, unsigned long long b) { return a < b ? b : a; }

int main()
{
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);
    
    for (int i = 0; i < n; ++i)
    {
        pancakes[i].clear();
        pre[i].clear();
        pre[i].push_back(0ULL);
        for (int j = 0; j < m; ++j)
        {
            unsigned long long tmp;
            scanf("%llu", &tmp);
            pancakes[i].push_back(tmp);
            pre[i].push_back(pre[i][j] + tmp);
            ans[0].push_back(0ULL);
            ans[1].push_back(0ULL);
        }
    }
    
    for (int i = 0; i < n; ++i)
        for (int j = 0; j <= m; ++j)
            for (int l = _m(k, m * i + j); l >=j; --l)
                ans[i&1][l] = _M(ans[i&1][l], ans[(i+1)&1][l - j] + pre[i][j]);
    
    printf("%llu\n", ans[(n-1)&1][k]);
    return 0;
}