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
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

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

    std::vector<long long int> elements_from_decreasing_sequences;
    std::vector<std::set<std::pair<long long int, int>>> increasing_sequence_tiers_pre_adjust;
    std::vector<std::set<std::pair<long long int, int>>> increasing_sequence_tiers_post_adjust;
    increasing_sequence_tiers_pre_adjust.resize(m);
    increasing_sequence_tiers_post_adjust.resize(m);

    std::vector<std::pair<long long int, int>> best_increasing_sequences;

    std::vector<std::vector<std::set<std::pair<long long int, int>>::iterator>> increasing_to_erase_when_sequence_selected;
    increasing_to_erase_when_sequence_selected.resize(n);
    for (auto& v : increasing_to_erase_when_sequence_selected) {
        v.reserve(m);
    }

    std::vector<long long int> stack;
    stack.reserve(m);
    for (int i = 0; i < n; i++) {
        bool is_increasing = false;
        for (int j = 0; j < m; j++) {
            long long int x;
            scanf("%lld", &x);
            stack.push_back(x);

            if (stack.size() >= 2 && stack[stack.size() - 2] < stack[stack.size() - 1]) {
                is_increasing = true;
            }
        }

        if (is_increasing) {
            long long int accum = 0;
            for (int j = 0; j < m; j++) {
                accum += stack[j];
                auto it = increasing_sequence_tiers_pre_adjust[j].insert({accum, i}).first;
                increasing_to_erase_when_sequence_selected[i].push_back(it);
            }
            best_increasing_sequences.push_back({accum, i});
        } else {
            std::copy(stack.begin(), stack.end(),
                    std::back_inserter(elements_from_decreasing_sequences));
        }

        stack.clear();
    }

    // Handle decreasing sequences
    std::sort(elements_from_decreasing_sequences.begin(), elements_from_decreasing_sequences.end(),
            std::greater<long long int>());
    std::vector<long long int> best_for_decreasing;
    best_for_decreasing.reserve(k + 1);
    {
        best_for_decreasing.push_back(0);
        long long int accum = 0;
        for (int i = 0; i < std::min(size_t(k), elements_from_decreasing_sequences.size()); i++) {
            accum += elements_from_decreasing_sequences[i];
            best_for_decreasing.push_back(accum);
        }
        best_for_decreasing.resize(k + 1, 0);
    }

    // Handle increasing sequences
    best_increasing_sequences.push_back({0, -1}); // Sentinel
    std::sort(best_increasing_sequences.begin(), best_increasing_sequences.end(),
            std::greater<std::pair<long long int, int>>());
    std::vector<long long int> best_for_increasing;
    const int increasing_total_count = m * increasing_sequence_tiers_pre_adjust[0].size();
    best_for_increasing.reserve(k + 1);
    {
        best_for_increasing.push_back(0);
        long long int full_sequence_accum = 0;
        int num_full_sequences = 0;
        for (int i = 0; i < std::min(k, increasing_total_count); i++) {
            int tier = i % m;
            std::pair<long long int, int> selected = {0, -1};
            if (!increasing_sequence_tiers_pre_adjust[tier].empty()) {
                selected = *increasing_sequence_tiers_pre_adjust[tier].rbegin();
                selected.first += full_sequence_accum;
            }
            if (!increasing_sequence_tiers_post_adjust[tier].empty()) {
                auto candidate = *increasing_sequence_tiers_post_adjust[tier].rbegin();
                candidate.first += full_sequence_accum + best_increasing_sequences[num_full_sequences].first;
                selected = std::max(selected, candidate);
            }

            best_for_increasing.push_back(selected.first);

            if (tier == m - 1) {
                // TODO: Do most of the stuff here
                const auto full_sequence_to_add = best_increasing_sequences[num_full_sequences++];
                full_sequence_accum += full_sequence_to_add.first;
                for (int j = 0; j < m; j++) {
                    auto it = increasing_to_erase_when_sequence_selected[full_sequence_to_add.second][j];
                    auto p = *it;
                    increasing_sequence_tiers_pre_adjust[j].erase(it);
                    p.first -= full_sequence_to_add.first;
                    increasing_sequence_tiers_post_adjust[j].insert(p);
                }
            }
        }
        best_for_increasing.resize(k + 1, 0);
    }

    // fprintf(stderr, "Best for decreasing:");
    // for (int i = 0; i <= k; i++) {
    //     fprintf(stderr, " %lld", best_for_decreasing[i]);
    // }
    // fprintf(stderr, "\n");

    // fprintf(stderr, "Best for increasing:");
    // for (int i = 0; i <= k; i++) {
    //     fprintf(stderr, " %lld", best_for_increasing[i]);
    // }
    // fprintf(stderr, "\n");

    // Merge
    long long int best_sum = 0;
    for (int i = 0; i <= k; i++) {
        best_sum = std::max(best_sum, best_for_decreasing[i] + best_for_increasing[k - i]);
    }

    printf("%lld\n", best_sum);

    return 0;
}