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
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <ranges>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef unsigned int uint;

const int INF = 1e9 + 9;
const ll LLINF = static_cast<ll>(2e18) + 7LL;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

struct Case {
  int n, m, k;
  vector<vector<ll>> a, asc, desc;
  vector<ll> best_asc, best_desc;

  void solve() {
    cin >> n >> m >> k;

    a.resize(n);
    REP(i, n) {
      a[i].resize(m);
      REP(j, m) { cin >> a[i][j]; }
    }

    split_to_asc_and_desc();

    solve_asc();
    DEB("best_asc:\n");
    REP(i, SZ(best_asc)) { DEB(best_asc[i] << " "); }
    DEB("\n");

    solve_desc();
    DEB("best_desc:\n");
    REP(i, SZ(best_desc)) { DEB(best_desc[i] << " "); }
    DEB("\n");

    ll result = calculate_result();

    cout << result << "\n";
  }

  void split_to_asc_and_desc() {
    REP(i, n) {
      if (is_asc(a[i])) {
        asc.PB(a[i]);
      } else {
        desc.PB(a[i]);
      }
    }
  }

  bool is_asc(const vector<ll> &t) {
    for (auto const &[first, second] : t | views::adjacent<2>) {
      if (second < first) {
        return false;
      }
    }
    return true;
  }

  void solve_asc() {
    DEB("=== solve_asc ===\n");
    vector<vector<ll>> pref(SZ(asc));

    REP(i, SZ(asc)) {
      pref[i].resize(m + 1, 0);
      FOR(j, 1, m) { pref[i][j] = pref[i][j - 1] + asc[i][j - 1]; }
    }

    sort(pref.begin(), pref.end(),
         [](const vector<ll> &v1, const vector<ll> &v2) {
           return v1.back() > v2.back();
         });

    REP(i, SZ(pref)) {
      DEB("pref i=" << i << ":   ");
      FOR(j, 0, m) { DEB(pref[i][j] << " "); }
      DEB("\n");
    }
    DEB("\n");

    vector<vector<pair<ll, int>>> max_pref(SZ(asc));
    FORD(i, SZ(asc) - 1, 0) {
      max_pref[i].resize(m + 1);
      FOR(j, 1, m) {
        int prev_i = i + 1;
        pair<ll, int> prev =
            (prev_i < SZ(asc)) ? max_pref[prev_i][j] : pair<ll, int>{-1LL, -1};
        max_pref[i][j] = max(prev, {pref[i][j], i});
      }
    }

    REP(i, SZ(max_pref)) {
      DEB("max_pref i=" << i << ":   ");
      FOR(j, 0, m) { DEB(max_pref[i][j] << " "); }
      DEB("\n");
    }
    DEB("\n");

    vector<vector<ll>> suffix(SZ(asc));

    REP(i, SZ(asc)) {
      suffix[i].resize(m + 1, 0);

      FORD(j, m - 1, 0) {
        ll value = pref[i][j + 1] - pref[i][j];
        suffix[i][j] = suffix[i][j + 1] + value;
      }
    }

    REP(i, SZ(suffix)) {
      DEB("suffix i=" << i << ":   ");
      FOR(j, 0, m) { DEB(suffix[i][j] << " "); }
      DEB("\n");
    }
    DEB("\n");

    vector<vector<ll>> min_suffix(SZ(asc));
    REP(i, SZ(asc)) {
      min_suffix[i].resize(m + 1, LLINF);
      REP(j, m + 1) {
        int prev_i = i - 1;
        ll prev = (prev_i >= 0) ? min_suffix[prev_i][j] : LLINF;
        min_suffix[i][j] = min(prev, suffix[i][j]);
      }
    }

    REP(i, SZ(min_suffix)) {
      DEB("min_suffix i=" << i << ":   ");
      FOR(j, 0, m) { DEB(min_suffix[i][j] << " "); }
      DEB("\n");
    }
    DEB("\n");

    best_asc.resize(1, 0);
    FOR(x, 1, (SZ(asc) * m)) {
      int i = x / m;
      int j = x % m;
      DEB("x=" << x << " i=" << i << " j=" << j << "\n");

      if (j == 0) {
        ll full_best = best_asc[(i - 1) * m] + pref[i - 1][m];
        DEB("full_best = " << full_best << "\n\n");
        best_asc.PB(full_best);
        continue;
      }

      ll cand_1_part = (i < SZ(asc)) ? max_pref[i][j].first : 0;
      ll cand_1_full = best_asc[i * m];
      ll cand_1 = cand_1_part + cand_1_full;
      DEB("cand_1: " << cand_1_part << " + " << cand_1_full << " = " << cand_1
                     << "\n");

      pair<ll, int> cand_2_partial = max_pref[0][j];
      ll cand_2 = 0;
      if (cand_2_partial.second < i) {
        ll cand_2_part = cand_2_partial.first;
        ll cand_2_full =
            best_asc[i * m] + pref[i][m] - pref[cand_2_partial.second][m];
        cand_2 = cand_2_part + cand_2_full;
        DEB("cand_2: " << cand_2_part << " + " << cand_2_full << " = " << cand_2
                       << "\n");
      }

      ll cand_3 = 0;
      if (i > 0) {
        cand_3 = cand_1_full + pref[i][m] - min_suffix[i - 1][j];
      }

      DEB("\n");
      best_asc.PB(max(cand_1, max(cand_2, cand_3)));
    }
  }

  void solve_desc() {
    priority_queue<tuple<ll, int, int>> q;

    REP(i, SZ(desc)) { q.push({desc[i][0], i, 0}); }

    best_desc.resize(1, 0);

    while (not q.empty()) {
      auto [value, i, j] = q.top();
      q.pop();
      best_desc.PB(best_desc.back() + value);
      int next_j = j + 1;
      if (next_j < m) {
        q.push({desc[i][next_j], i, next_j});
      }
    }
  }

  ll calculate_result() {
    ll best = 0;
    FOR(split, 0, k) {
      int from_asc = split;
      int from_desc = k - split;
      ll asc_result = 0;
      ll desc_result = 0;
      if (from_asc < SZ(best_asc)) {
        asc_result = best_asc[from_asc];
      }
      if (from_desc < SZ(best_desc)) {
        desc_result = best_desc[from_desc];
      }
      best = max(best, asc_result + desc_result);
    }
    return best;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int z = 1;
  // cin >> z;
  while (z--) {
    Case().solve();
  }
}