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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#pragma warning(disable:4786)
#pragma warning(disable:4996)
#include <bits/stdc++.h>
using namespace std;

#define MEM(a, b) memset(a, (b), sizeof(a))
#define CLR(a) memset(a, 0, sizeof(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) )
#define S(X) ( (X) * (X) )
#define SZ(V) (int )V.size()
#define FORN(i, n) for(int i = 0; i < n; i++)
#define FORAB(i, a, b) for(int i = a; i <= b; i++)
#define ALL(V) V.begin(), V.end()
#define IN(A, B, C)  ((B) <= (A) && (A) <= (C))
#define AIN(A, B, C) assert(IN(A, B, C))
#define LowerBound(c, x) distance((c).begin(), lower_bound(ALL(c), (x)))
#define UpperBound(c, x) distance((c).begin(), upper_bound(ALL(c), (x)))
#define UNIQUE(V) sort(ALL(V)), V.erase(unique(ALL(V)), V.end()), V.shrink_to_fit()

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }

template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto& x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << to_string(H); dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

template<class... T>
void input(T&... a) {
    (cin >> ... >> a);
}

void printsuc(int suc){
    if (suc == 1) cout << "\n";
    if (suc == 2) cout << " ";
}

template<class t>
void print_single(t x, int suc = 1){
    cout << x;
    printsuc(suc);
}

template<class t, class u>
void print_single(const pair<t, u>& p, int suc = 1) {
    print_single(p.first, 2);
    print_single(p.second, suc);
}

template<class T>
void print_single(const vector<T>& V, int suc = 1) {
    FORN(i, V.size()) print_single(V[i], i == int(V.size()) -1 ? 3 : 2);
    printsuc(suc);
}

template<class T, size_t N>
void print_single(const array<T, N>& V, int suc = 1) {
    FORN(i, N) print_single(V[i], i == int(N) - 1 ? 3 : 2);
    printsuc(suc);
}

template<class T>
void print(const T& t) {
    print_single(t);
}

template<class T, class ...Args>
void print(const T& t ,const Args&... args) {
    print_single(t, 2);
    print(args...);
}

// Alias function that calls print
template<class ...Args>
void output(const Args&... args) {
    print(args...);
}

typedef long long int LL;
typedef long long i64;
typedef unsigned long long u64;
typedef __int128 i128;
typedef unsigned __int128 u128;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<LL, int> PLI;
typedef pair<long double, long double> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VP;
typedef vector<long double> VD;
typedef vector<vector<int>> VVI;
typedef vector<vector<LL>> VVL;
typedef vector<string> VS;
// typedef long long double ld;
typedef unsigned long long ULL;
typedef unsigned long long u64;

//#define MAXN 1000
//#define MAXN2 MAXN*MAXN

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rng(10);
// shuffle(V.begin(), V.end(), rng);
// int u = rng();
// int u = uniform_int_distribution<long long>(L, R)(rng);
// long double u = uniform_real_distribution<float>(L, R)(rng);

// U R D L UR DR DL UL
int dr[] = {-1, 0, 1,  0, -1, 1,  1, -1};
int dc[] = { 0, 1, 0, -1,  1, 1, -1, -1};

// stable argument sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
    vector<int> ids(A.size());
    iota(ids.begin(), ids.end(), 0);
    sort(ids.begin(), ids.end(), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
    return ids;
}

// Usage:
//   Example: Sort by size
//       auto idx = argsort(words, [](const string& a, const string& b) { return a.size() < b.size(); });
template <typename T, typename Compare>
vector<int> argsort(const vector<T>& A, Compare comp) {
    vector<int> ids(A.size());
    iota(ids.begin(), ids.end(), 0);
    sort(ids.begin(), ids.end(), [&](int i, int j) {
        return comp(A[i], A[j]) || (!comp(A[j], A[i]) && i < j);
    });
    return ids;
}

// Usage:
// binary_search(check, l, r)   - returns the last index i in [l, r) where check(i) is true
// If none, returns l - 1.
template <typename F>
LL binary_search(F check, LL ok, LL ng) {
    if (!check(ok)) return ok - 1;
    while (ng - ok > 1) {
        auto x = (ng + ok) / 2;
        (check(x) ? ok : ng) = x;
    }
  return ok;
}

