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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define pii pair<ll,ll>
#define vii vector<pair<ll,ll>>
#define vi vector<ll>
#define pll pair<ll, ll>
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define count_bits(x) __builtin_popcountll((x))
const ll M = 1e9+7;
const ll INF = 1e9;
//mt19937 mt;void random_start(){mt.seed(chrono::time_poll_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;

const ll MAXN = 3e5 + 5;

vi A[MAXN];
ll S[MAXN];
vi pref[MAXN];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n, m, k;
    cin >> n >> m >> k;
    ll x;
    for(ll i = 1; i <= n; ++i){
        A[i].push_back(0);
        pref[i].push_back(0);
        for(ll j = 1; j <= m; ++j){
            cin >> x;
            S[i] += x;
            A[i].push_back(x);
            pref[i].push_back(pref[i].back() + x);
        }
    }
    vi rest;
    vi sensowne;
    for(ll i = 1; i <= n; ++i){
        bool c = true;
        for(ll j = 1; j < m; ++j){
            if(A[i][j] < A[i][j+1]) c = false;
        }
        if(m == 1 or c){
            for(ll j = 1; j <= m; ++j){
                rest.push_back(A[i][j]);
            }
        }
        else{
            sensowne.push_back(i);
        }
    }
    sort(all(rest));
    ll K = siz(rest);
    rest.push_back(0);
    reverse(all(rest));
    vi prefsum;
    prefsum.push_back(0);
    for(int i = 1; i <= K; ++i){
        prefsum.push_back(prefsum.back() + rest[i]);
    }
    ll W = 0;
    ll ans = 0;
    for(ll i = 0; i < m; ++i){
        if(i > k) break;
        if(i > K) break;
        ll pocz = i + 1;
        vi O;
        while(true){
            ll kon = pocz + m - 1;
            if(kon > K) break;
            O.push_back(prefsum[kon] - prefsum[pocz - 1]);
            pocz += m;
        }
        ll tmp = k - i;
        ll reszta = tmp % m;
        ll cale = tmp / m;
        for(auto u : sensowne){
            O.push_back(S[u]);
        }
        sort(all(O), greater<ll>());
        // cout << cale << " " << reszta << "\n";
        // for(auto u : O){
        //     cout << u << " ";
        // }
        // cout << "ZERAAAAAA\n";
        if(siz(O) <= cale){
            ll curr = W;
            for(auto u : O){
                curr += u;
            }
            ans = max(ans, curr);
            W += rest[i+1];
            continue;
        }

        ll curr = W;
        ll mini;
        for(ll j = 0; j < cale; ++j){
            curr += O[j];
        }
        
        ans = max(ans, curr);
        for(auto j : sensowne){
            ll vl = pref[j][reszta];
            ll Y;
            if(cale == 0 or S[j] < O[cale-1]) Y = 0;
            else Y = (S[j] - (cale != siz(O) ? O[cale] : 0));
            
            ans = max(ans, curr + vl - Y);
        }

        W += rest[i+1];
    }
    cout << ans << "\n";
    return 0;
}