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
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(__typeof(b) i = a; i < b; ++i)
#define FORev(i,a,b) for(__typeof(a) i = a; i >= b; --i)
#define pb push_back
#define c(x) cin >> x
#define all(x) x.begin(),x.end()
const ll inf = 1e18;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll n,m,k; c(n); c(m); c(k);
    vector<ll> opt;
    vector<vector<ll>> pref1;
    FOR(i,0,n){
        vector<ll> tab(m);
        for(auto& x : tab) c(x);
        if(tab[0] >= tab[m-1])
            opt.insert(opt.end(), all(tab));
        else{
            vector<ll> cur_p(m+1);
            cur_p[0] = 0;
            FOR(j,1,m+1) cur_p[j] = cur_p[j-1] + tab[j-1];
            pref1.pb(cur_p);
        }
    }

    sort(all(opt), greater<ll>());
    vector<ll> pref2(opt.size()+1,0);
    FOR(i,1,(ll)pref2.size()) 
        pref2[i] = pref2[i-1]+opt[i-1];
    vector<ll> dp(k+1, -inf);
    dp[0] = 0;
    /*
    for (int i = 1; i <= n; i++)
        for (int j = W; j >= w[i]; j--)
            f[j] = max(f[j], f[j - w[i]] + v[i]);
    */
    for(auto& pref : pref1){
        FORev(i,k,0){
            FOR(j,1,min(m,i)+1){
                dp[i] = max(dp[i],dp[i-j]+pref[j]);
            }
        }
    }

    ll res = 0;
    FOR(i,0,min((ll)opt.size(),k)+1)
        res = max(res,pref2[i]+dp[k-i]);

    cout << res << "\n";
}