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
#include <cstdio>
#include <vector>
using namespace std;

int main() {
    int n, m, k;
    scanf("%d%d%d", &n,&m,&k);
    long long int a_i;
    vector<vector<long long int> > r;
    vector<vector<long long int> > a;
    a.push_back(vector<long long int>(k+1, 0));
    for (int i=0; i < n; i++) {
        vector<long long int> a_stack;
        a_stack.push_back(0);
        for (int j=0; j < m; j++) {
            scanf("%lld", &a_i);
            a_stack.push_back(a_i);
        }
        a.push_back(a_stack);
    }

    for (int i=0; i <= n; i++) {
        vector<long long int> v(k+1, 0);
        r.push_back(v);
    }

    for (int i=1; i <= n; i++) {
        for (int j=1; j <= k; j++) {
            long long best = 0;
            long long sum_a = 0;
            for (int t=j; t >= 0 && t >= j - m; t--) {
                sum_a += a[i][j-t];
                long long act_r = r[i-1][t] + sum_a;
                if (act_r > best)
                    best = act_r;
            }
            r[i][j] = best;
        }
    }

    printf("%lld\n", r[n][k]);
    return 0;
}