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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m, k;
    cin>>n>>m>>k;

    vector<ll> dp(k+1, -1);
    dp[0] = 0;
    vector<ll> curr(m+1, 0), pref(m+1, 0);

    for(int i=1; i<=n; i++){
        pref[0] = 0;
        for(int j=1; j<=m; j++){
            cin>>curr[j];
            pref[j] = pref[j-1] + curr[j];
        }
        vector<ll> nowe_dp(k+1, -1);
        for(int already = 0; already <= k; already++){
            if(dp[already] == -1) continue;

            for(int take = 0; take <= m && already + take <= k; take++){
                ll total = already + take;
                nowe_dp[total] = max(nowe_dp[total], dp[already] + pref[take]);
            }
        }
        dp = nowe_dp;
    }
    cout<<dp[k];

    return 0;
}