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
#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)
#define FORL(i,p,k) for(long long i=(p); i<=(k); ++i)
#define RFOR(i,p,k) for(int i=(p); i>=(k); --i)
#define REP(i,k) FOR(i,0,(k)-1)
#define FORC(init,cond,inc) for(init; cond; inc)
#define FORPWK(pw,k,cond) for(int pw = 1, k = 1; cond; pw *= 2, ++k)
#define all(x) (x).begin(), (x).end()
#define siz(x) int((x).size())
#define f first
#define s second
#define v vector
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define C const
using namespace std;
typedef long long ll;
typedef v<int> vi;
typedef v<long long> vl;
typedef C int ci;
typedef C ll cll;
typedef pair<int, int> pii;
template<typename T>
using V = vector<T>;

inline void solve(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, m, k;
	cin >> n >> m >> k;
	v<vl> ros;
	vl mal;

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

		if(a[0] >= a[m - 1]){
			REP(j, m){
				mal.pb(a[j]);
				}
			}
		
		else{
			vl p(m + 1, 0);
			REP(j, m){
				p[j + 1] = p[j] + a[j];
				}
			ros.pb(p);
			}
		}

	sort(all(mal), greater<ll>());
	if(siz(mal) > k){
		mal.resize(k);
		}

	if(siz(mal) > 0){
		FOR(i, 1, siz(mal) - 1){
			mal[i] += mal[i - 1];
			}
		}

	if(siz(ros) == 0){
		cout << mal[k - 1] << "\n";
		return;
		}

	sort(all(ros), [&](C vl& a, C vl& b){ return a[m] > b[m]; });

	int r = siz(ros), lim = min(k, r * m);
	vl dp(lim + 1, -(1LL << 60)), sum(r + 1, 0), lew(r + 1, -(1LL << 60)), pra(r + 2, -(1LL << 60));

	dp[0] = 0;
	FOR(i, 1, r){
		sum[i] = sum[i - 1] + ros[i - 1][m];
		if(i * m <= lim){
			dp[i * m] = sum[i];
			}
		}

	FOR(res, 1, m - 1){
		lew[0] = -(1LL << 60);
		FOR(i, 1, r){
			ll x = ros[i - 1][res] - ros[i - 1][m];
			if(lew[i - 1] > x){
				lew[i] = lew[i - 1];
				}
			else{
				lew[i] = x;
				}
		}

		pra[r + 1] = -(1LL << 60);
		RFOR(i, r, 1){
			ll x = ros[i - 1][res];
			if(pra[i + 1] > x){
				pra[i] = pra[i + 1];
				}
				
			else{
				pra[i] = x;
				}
			}

		for(int pel=0; ; ++pel){
			int ile = pel * m + res;
			if(ile > lim){
				break;
				}
				
			if(pel == 0){
				dp[ile] = pra[1];
				}
			
			else if(pel >= r){
				dp[ile] = -(1LL << 60);
				}
			
			else{
				ll naj = -(1LL << 60);
				{
					ll x = sum[pel] + pra[pel + 1];
					if(x > naj){
						naj = x;
						}
				}
				{
					ll x = sum[pel + 1] + lew[pel];
					if(x > naj){
						naj = x;
						}
				}
				dp[ile] = naj;
			}
		}
	}

	ll wyn = 0, g = min(k, siz(mal));

	FOR(i, 0, g){
		int j = k - i;
		if(j < 0){
		}
		
		else if(j > lim){
		}
		
		else if(i == 0){
			if(dp[j] > wyn){ 
				wyn = dp[j];
				}
			}
			
		else{
			ll x = dp[j] + mal[i - 1];
			if(x > wyn){
				wyn = x;
				}
			}
		}

	cout << wyn << "\n";
}

signed main(){
	int tt = 1;
	while(tt--) solve();
}