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
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
auto operator<<(ostream&o,auto p)->decltype(p.first,o){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(ostream&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
// clang-format on

const LL NINF = numeric_limits<LL>::min();
int nstacks, stack_each, to_take;
priority_queue<pair<LL, int>> pq;  // (val, stack)
vector<vector<LL>> stacks;
vector<int> normal, reversed;
vector<LL> scores_for_reversed;
vector<LL> scores_for_normal;

void solve_reversed();
void solve_normal();

int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> nstacks >> stack_each >> to_take;
    stacks.resize(nstacks, vector<LL>(stack_each));

    // Input
    REP (id, nstacks) {
        auto &st = stacks[id];
        for (auto &x : st) cin >> x;
        debug(id, st);
        if (is_sorted(st.begin(), st.end()))
            normal.emplace_back(id);
        else {
            reversed.emplace_back(id);
            reverse(st.begin(), st.end());
        }
    }

    // Process reversed and normal stacks separately
    solve_reversed();
    solve_normal();

    LL score = 0;
    FOR (take, 0, to_take)
        score = max(
          score, scores_for_reversed[take] + scores_for_normal[to_take - take]);
    cout << score << "\n";
    return 0;
}

void
solve_normal()
{
    if (normal.empty()) {
        scores_for_normal.assign(to_take + 1, 0);
        return;
    }

    debug("normal", normal);

    // Convert normal stacks to psums
    for (int id : normal) {
        vector<LL> psum(stack_each + 1);
        FOR (i, 1, stack_each) psum[i] = psum[i - 1] + stacks[id][i - 1];
        swap(stacks[id], psum);
    }

    // Sort normal by decreasing total sum
    sort(normal.begin(),
         normal.end(),
         [](int a, int b) { return stacks[a].back() > stacks[b].back(); });
    for (int id : normal) debug(id, stacks[id]);

    // Prefix sum when we take full stacks
    vector<LL> full_psum(ssize(normal) + 1);
    FOR (take, 1, ssize(normal))
        full_psum[take] = full_psum[take - 1] + stacks[normal[take - 1]].back();
    debug(full_psum);

    // full [0, ssize(normal)-1], left [0, stack_each - 1]
    // prepro: case1[full][left] = max i [0, full-1]  stacks[normal[i]][left] - stacks[normal[i]].back()
    vector<vector<LL>> case1(ssize(normal), vector<LL>(stack_each, NINF));
    FOR (full, 1, ssize(normal) - 1) {
        REP (left, stack_each) {
            case1[full][left] = max(case1[full - 1][left], stacks[normal[full - 1]][left] - stacks[normal[full - 1]].back());
        }
    }

    // prepro: case2[full][left] = max i [full, ssize(normal) - 1]  stacks[normal[i]][left]
    vector<vector<LL>> case2(ssize(normal), vector<LL>(stack_each, NINF));
    case2[ssize(normal) - 1] = stacks[normal[ssize(normal) - 1]];
    for (int full = ssize(normal) - 2; full >= 0; --full) {
        REP (left, stack_each) {
            case2[full][left] = max(case2[full + 1][left], stacks[normal[full]][left]);
        }
    }

    scores_for_normal.assign(to_take + 1, full_psum.back());
    FOR (k, 0, to_take) {
        if (k >= ssize(normal) * stack_each) break;
        int full = k / stack_each, left = k % stack_each;
        auto &score = scores_for_normal[k];
        score = max(full_psum[full + 1] + case1[full][left], full_psum[full] + case2[full][left]);

    }
    debug(scores_for_normal);
}

void
solve_reversed()
{
    debug("reversed", reversed);
    scores_for_reversed.resize(to_take + 1);
    scores_for_reversed[0] = 0;
    LL score               = 0;

    priority_queue<pair<LL, int>> pq;
    for (int id : reversed) pq.push({stacks[id].back(), id});
    FOR (take, 1, to_take) {
        if (!pq.empty()) {
            auto [val, id] = pq.top();
            pq.pop();

            score += val;
            stacks[id].pop_back();
            if (!stacks[id].empty()) pq.push({stacks[id].back(), id});
            debug(id, val, stacks[id]);
        }
        scores_for_reversed[take] = score;
    }
    debug(scores_for_reversed);
}