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
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)

using namespace std;

typedef long long llint;

const int MAX = 300000;

vector<llint> A[MAX], G[MAX], S[MAX], X[MAX], Y[MAX];
bool J[MAX];
llint T[MAX], T2[MAX];
vector<int> P;
vector<int> Q;
int n, m, k, d, g;

struct {
  bool operator()(int a, int b) const { return T[a] > T[b]; }
} myLess;

struct {
  bool operator()(int a, int b) const { return G[a][P[a]] < G[b][P[b]]; }
} pqLess;

int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n >> m >> k;
	
	REP(i, n) {
		A[i].clear(); J[i]=false; llint prev_a = 0, a = 0;
		REP(j, m) {
			cin >> a;
			A[i].push_back(a);
			if (j > 0 && a > prev_a) J[i]=true;
			prev_a = a;
		}
	}

	g = 0; d = n-1; REP(i, n) if (J[i]) {
		G[g] = A[i]; g++;
	} else {
		G[d] = A[i]; d--;
	}
	d = n-g;
	
	// cerr << "g=" << g << " d=" << d << endl;

	P.resize(g);
	REP(i, g) { T[i] = 0; P[i]=i; }
	REP(i, g) REP(j, m) { T[i] += G[i][j]; }

	// cerr << "(1) T[]={"; REP(i, g) cerr << T[i] << " "; cerr << "}" << endl;

	// Sort G (unreversed stack of pancakes) by T, decreasing;
	std::sort(P.begin(), P.end(), myLess);
	REP(i, g) { T2[i] = T[P[i]]; S[i] = G[P[i]]; }
	REP(i, g) { T[i] = T2[i]; G[i] = S[i]; }
	
	// cerr << "(2) T[]={"; REP(i, g) cerr << T[i] << " "; cerr << "}" << endl;
	
	REP(i, g) S[i] = G[i];
	REP(i, g) REP(j, m) if (j>0) { S[i][j] += S[i][j-1]; }  // S[i][j] == sum(G[i][0..j])

	REP(i, g) { X[i] = Y[i] = S[i]; }
	if (g>1) REP(j, m) FORD(i, g-2, 0) X[i][j] = max(X[i][j], X[i+1][j]);
	REP(i, g) REP(j, m) { Y[i][j] = T[i] - S[i][j]; }
	if (g>1) REP(j, m) FOR(i, 0, g-2) Y[i+1][j] = min(Y[i][j], Y[i+1][j]);	

	P.resize(n); REP(i, n) { P[i] = 0; }	
	Q.resize(d); REP(i, d) { Q[i] = g+i; }
	priority_queue pq(Q.begin(), Q.end(), pqLess);	

	REP(i, g) { T2[i] = ((i>0) ? T2[i-1] : 0) + T[i]; }

	llint res = -1;	
	llint res_d = 0;
	llint res_g = 0;
	
	FOR(p, 0, k) {
		// what if we eat p pancakes from reversed stacks (and k-p from regular stacks)
		if (p > d*m) break;
		
		// handle the pancakes from reversed stacks
		if (p == 0) {
			res_d = 0;
		} else {
			// sanity check. should never happen (we check p > d*m earlier)
			if (pq.empty()) {
				//cerr << "??? p=" << p << endl;
				break;
			}			
			int ix = pq.top(); pq.pop();
			res_d += G[ix][P[ix]];
			if (++P[ix] < m) pq.push(ix);
		}

		if ((k-p) > g*m) {
			continue;
		}
		
		// handle the remaining (k-p) pancakes from regular stacks.
		int t = (k-p) / m;  // from t stacks, consume all m pancakes.
		int u = (k-p) % m;  // from one stack, consume 'u' pancakes.

		res_g = 0;		
		if (u == 0) {
			res_g = (t>0) ? T2[t-1] : 0;
		} else {		
			if (t <= g) {
				res_g = max(res_g, ((t>0) ? T2[t-1] : 0) + X[t][u-1]);
			}
			if (t+1 <= g) {
				res_g = max(res_g, T2[t] - Y[t][u-1]);
			}
			// res_g = max(res_g1, res_g2);
		}
		// cerr << "p=" << p << " t=" << t << " u=" << u << " res_g1=" << res_g1 << " res_g2=" << res_g2 << " res_d=" << res_d << endl; 
				
		if (res < res_d + res_g) res = res_d + res_g;
	}
			
	cout << res << endl;

	return 0;
}