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
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define st first
#define nd second


const int MAX_N = 3e5;
const int MAX_K = 3e5;

int n, m, k;
ll sum_of_pancakes[MAX_N + 1], sum_of_x_pancakes[MAX_N + 1];
ll best_decreasing[MAX_K + 1], best_increasing[MAX_K + 1];

vector<ll> pancakes[MAX_N + 1];
vector<pair<ll, int>> sorted_increasing;
vector<int> decreasing, increasing;
priority_queue<pair<ll, pair<int, int>>> pq;
set<pair<ll, int>> s;

bool is_decreasing(int i){
    for(int j = 1; j < m; j++){
        if (pancakes[i][j] > pancakes[i][j-1]) return false;
    }
    return true;
}

void calc_best_decreasing(){
    for(int i : decreasing){
        pq.push({pancakes[i][0], {i, 0}});
    }

    int cnt = 1;
    ll sum = 0;
    while(cnt <= k && !pq.empty()){
        auto top = pq.top();
        pq.pop();

        int i = top.nd.st;
        int j = top.nd.nd;

        sum += top.st;
        best_decreasing[cnt] = sum;

        if (j + 1 < m){
            pq.push({pancakes[i][j+1], {i, j+1}});
        }
        cnt++;
    }
}

void sum_up_all_pancakes(){
    for(int i : increasing){
        for(ll pancake : pancakes[i]){
            sum_of_pancakes[i] += pancake;
        }
    }
}

void sum_up_first_x_pancakes(int x){
    for(int i : increasing){
        sum_of_x_pancakes[i] += pancakes[i][x-1];
    }
}
void create_sorted_increasing(){
    for(int i : increasing){
        sorted_increasing.push_back({sum_of_pancakes[i], i});
    }
    sort(sorted_increasing.begin(), sorted_increasing.end());
    reverse(sorted_increasing.begin(), sorted_increasing.end());
}

void init_untaken_set(){
    for(int i = 0; i < sorted_increasing.size(); i++){
        s.insert({sum_of_x_pancakes[sorted_increasing[i].nd], i});
    }
}

void calc_best_increasing(){
    ll sum = 0;
    for(int j = 0; j < sorted_increasing.size(); j++){
        sum += sorted_increasing[j].st;
        best_increasing[(j+1)*m] = sum;
    }

    for(int i = 1 ; i < m; i++){
        ll sum = 0;
        ll best_taken = LLONG_MAX;
        sum_up_first_x_pancakes(i);
        init_untaken_set();
        for(int j = 0; j*m + i <= sorted_increasing.size()*m ; j++){
            if (j == 0) best_increasing[i + j*m] = s.rbegin()->st;
            else {
                ll next_val = sorted_increasing[j].st;
                ll cand1 = next_val + sum - best_taken;
                ll cand2 = sum + s.rbegin()->st;
                best_increasing[i + j*m] = max(cand1, cand2);
            }
            ll val = sorted_increasing[j].st;
            int pos = sorted_increasing[j].nd;
            sum+=val;
            s.erase({sum_of_x_pancakes[pos], j});
            if (val - sum_of_x_pancakes[pos] < best_taken){
                best_taken = val - sum_of_x_pancakes[pos];
            }
        }
        s.clear();
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m >> k;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            ll p;
            cin >> p;
            pancakes[i].push_back(p);
        }
        if (is_decreasing(i)) decreasing.push_back(i);
        else increasing.push_back(i);
    }

    calc_best_decreasing();

    sum_up_all_pancakes();
    create_sorted_increasing();
    calc_best_increasing();

    ll res = 0;
    res = max(best_decreasing[k], best_increasing[k]);
    for(int i = 1; i < k; i++){
        if (best_decreasing[i] == 0 || best_increasing[k - i] == 0) continue;
        res = max(res, best_decreasing[i] + best_increasing[k - i]);
    }
    cout << res << "\n";
}