// Usage:
// binary_search_real(check, l, r)   - returns the last long double d in [l, r] where check(d) is true
template <typename F>
long double binary_search_real(F check, long double ok, long double ng, int iter = 100) {
  FORN(_, iter) {
    long double x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// MSB: (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(unsigned int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(i64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// LSB: (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(unsigned int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(i64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

const LL MOD = 1000000007;
// const LL MOD = 998244353;
// const LL INF = 2000000000000000001LL; //2e18 + 1

void solve(int ks) {
    int n, m, k;
    input(n, m, k);

    VVL inc, dec;
    FORN(_, n) {
        VL Z(m);
        FORN(i, m) input(Z[i]);
        if (Z[0] >= Z.back()) dec.push_back(Z);
        else inc.push_back(Z);
    }

    VL dec_all;
    for (auto V : dec) {
        for (LL v : V) {
            dec_all.push_back(v);
        }
    }
    sort(ALL(dec_all), greater<LL>());
    for (int i = 1; i < SZ(dec_all); i++) {
        dec_all[i] += dec_all[i - 1];
    }

    VPL inc_sum_idx;
    int inc_sz = SZ(inc);
    FORN(i, inc_sz) {
        LL sum = 0;
        for (LL v : inc[i]) sum += v;
        inc_sum_idx.push_back({sum, i});
    }
    sort(ALL(inc_sum_idx), greater<PLL>());
    VVL ordered_inc;
    VL cum_sum_inc;
    for (auto p : inc_sum_idx) {
        ordered_inc.push_back(inc[p.second]);
        LL sum_to_insert = p.first;
        if (!cum_sum_inc.empty()) sum_to_insert += cum_sum_inc.back();
        cum_sum_inc.push_back(sum_to_insert);
    }

    VVL suffix_mx = ordered_inc;
    for (int i = SZ(suffix_mx) - 1; i >= 0; i--) {
        for (int j = 1; j < m; j++) {
            suffix_mx[i][j] += suffix_mx[i][j - 1];
        }
        if (i < SZ(suffix_mx) - 1) {
            for (int j = 0; j < m; j++) {
                suffix_mx[i][j] = MAX(suffix_mx[i][j], suffix_mx[i + 1][j]);
            }
        }
    }

    VVL prefix_mx = ordered_inc;
    for (int i = 0; i < SZ(prefix_mx); i++) {
        for (int j = 1; j < m; j++) {
            prefix_mx[i][j] += prefix_mx[i][j - 1];
        }
        for (int j = 0; j < m; j++) {
            prefix_mx[i][j] -= prefix_mx[i][m - 1];
            if (i) prefix_mx[i][j] = MAX(prefix_mx[i][j], prefix_mx[i - 1][j]);
        }
    }

    VL inc_all;
    inc_all.push_back(0);
    int total_inc = SZ(prefix_mx) * m;
    for (int i = 1; i <= total_inc; i++) {
        int gota = i / m;
        int extra = i % m;

        if (extra == 0) {
            inc_all.push_back(cum_sum_inc[gota - 1]);
            continue;
        }

        LL op_suffix = (gota ? cum_sum_inc[gota - 1] : 0) + suffix_mx[gota][extra - 1];
        LL op_prefix = cum_sum_inc[gota] + prefix_mx[gota][extra - 1];
        inc_all.push_back(MAX(op_prefix, op_suffix));
    }

    LL ans = 0;
    for (int i = 0; i < SZ(inc_all); i++) {
        int rem = k - i;
        if (rem <= dec_all.size()) {
            LL cur = inc_all[i] + (rem == 0 ? 0 : dec_all[rem - 1]);
            ans = MAX(ans, cur);
        }
    }
    output(ans);
}

void gen() {
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    std::cout << fixed << std::setprecision(10);

    long double start_time = clock();
#ifdef LOCAL
    freopen("C:\\Home\\Contest\\sample.in", "r", stdin);
    // freopen("C:\\Home\\Contest\\0.out", "w", stdout);
#endif

    gen();

    if (0) {
        int T;
        input(T);
        for (int ks = 1; ks <= T; ks++) {
            solve(ks);
            if (ks % 1 == 0) dbg(ks, " done");
            long double time_elapsed = (clock() - start_time) / CLOCKS_PER_SEC;
            dbg(ks, ":", time_elapsed);
        }
    }
    else {
        solve(1);
    }

    long double TimeElapsed = (clock() - start_time) / CLOCKS_PER_SEC;
    dbg(TimeElapsed);
    return 0;
}