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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <queue>

#include <numeric>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>
#define FF          pair<float,float>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VVVI        VC<VVI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>
#define VFF         VC<FF>
#define VVD         VC<VD>
#define VVVD        VC<VVD>
#define VULL         VC<ULL>
#define VLL         VC<LL>
#define VVLL         VC<VLL>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

VVLL p;
VLL Tdec;
VLL Tinc;
int n,m,k;

void read_data(){
    cin >> n >> m >> k;
    p = VVLL(n,VLL(m));
    REP(i,n) REP(j,m) 
        cin >> p[i][j];
    Tdec = VLL(n*m+1,0);
    Tinc = VLL(n*m+1,0);
}

void solve_decreasing(){
    VLL sorted;
    sorted.reserve(n*m);
    for(int i=0; i < SIZE(p);)
        if (SIZE(p[i]) == 1 || p[i].back() <= p[i][0]){
            sorted.insert(sorted.end(),ALL(p[i]));
            p[i].swap(p.back());
            p.pop_back();
            n--;
        } else
            i++;

    sort(ALL(sorted));
    reverse(ALL(sorted));
    REP(i,SIZE(sorted))
        Tdec[i+1] = Tdec[i] + sorted[i];
}

void solve_increasing(){
    // computing sums and sorting by them
    VC<pair<LL,int>> order(n);
    REP(i,n) order[i] = MP(std::accumulate(ALL(p[i]),0LL),i);
    sort(ALL(order));
    reverse(ALL(order));
    VVLL p2(n);
    REP(i,n)
        p2[i].swap(p[order[i].Y]);
    p.swap(p2);

    VLL fullStacks(n+1,0LL);
    REP(i,n) 
	    fullStacks[i+1] = fullStacks[i] + order[i].X;
    VLL topkm(n,0LL);
    VLL prefMax(n+1,0LL);
    VLL sufMax(n,0LL);
    REP(km,m){
       prefMax[1] = topkm[0]-order[0].X;
       FOR(i,2,n)
	       prefMax[i] = max(prefMax[i-1],topkm[i-1]-order[i-1].X);
       sufMax[n-1] = topkm[n-1];
       for(int i=n-2; i>=0; i--)
	       sufMax[i] = max(sufMax[i+1],topkm[i]);
       //cout << "km = " << km << endl;
       //cout << "pref = "; REP(i,n) cout << prefMax[i] << " "; cout << endl;
       //cout << "suf = "; REP(i,n) cout << sufMax[i] << " "; cout << endl;
       REP(kd,n){
	  LL val = 0;
          if (kd>0)
		  val = max(val,fullStacks[kd+1]+prefMax[kd]);
	  val = max(val,fullStacks[kd]+sufMax[kd]);
	  Tinc[m*kd+km] = val; 
       }
       REP(i,n)
	       topkm[i] += p[i][km];
    }
    Tinc[m*n] = fullStacks[n];	
}

long long solve(){
    LL M = 0;
    REP(i,k+1)
        M = max(M,Tinc[i]+Tdec[k-i]);
    return M;
}

int main(int argc, char *argv[]){
    read_data();
    solve_decreasing();
    if (n > 0)
    	solve_increasing();
    solve();
    cout << solve() << endl;
    return 0;
}