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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <string>
#include <vector>

#ifdef LOCAL
    #define dbg(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dbg(...)
#endif

using namespace std;

struct AscTower {
    vector<long long> values;
    vector<long long> partvals; // partvals[x] is the sum when picking first x numbers from values
};

vector<vector<long long>> stoses;

vector<long long> descendings;
vector<AscTower> ascendings;
vector<vector<long long>> partvals;

// only_rising_results[B][A] is the highest score when only eating A full ascending towers and B top elements from one ascending tower.
vector<vector<long long>> only_rising_results;

long long bestres[300001];

int main() {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);

    for (int i = 0; i < n ; ++i) {
        vector<long long> stos;
        bool descending = true;
        for (int j = 0; j < m; ++j) {
            long long nalval;
            scanf("%lld", &nalval);
            stos.push_back(nalval);
            if (j == 0) continue;
            // every stos is descending, until we see an ascending pair
            if (nalval > stos[j-1]) descending = false;
        }
        if (descending) {
            descendings.insert(descendings.end(), stos.begin(), stos.end());
        } else {
            auto tower = AscTower(stos, vector<long long>());
            ascendings.push_back(tower);
        }
    }

    dbg("SORTING DESCENDINGS\n");
    if (!descendings.empty()) ranges::sort(descendings, greater<>());

    dbg("ASSEMBLING ASCENDINGS\n");
    for (auto & ascending : ascendings) {
        ascending.partvals.reserve(m+1);
        ascending.partvals.push_back(0LL);
        for (int j = 0; j < m; ++j) {
            ascending.partvals.push_back(ascending.partvals[j] + ascending.values[j]);
        }
    }

    dbg("SORTING ASCENDINGS\n");
    ranges::sort(ascendings, [&](const AscTower& a, const AscTower& b) {
        return a.partvals[m] > b.partvals[m];
    });

    dbg("ALL DESCENDINGS:\n");
    for (unsigned long i = 0; i < descendings.size(); ++i) {
        dbg("%lld\n", descendings[i]);
    }

    dbg("ALL ASCENDING SUMS:\n");
    for (unsigned long i = 0; i < ascendings.size(); ++i) {
        for (unsigned long j = 0; j <= m; ++j) {
            dbg("%lu %lu:  %lld\n", i, j, ascendings[i].partvals[j]);
        }
        //dbg("%lld\n", partvals[i][m-1]);
    }

    // COMPUTE RESULTS FOR topval = 0 (only full towers)
    dbg("COMPUTE ONLY-FULL-ASCENDING RESULTS\n");
    only_rising_results.push_back(vector{0LL});
    for (unsigned int i = 0; i < ascendings.size(); ++i) {
        only_rising_results[0].push_back(only_rising_results[0][i] + ascendings[i].partvals[m]);
    }

    dbg("CREATE PARTVAL INDICES STUFF\n");
    std::vector<int> partval_indices(ascendings.size());
    std::iota(partval_indices.begin(), partval_indices.end(), 0);
    dbg("START TOPVAL ITERATION\n");
    for (int topvals = 1; topvals < m; ++topvals) {
        // partval_indices are sorted descending based on sum of `topvals` top values from ascending stos of this idx
        ranges::sort(partval_indices, [&](const int a, const int b) {
            return ascendings[a].partvals[topvals] > ascendings[b].partvals[topvals];
        });

        if (ascendings.empty()) {
            only_rising_results.push_back(vector{0LL});
            continue;
        }

        // start with no full towers and highest possible partial
        only_rising_results.push_back(vector{ascendings[partval_indices[0]].partvals[topvals]});


        dbg("-------------------------------------------");
        dbg("Topval %d: \n", topvals);
        for (unsigned long i = 0; i < partval_indices.size(); ++i) {
            dbg("%3d (%6lld) ", partval_indices[i], ascendings[partval_indices[i]].partvals[topvals]);
        }
        dbg("\n");

        // We will never use as lower partval index after a higher one
        vector<int> relevant_partval_indices = vector<int>();
        relevant_partval_indices.push_back(partval_indices[0]);
        int last_relevant_idx = partval_indices[0];
        for (unsigned long i = 1; i < partval_indices.size(); ++i) {
            if (partval_indices[i] > last_relevant_idx) {
                relevant_partval_indices.push_back(partval_indices[i]);
                last_relevant_idx = partval_indices[i];
            }
        }

        unsigned long current_partval_index = 0;
        unsigned long next_partval_index = 0;

        for (unsigned long i = 1; i < ascendings.size(); ++i) {
            // attempt to add i-th whole tower.

            dbg("Adding %3lu-th whole tower at topval %3d.\n", i, topvals);

            dbg("Relevant partval indices:\n");
            for (auto x: relevant_partval_indices) {
                dbg("%d ", x);
            }
            dbg("\n");

            dbg("Current partval index: %lu\n", current_partval_index);
            dbg("Next partval index: %lu\n", next_partval_index);

            // the tower currently used as the non-full
            unsigned long current_partval = relevant_partval_indices[current_partval_index];


            // We only need it if we aim to add tower of index i, so it should be higher than i
            while (next_partval_index <= current_partval_index || relevant_partval_indices[next_partval_index] < i) {
                next_partval_index++;
                if (next_partval_index >= relevant_partval_indices.size()) {
                    dbg("Next partval does not exist, breaking!\n");
                    break;
                }
            }

            dbg("Nest partval index after moving: %lu\n", next_partval_index);

            unsigned long next_partval = 10000000;
            if (next_partval_index >= relevant_partval_indices.size()) {
                dbg("Next partval does not exist!\n");
            } else {
                next_partval = relevant_partval_indices[next_partval_index];
            }

            dbg("Current partval: %3lu\n", current_partval);
            dbg("Next partval: %3lu\n", next_partval);

            if (i <= current_partval) {
                // ascendings[i] can be added with no issues
                dbg("Add whole tower %3lu (of sum %8lld)\n", i-1, ascendings[i-1].partvals[m]);
                only_rising_results[topvals].push_back(only_rising_results[topvals][i-1] + ascendings[i-1].partvals[m]);
            } else {
                // keep old partial and add next full tower
                long long keep_old_partial_result = only_rising_results[topvals][i-1] + ascendings[i].partvals[m];
                // replace old partial with full -> use the next relevant partval
                if (next_partval == 10000000) {
                    dbg("ERROR: SOMETHING WENT WRONG, NEXT PARTVAL SHOULD NOT BE AVAILABLE");
                }
                long long replace_old_partial_result = only_rising_results[topvals][i-1] - ascendings[current_partval].partvals[topvals] + ascendings[next_partval].partvals[topvals] + ascendings[current_partval].partvals[m];
                if (keep_old_partial_result > replace_old_partial_result) {
                    dbg("Keeping old partval. Adding whole tower %3lu (of sum %8lld)\n", i, ascendings[i-1].partvals[m]);
                    only_rising_results[topvals].push_back(keep_old_partial_result);
                } else {
                    dbg("Replacing old partval with whole tower %3lu (of sum %8lld)\n", current_partval, ascendings[current_partval].partvals[m]);
                    only_rising_results[topvals].push_back(replace_old_partial_result);
                    current_partval_index = next_partval_index;
                }
            }
            dbg("Best result for %3d size partial tower and %3lu full towers is %8lld\n", topvals, i, only_rising_results[topvals][i]);
        }
    }

    dbg("BEST RESULTS FOR ONLY-ASCENDING TOWERS:\n");
    for (int i = 0; i <= k; ++i) {
        int full_towers = i/m;
        int partial_tower_size = i%m;
        if (full_towers >= only_rising_results[partial_tower_size].size()) {
            bestres[i] = 0;
            dbg("%3d: %8lld (not enough ascending towers)\n", i, 0LL);
        } else {
            bestres[i] = only_rising_results[i%m][i/m];
            dbg("%3d: %8lld\n", i, only_rising_results[i%m][i/m]);
        }
    }

    long long best_total = 0;
    long long desctotal = 0;
    for (int i = 0; i <=k; ++i) {
        // best result using [i] top desc and [k-i] asc
        long long res = bestres[k-i] + desctotal;
        dbg("Res for %d is %lld\n", i, res);
        if (res > best_total) best_total = res;
        if (i >= descendings.size()) break;
        desctotal += descendings[i];
    }

    printf("%lld\n", best_total);
    return 0;
}