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
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <deque>
#include <tuple>
#include <bitset>

using namespace std;

#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define x first
#define y second

typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vi;
typedef vector<ll> vll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, m, k;
    cin >> n >> m >> k;

    vector<vll> A(n);
    vector<vll> A_pref(n);

    vll D;
    vector<vll> I_pref;
    for(int i=0; i<n; i++){
        ll pref = 0;
        A[i].resize(m);
        A_pref[i].resize(m);
        for(int j=0; j<m; j++){
            ll a;
            cin >> a;
            pref+=a;
            A[i][j] = a;
            A_pref[i][j] = pref;

        }
        if(A[i][m-1]<=A[i][0]){
            for(auto a: A[i]){
                D.pb(a);
            }
        }else{
            I_pref.pb(A_pref[i]);

        }
    }

    sort(D.begin(), D.end(), greater<>());
    int d = min(k, (int)D.size());

    vll D_best(d+1, 0);
    for(int i=0; i<d; i++){
        D_best[i+1] = D_best[i] + D[i];
    }

    if(I_pref.empty()){
        cout << D_best[k] << "\n";
        return 0;
    }

    sort(I_pref.begin(), I_pref.end(), [](const vll& a, const vll& b) {
        return a.back() > b.back(); 
    });

    vector<vll> I_suff_max(m);
    for(int i=0; i<m; i++){
        I_suff_max[i].resize(I_pref.size());
        I_suff_max[i].back() = I_pref.back()[i];
        for(int j=I_pref.size()-2; j>=0; j--){
            I_suff_max[i][j] = max(I_pref[j][i], I_suff_max[i][j+1]);
        }
    }

    vector<vll> I_pref_max(m);
    for(int i=0; i<m; i++){
        I_pref_max[i].resize(I_pref.size());
        I_pref_max[i][0] = I_pref[0][i] - I_pref[0].back();
        for(int j=1; j<I_pref.size(); j++){
            I_pref_max[i][j] = max(I_pref_max[i][j-1], I_pref[j][i] - I_pref[j].back());
        }
    }

    int ii = min(k, n*m-(int)D.size());
    
    vll I_best(ii+1, 0);
    ll i_sum = 0;
    ll i_rows = 0;
    int i_cur = 0;
    for(int i=0; i<ii; i++){
        I_best[i+1] = i_sum + max(
            I_suff_max[i_cur][i_rows], 
            I_pref[i_rows].back() + I_pref_max[i_cur][i_rows]
        );
        i_cur++;

        if(i_cur==m){
            i_sum += I_pref[i_rows].back();
            i_rows++;
            i_cur = 0;
        }
    }

    ll ans = 0;
    for(int i=max(0, k-ii); i<=d; i++){
        ll cur_ans = D_best[i] + I_best[k-i];
        ans = max(ans, cur_ans);
    }

    cout << ans << "\n";

    return 0;
}