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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll INF = 1e18 + 10;

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<vector<ll>> increasing_piles, decreasing_piles;
    vector<vector<ll>> prefix_sum;

    for (int i = 0; i < n; ++i) {
        vector<ll> pile(m);
        for (ll &x : pile) {
            cin >> x;
        }

        bool inc = false;
        for (int j = 1; j < m; ++j) {
            if (pile[j] > pile[j - 1]) {
                inc = true;
            }
        }

        if (inc) {
            increasing_piles.push_back(pile);
            vector<ll>sums(m + 1);
            sums[0] = 0;
            for (int j = 1; j <= m; ++j) {
                sums[j] = sums[j - 1] + pile[j - 1];
            }
            prefix_sum.push_back(sums);
        } else {
            decreasing_piles.push_back(pile);
        }
    }

    vector<ll> greedy_top(m * (int)decreasing_piles.size() + 1);
    greedy_top[0] = 0;

    priority_queue<tuple<ll, int, int>>pq;
    

    for (int i = 0; i < (int)decreasing_piles.size(); ++i) {
        pq.push({decreasing_piles[i][0], i, 0});
    }
    
    int id = 0;
    while (!pq.empty()) {
        auto [val, i, j] = pq.top();
        pq.pop();
        id++;
        greedy_top[id] = greedy_top[id - 1] + val;
        if (j < m - 1) {
            pq.push({decreasing_piles[i][j + 1], i, j + 1});
        }
    }
    
    vector<pair<long long, int>> pile_order;

    for (int i = 0; i < (int)increasing_piles.size(); ++i) {
        ll total = 0;
        for (auto x : increasing_piles[i]) total += x;
        pile_order.emplace_back(total, i);
    }
    sort(pile_order.begin(), pile_order.end());
    reverse(pile_order.begin(), pile_order.end());
    
    ll answer = 0;
    
    vector<ll> sum_of_best((int)pile_order.size() + 1);
    sum_of_best[0] = 0;
    for (int i = 1; i <= (int)pile_order.size(); ++i) {
        sum_of_best[i] = sum_of_best[i - 1] + pile_order[i-1].first;
    }

    vector<vector<ll>> c1((int)pile_order.size() + 1), c2((int)pile_order.size() + 1);

    c1.back().resize(m + 1);

    for (auto &x : c1.back()) x = 0;

    for (int i = (int)pile_order.size() - 1; i >= 0; --i) {
        c1[i].resize(m + 1);
        for (int j = 0; j <= m; ++j) {
            c1[i][j] = max(c1[i + 1][j], prefix_sum[pile_order[i].second][j]);
        }
    }

    for (int i = 0; i < (int)pile_order.size(); ++i) {
        c2[i].resize(m+1);
        for (int j = 0; j <= m; ++j) {
            ll val = pile_order[i].first - prefix_sum[pile_order[i].second][j];
            if (i == 0) c2[i][j] = val;
            else c2[i][j] = min(c2[i - 1][j], val);
        }
    }

    for (int g = 0; g <= min(k, m * (int)decreasing_piles.size()); ++g) {
        // take g from decreasing piles and k - g from increasing
        if (k - g > m * (int)increasing_piles.size()) continue;
        
        int full_piles = (k - g) / m;
        int remainder = (k - g) - full_piles * m;
        
        // will need to speed this up
        
        // either take the best full_piles and search for remainder
        ll opt1 = sum_of_best[full_piles] + c1[full_piles][remainder];
        //ll best1 = 0;
        /*
        for (int j = 0; j < (int)increasing_piles.size(); ++j) {
            if (j < full_piles) {
                //opt1 += pile_order[j].first;
            } else {
                best1 = max(best1, prefix_sum[pile_order[j].second][remainder]);
            }
        }*/
        //opt1 += best1;
        ll opt2 = 0;
        //ll best2 = INF;
        // or don't take one of them
        if (full_piles + 1 <= (int)increasing_piles.size()) {
            opt2 = sum_of_best[full_piles + 1] - c2[full_piles][remainder] ;
            /*
            for (int j = 0; j <= full_piles; ++j) {
                opt2 += pile_order[j].first;
                best2 = min(best2, pile_order[j].first - prefix_sum[pile_order[j].second][remainder]);
            }
            opt2 -= best2;
            */
        }


        answer = max(answer, max(opt1, opt2) + greedy_top[g]);
    }

    cout << answer;
    



}

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