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
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define vec vector
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

const int inf=1e18;

void slv(){
	int n,m,k;
	cin>>n>>m>>k;

	vec<vi> a(n,vi(m));
	rep(i,n){
		rep(j,m){
			cin>>a[i][j];
		}
	}

	auto get_sorted=[&](vec<vi> a){
		int n=sz(a);	
		vi dp(n*m+1);
		priority_queue<pair<int,pii>> pq;
		rep(i,n){
			pq.push({a[i].back(),{i,m-1}});
		}

		int s=1;
		while(sz(pq)){
			auto top=pq.top();
			pq.pop();
			int val=top.fi;
			auto [i,j]=top.se;
			// print(val,s,i,j);
			dp[s]=dp[s-1]+val;
			s++;
			j--;
			if(j>=0){
				// print("e",i,j);
				pq.push({a[i][j],pii{i,j}});
			}
		}

		rng(i,1,sz(dp)) dp[i]=max(dp[i],dp[i-1]);
		
		return dp;
	};

	auto get_rev_sorted=[&](vec<vi> a){
		int n=sz(a);
		
		rep(i,n){
			a[i].pb(0);
			reverse(all(a[i]));
			rng(j,1,m+1){
				a[i][j]+=a[i][j-1];
			}
		}

		vi ord(n);
		rep(i,n) ord[i]=i;
		sort(all(ord),[&](int l,int r){
			return a[l].back()>a[r].back();
		});
		
		vi ps_sum(n);
		rep(i,n){
			if(i){
				ps_sum[i]+=ps_sum[i-1];
			}
			ps_sum[i]+=a[ord[i]].back();
		}

		vec<vi> max_rem_ps(n,vi(m+1,-inf));
		rep(i,n){
			rep(j,m+1){
				if(i){
					max_rem_ps[i][j]=max(max_rem_ps[i][j],max_rem_ps[i-1][j]);
				}
				max_rem_ps[i][j]=max(max_rem_ps[i][j],-a[ord[i]].back()+a[ord[i]][j]);
			}
		}

		vec<vi> max_rem_sf(n,vi(m+1,-inf));
		per(i,n){
			rep(j,m+1){
				if(i+1<n){
					max_rem_sf[i][j]=max(max_rem_sf[i][j],max_rem_sf[i+1][j]);
				}
				max_rem_sf[i][j]=max(max_rem_sf[i][j],a[ord[i]][j]);
			}
		}

		vi dp(n*m+1);
		rep(s,n*m+1){
			int rem=s%m;
			int c=s/m;
			int total=0;
			total=(c==0?0:ps_sum[c-1]);
			// rep(i,c){
			// 	total+=a[ord[i]].back();
			// }
			int val=0;
			if(c<n){
				val=max_rem_sf[c][rem];
			}
			// rng(i,c,n){
			// 	val=max(val,a[ord[i]][rem]);
			// }
			dp[s]=max(dp[s],total+val);
			if(c<n and c){
				int now=total;
				now+=max_rem_ps[c-1][rem];
				now+=a[ord[c]].back();
				// int now1=0;
				// rep(i,c){
				// 	now=max(now,total-a[ord[i]].back()+a[ord[i]][rem]);
				// }
				// print(now,now1,c);
				dp[s]=max(dp[s],now);
			}
		}

		rng(i,1,sz(dp)) dp[i]=max(dp[i],dp[i-1]);

		return dp;
	};

	vec<vi> lhs,rhs;
	rep(i,n){
		reverse(all(a[i]));
		bool is_sorted=1;
		rep(j,m-1){
			if(a[i][j]>a[i][j+1]){
				is_sorted=0;
			}
		}
		if(is_sorted){
			lhs.pb(a[i]);
		}else{
			rhs.pb(a[i]);
		}
	}

	auto dp_l=get_sorted(lhs);
	auto dp_r=get_rev_sorted(rhs);

	// vi dp(n*m+1);
	// rep(i,sz(dp_l)) rep(j,sz(dp_r)) dp[i+j]=max(dp[i+j],dp_l[i]+dp_r[j]);

	// rep(i,sz(dp)) cout<<dp[i]<<" ";
	// cout<<"\n";

	int ans=0;
	rep(i,k+1){
		if(i<=sz(dp_l) and k-i<sz(dp_r)){
			ans=max(ans,dp_l[i]+dp_r[k-i]);
		}
	}
	cout<<ans<<"\n";
}

signed main(){
	ios::sync_with_stdio(0),cin.tie(0);

	int t;
	t=1;
	// cin>>t;

	rep(cs,t){
		slv();
	}
}