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
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
ll inf = 1e12;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    ll n, m, k;
    cin >> n >> m >> k;
    ll k_static = n * m;
    vector<vector<ll>> arr(n, vector<ll>(m));
    vector<vector<ll>> pref(n, vector<ll>(m));
    priority_queue<ll> next;
    vector<bool> raise(n, true);
    for(ll i = 0; i < n; i++){
        ll sum = 0;
        ll first = inf;
        for(ll j = 0; j < m; j++){
            cin >> arr[i][j];

            if(arr[i][j] > first){
                raise[i] = false;
            }
            first = arr[i][j];
            sum += arr[i][j];
            pref[i][j] = sum;


        }
        if(raise[i]){
            k_static -= m;

            for(ll j = 0; j < m; j++){
                next.push(arr[i][j]);
            }
        }
    }
    vector<ll> dp(n*m+1);
    vector<ll> done(n);
    for(ll j = 0; j < m; j++){
        if(k_static == 0){
            break;
        }
        priority_queue<pair<ll, ll>> sums;
        priority_queue<pair<ll, ll>> lesser;

        for(ll i = 0; i < n; i++){
            if(!raise[i]){
                lesser.push({pref[i][j] - arr[i][j], i});
                sums.push({pref[i][m-1], i});
            }
            done[i] = 0;
        }

        ll x = 0;
        ll val = 0;
        for(ll i = 0; i * m + j <= min(k, k_static); i++){

            if(i == 0){
                auto [a, b] = lesser.top();
                lesser.pop();
                val = a;
                x = b;
                dp[j] = val;
                continue;
            }
            auto [a, b] = sums.top();
            if(b != x){
                sums.pop();
                done[b] = 1;
                dp[i*m+j] = dp[(i-1)*m+j] + a;
                continue; 
            }
            if(lesser.empty()){
                dp[i*m+j] = dp[(i-1)*m+j] + a - val;
                continue;
            }
            auto [c, y] = lesser.top();
            if(done[y] == 1){
                lesser.pop();
                i--;
                continue;   
            }
            sums.pop();
            auto [e, f] = sums.top();
            if(a - val + c >= e){
                dp[i*m+j] = dp[(i-1)*m+j] + a - val + c;
                x = y;
                val = c;
                lesser.pop();
            }else{
                dp[i*m+j] = dp[(i-1)*m+j] + e;
                done[f] = 1;
                sums.pop();
                sums.push({a, b});
            }
        }
    }

    ll ans = dp[k];
    ll tmp = 0;
    for(ll l = k - 1; l >= 0; l--){
        if(next.empty()){
            break;
        }
        auto a = next.top();
        next.pop();
        tmp += a;
        ans = max(ans, dp[l] + tmp);
    }
    cout << ans << '\n';
    



    


    

}