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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vll = vector<ll>;

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

#define rep(i,b,e) for(int i=(b); i<(e); ++i)
#define each(a,x) for(auto &a : (x))
#define all(x) (x).begin(),(x).end()
#define sz(x) int((x).size())

struct SegmentTree{ // (=,max), (punkt, przedział)
    vector<pair<ll,int>> val;
    int M=1;

    void build(int n){
        n+=2;
        while(M<=n) M*=2;
        val.resize(2*M);
        rep(i,M,2*M)
            val[i] = {0,i-M};
    }

    void update(int a, ll v){
        val[a+=M].fi = v;
        while(a!=1){
            a/=2;
            if(val[2*a] >= val[2*a+1])
                val[a] = val[2*a];
            else
                val[a] = val[2*a+1];
        }
    }

    pair<ll,int> query(int a, int b){ // -> (maksimum, pierwszy indeks z maksimum)
        a += M;
        b += M;
        pair<ll,int> ans = val[a];
        if(val[b].fi > val[a].fi) 
            ans = val[b];
        while(a/2 != b/2){
            if(!(a&1) && (val[a+1].fi > ans.fi || (val[a+1].fi == ans.fi && val[a+1].se < ans.se)))
                ans = val[a+1];
            if((b&1) && (val[b-1].fi > ans.fi || (val[b-1].fi == ans.fi && val[b-1].se < ans.se)))
                ans = val[b-1];
            a>>=1;
            b>>=1;
        }
        return ans;
    }
};

ll sum_vll(const vll &arr){
    ll sum = 0;
    each(x,arr)
        sum += x;
    return sum;
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n,m,k;
    cin >> n >> m >> k;

    vector<vll> incr,decr;
    rep(w,0,n){
        vll seq(m);
        each(x,seq)
            cin >> x;
        if(seq.front() <= seq.back()){
            rep(i,1,sz(seq)) // od razu aplikujemy s. prefiksowe
                seq[i] += seq[i-1];
            incr.eb(seq);
        }else{
            reverse(all(seq));
            decr.eb(seq);
        }
    }   
    // cerr << "INCR:" << endl;
    // each(seq,incr){
    //     each(elem,seq) cerr << elem << " ";
    //     cerr << endl;
    // }
    // cerr << "DECR:" << endl;
    // each(seq,decr){
    //     each(elem,seq) cerr << elem << " ";
    //     cerr << endl;
    // }
    // Sklejamy ciągi malejące w jeden długi ciąg
    vll decr_flat;
    {
        priority_queue<pair<ll,ll>> pq; 
        rep(i,0,sz(decr)){
            pq.push({decr[i].back(), i});
            decr[i].pop_back();
        }
        // cerr << "constructing decr_flat..." << endl;

        while(!pq.empty()){
            auto [val, i] = pq.top();
            pq.pop();
            if(!decr[i].empty()){
                pq.push({decr[i].back(), i});
                decr[i].pop_back();
            }
            decr_flat.eb(val);
        }
        // Sumy prefiksowe
        rep(i,1,sz(decr_flat))
            decr_flat[i] += decr_flat[i-1];
    }

    // Posortuj ciągi rosnące
    sort(all(incr), [&](const vll &a, const vll &b){
        return a.back() > b.back();
    });
    // Sumy prefiksowe po sumach ciągach rosnących
    vll incr_block_sum(sz(incr));
    vll incr_block_prefsum(sz(incr));
    rep(i,0,sz(incr)){
        incr_block_sum[i] = incr[i].back();
        if(i > 0) incr_block_prefsum[i] += incr_block_prefsum[i-1];
        incr_block_prefsum[i] += incr[i].back();
    }
    // cerr << "incr_block_sum: ";
    // rep(i,0,sz(incr_block_sum)) cerr << incr_block_sum[i] << " ";
    // cerr << endl;
    // cerr << "incr_block_prefsum: ";
    // rep(i,0,sz(incr_block_sum)) cerr << incr_block_prefsum[i] << " ";
    // cerr << endl;
    

    // Tworzymy mnóstwo drzew przedziałowych
    vector<SegmentTree> tree(m), tree_reduced(m);
    rep(i,0,m){
        tree[i] = SegmentTree();
        tree[i].build(sz(incr));

        tree_reduced[i] = SegmentTree();
        tree_reduced[i].build(sz(incr));
    }

    rep(i,0,sz(incr)){
        rep(j,0,m){
            tree[j].update(i, incr[i][j]);
            tree_reduced[j].update(i, incr[i][j] - incr_block_sum[i]);
        }
    }


    int elems_incr = k, elems_decr = 0;
    while(elems_incr > sz(incr) * m){
        --elems_incr;
        ++elems_decr;
    }
    // cerr << "init ELEMS_INCR:" << elems_incr << endl;
    // cerr << "init ELEMS_DECR:" << elems_decr << endl;
    // cerr << "INCR:" << endl;
    // each(seq,incr){
    //     each(elem,seq) cerr << elem << " ";
    //     cerr << endl;
    // }
    // cerr << "DECR_FLAT:" << endl;
    // each(elem,decr_flat){
    //     cerr << elem << " ";
    // }
    // cerr << endl;


    ll ans = 0;
    while(elems_incr >= 0
       && elems_decr <= sz(decr) * m){
        // cerr << "ELEMS_INCR:" << elems_incr << endl;
        // cerr << "ELEMS_DECR:" << elems_decr << endl;

        ll decr_val = elems_decr > 0 ? decr_flat[elems_decr-1] : 0;
        ll incr_val = 0;
        // cerr << " decr_val: " << decr_val << endl;
        
        const int cnt_whole_blocks = elems_incr / m; 
        const int rest = elems_incr % m;
        // cerr << "rest: " << rest << endl;
        // cerr << "cnt_whole_blocks: " << cnt_whole_blocks << endl;
        
        if(rest == 0){
            // cerr << "REST = 0" << endl;
            incr_val = cnt_whole_blocks > 0 ? incr_block_prefsum[cnt_whole_blocks-1] : 0;
        }else{
            // cerr << "REST =/= 0" << endl;
            // 1. trzeba wywalić jeden z użytych całych bloków
            if(cnt_whole_blocks > 0){
                ll incr_block_val = cnt_whole_blocks > 0 ? incr_block_prefsum[cnt_whole_blocks-1] : 0;
                auto [incr_rest_val, pos] = tree_reduced[rest-1].query(0, cnt_whole_blocks-1);
                // incr_block_val -= incr_block_sum[pos];
                incr_block_val += incr_block_sum[cnt_whole_blocks];

                incr_val = max(incr_val, incr_block_val + incr_rest_val);
                // cerr << "  incr_val_opt1: " << incr_block_val + incr_rest_val << " = " << incr_block_val << " + " << incr_rest_val << endl;
            }
            // 2. trzeba dołożyć kawałek nowego bloku
            {
                ll incr_block_val = cnt_whole_blocks > 0 ? incr_block_prefsum[cnt_whole_blocks-1] : 0;
                auto [incr_rest_val, pos] = tree[rest-1].query(cnt_whole_blocks, sz(incr)-1);
                
                incr_val = max(incr_val, incr_block_val + incr_rest_val);
            // cerr << "  incr_val_opt2: " << incr_block_val + incr_rest_val << " = " << incr_block_val << " + " << incr_rest_val << endl;
                
            } 
        }
        // cerr << " incr_val: " << incr_val << endl;
        // cerr << "cur: " << incr_val + decr_val << endl;
        // cerr << endl;

        ans = max(ans, incr_val + decr_val);
        --elems_incr;
        ++elems_decr; 
    }
    cout << ans;
    
}