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
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
namespace debug {
template <class c> struct rge { c b, e; };
template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; }
template <class c> char spk(...);
template <class c> auto spk(c *a) -> decltype(cerr << *a, 0);
struct stream {
  ~stream() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof spk<c>(0) != 1, stream &>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof spk<c>(0) == 1, stream &>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class a, class b> stream &operator<<(pair<a, b> p) {
    return *this << "(" << p.first << ", " << p.second << ")";
  }
  template <class c> stream &operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; it++)
      *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
  stream &_dbg(const string &s, int i, int b) { return *this; }
  template <class c, class... cs>
  stream &_dbg(const string &s, int i, int b, c arg, cs... args) {
    if (i == (int)(s.size()))
      return (*this << ": " << arg);
    b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{') - (s[i] == ')') -
         (s[i] == ']') - (s[i] == '}');
    return (s[i] == ',' && b == 0)
               ? (*this << ": " << arg << "     ")._dbg(s, i + 1, b, args...)
               : (s[i] == ' ' ? *this : *this << s[i])
                     ._dbg(s, i + 1, b, arg, args...);
  }
};
} // namespace debug
#ifdef DEBUG
#define dout debug::stream()
#define dbg(...) ((dout << "line:" << __LINE__ << " >> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__))
#else
#define dout
#define dbg(...)
#endif
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define SZ(x) (int)(x).size()
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;

// increasing -> convex prefix sum -> check border
// decreasing -> concave prefix sum -> minkowsky sum
vvi I, D, suf, pref, T;
vi S;

vi max_hull_inc(int m) {
    int n = SZ(I);
    suf.resize(n+2, vi(m+1, 0));
    pref.resize(n+2, vi(m+1, 0));
    S.resize(n+2, 0);
    vector<pll> ind(n);
    rep(i, 0, n-1) {
        ind[i].st = accumulate(all(I[i]), 0ll);
        ind[i].nd = i;
    }
    sort(all(ind), [](const pll& a, const pll& b) {
        return a.st == b.st ? a.nd < b.nd : a.st > b.st;
    });
    T.resize(n+1);
    T[0].resize(m+1, 0);
    rep(i, 1, n) {
        T[i].resize(m+1);
        T[i][0] = 0;
        rep(j, 0, m-1) T[i][j+1] = T[i][j] + I[ind[i-1].nd][j];
    }
    rep(j, 0, m) pref[0][j] = 1e18l;
    rep(i, 1, n) {
        rep(j, 0, m) {
            pref[i][j] = min(T[i].back() - T[i][j], pref[i-1][j]);
        }
    }
    per(i, 1, n) {
        rep(j, 0, m-1) {
            suf[i][j] = max(T[i][j], suf[i+1][j]);
        }
    }
    rep(i, 1, n) S[i] = T[i].back() + S[i-1];
    dbg(T, pref, suf, S);
    vi hull(n*m+1);
    rep(k, 0, n * m) {
        ll a = S[k / m] + suf[k/m+1][k%m];
        ll b = 0;
        if (k/m < n) {
            b = S[k/m+1] - pref[k/m+1][k%m];
        }
        hull[k] = max({a, b, 0ll});
    }
    
    return hull;
}

vi max_hull_dec(int m) {
    priority_queue<pair<ll,int>> Q;
    int n = SZ(D);
    vi hull(n*m+1, 0);
    vi ind(n, 1);
    rep(i, 0, n-1) Q.emplace(D[i][0], i);
    rep(k, 1, n*m) {
        auto [val, i] = Q.top();
        Q.pop();
        hull[k] = hull[k-1] + val;
        if (ind[i] < m) {
            Q.emplace(D[i][ind[i]], i);
            ind[i]++;
        }
    }
    return hull;
}



int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int n, m, k;
    cin >> n >> m >> k;
    vi A(m);
    rep(i, 1, n) {
        rep(j, 0, m-1) cin >> A[j];
        bool is_inc = true;
        rep(j, 0, m-2) is_inc &= A[j] <= A[j+1];
        if (is_inc) I.push_back(A);
        else D.push_back(A);
    }
    dbg(I, D);
    vi L = max_hull_inc(m);
    dbg(L);
    vi R = max_hull_dec(m);
    dbg(R);
    ll ans = 0;
    rep(i, 0, min(SZ(L)-1, k)) {
        if (k-i < 0 || k-i >= SZ(R)) continue;
        ans = max(ans, L[i] + R[k-i]);
    }
    cout << ans << '\n';
    return 0;
}