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
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define fi first
#define sn second

typedef long long ll;
typedef vector<ll> VI;
typedef vector<char> VC;
typedef pair<int, int> PI;

const int DUNNO = 0;
const int ASC = 1;
const int DESC = 2;

// const ll INF = ((ll)300 * (ll)1000) * (ll)1000 * (ll)1000 * (ll)1000 * (ll)1000 + (ll)7;
ll INF = LLONG_MAX;
// struct Sortable {
//     VI& v;
//     ll total;
// };

struct SortableColumn {
    // VI v;
    VI pref;
    ll sum;
};

struct BestVals {
    VI vals;
    int cnt;
};

// bool operator<(const Sortable& a, const Sortable& b) {
//     return a.total > b.total;
// }


bool operator<(const SortableColumn& a, const SortableColumn& b) {
    return a.sum > b.sum;
}


int main() {
#ifdef DEBUG
    cout << "Debug mode" << endl;
#endif
    int n, k, m;
    cin >> n >> m >> k;

    vector<VI> pancakes(n);

    // map < ll, BestVals> M;
    vector<SortableColumn> asc_cols;

    VI all_desc;
    // VI ascending;
    VI descending;
    VI ascending(1, 0);

    ll sum_asc = 0;
    ll cnt_asc = 0;

    for (int i = 0; i < n; i++) {
        pancakes[i].resize(m);
        ll total = 0;
        for (int j = 0; j < m; j++) {
            cin >> pancakes[i][j];
            total += pancakes[i][j];
        }

        int monotono = DUNNO;
        for (int j = 1; j < m; j++) {
            if (pancakes[i][j] > pancakes[i][j - 1]) {
                monotono = ASC;
                break;
            }
            else if (pancakes[i][j] < pancakes[i][j - 1]) {
                monotono = DESC;
                break;
            }
        }

        if (monotono == DESC or monotono == DUNNO) {
            all_desc.insert(all_desc.end(), pancakes[i].begin(), pancakes[i].end());
        }
        else {
            SortableColumn col;
            // col.v = pancakes[i];
            col.sum = total;
            col.pref = VI(m + 1, 0);

            for (int j = 1; j <= m; j++) {
                col.pref[j] = col.pref[j - 1] + pancakes[i][j - 1];
            }

            asc_cols.pb(col);
        }
    }

    sort(all_desc.rbegin(), all_desc.rend());
    sort(asc_cols.begin(), asc_cols.end());

#ifdef DEBUG
    cout << "all_desc" << endl;
    for (int i = 0; i < all_desc.size(); i++) {
        cout << all_desc[i] << " ";
    }
    cout << endl;
#endif

    int desc_num = (int)all_desc.size();

    descending.resize(desc_num + 1);
    descending[0] = 0;
    for (int i = 1; i <= desc_num; i++)
        descending[i] = descending[i - 1] + all_desc[i - 1];

    int n0 = asc_cols.size();

    VI full(n0 + 1, 0);
    for (int i = 1; i <= n0; i++) {
        full[i] = full[i - 1] + asc_cols[i - 1].sum;
    }


#ifdef DEBUG
    //DBG
    cout << "n0: " << n0 << endl;
    for (int i = 0; i <= n0; i++) {
        cout << full[i] << " ";
    }
    cout << endl;
    //DBG
#endif

    vector<VI> best_pref(n0 + 1);
    vector<VI> best_suff(n0 + 2);
    best_suff[n0 + 1] = VI(m + 1, 0);

    for (int i = 0; i <= n0; i++) {
        best_pref[i] = VI(m + 1, 0);
        best_suff[i] = VI(m + 1, 0);
    }
#ifdef DEBUG
    //DBG
    cout << "stuff " << n0 << "x" << m + 1 << endl;
    cout << "asc_cols" << asc_cols.size() << endl;
    //DBG
#endif
    for (int r = 1; r < m; r++) {
        best_pref[0][r] = -INF;
        for (int q = 1; q <= n0; q++) {
            best_pref[q][r] = max(best_pref[q - 1][r], asc_cols[q - 1].pref[r] - asc_cols[q - 1].sum);
        }

        best_suff[n0 + 1][r] = -INF;
        for (int q = n0; q >= 1; q--) {
            best_suff[q][r] = max(best_suff[q + 1][r], asc_cols[q - 1].pref[r]);
        }
    }


#ifdef DEBUG
    //DBG
    for (int r = 0; r < m; r++) {
        for (int q = 0; q <= n0; q++) {
            cout << best_pref[q][r] << " ";
        }
        cout << endl;
    }
    for (int r = 0; r < m; r++) {
        for (int q = 0; q <= n0; q++) {
            cout << best_suff[q][r] << " ";
        }
        cout << endl;
    }
    cout << "end_stuff" << endl;
    //DBG
#endif

    int asc_num = n0 * m;
    // cout << asc_num + 1 << endl;
    if (asc_num > 0) {
        ascending = VI(asc_num + 1, 0);
    }
    // VI ascending(asc_num + 1, 0);

    for (int q = 0; q <= n0; q++) {
        ascending[q * m] = full[q];
    }


    for (int q = 0; q < n0; q++) {
        for (int r = 1; r < m; r++) {
            ascending[q * m + r] = max(
                full[q] + best_suff[q + 1][r],
                full[q + 1] + best_pref[q][r]
            );
        }
    }


#ifdef DEBUG
    //DBG
    for (int i = 0; i <= k; i++) {
        cout << i << " ";
    }
    cout << endl;
    for (int i = 0; i < ascending.size(); i++) {
        cout << ascending[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < descending.size(); i++) {
        cout << descending[i] << " ";
    }
    cout << endl;
    cout << "sizes " << ascending.size() << " " << descending.size() << endl;
    //DBG
#endif


    ll res = 0;
    for (int k0 = 0; k0 <= k; k0++) {
        if (k0 < ascending.size() && k - k0 < descending.size()) {
#ifdef DEBUG
            //DBG
            cout << k0 << " " << k - k0 << ": " << res << " " << ascending[k0] << " + " << descending[k - k0] << endl;
            //DBG
#endif
            res = max(res, ascending[k0] + descending[k - k0]);
        }
    }
    cout << res << endl;


    return 0;
}