#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MX = 4e5+9, INF = 1e18;
vector<int>pen, pref[MX], suf[MX];
vector<pair<int, vector<int>>>vect = {{INF, {}}};
int sums[MX];
bool cmp(const pair<int, vector<int>>& a, const pair<int, vector<int>>& b){
return a.first > b.first;
}
int get(){
int x = 0;
char c = getchar_unlocked();
while(c < '0' || c > '9') c = getchar_unlocked();
while(c >= '0' && c <= '9'){
x = x*10+c-'0';
c = getchar_unlocked();
}
return x;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n = get(), m = get(), k = get();
for(auto i = 1; i <= n; ++i){
vector<int>tmp = {0};
bool dec = 1;
int sum = 0;
for(auto i = 0; i < m; ++i){
int x = get();
tmp.push_back(x);
sum += x;
if(i && tmp[i+1] > tmp[i]) dec = 0;
}
if(dec) for(auto i : tmp) pen.push_back(i);
else vect.push_back({sum, tmp});
}
sort(pen.rbegin(), pen.rend());
while(pen.size() > k) pen.pop_back();
while(pen.size() <= k) pen.push_back(0);
reverse(pen.begin(), pen.end());
int all = 0;
for(auto i : pen) all += i;
int res = all;
sort(vect.begin(), vect.end(), cmp);
int N = vect.size()-1;
for(auto i = 1; i <= N; ++i) sums[i] = sums[i-1]+vect[i].first;
for(auto i = 0; i <= m; ++i) pref[N+1].push_back(0);
for(auto i = N; i; --i){
int sum = 0;
pref[i].push_back(0);
for(auto j = 1; j <= m; ++j){
sum += vect[i].second[j];
pref[i].push_back(max(pref[i+1][j], sum));
}
}
for(auto i = 0; i <= m; ++i) suf[0].push_back(INF);
suf[0].push_back(0);
for(auto i = 1; i <= N; ++i){
for(auto j = 0; j <= m; ++j) suf[i].push_back(INF);
suf[i].push_back(0);
int sum = 0;
for(auto j = m; j; --j){
sum += vect[i].second[j];
suf[i][j] = min(suf[i-1][j], sum);
}
}
for(auto p = 1; p <= min(k, m*N); ++p){
all -= pen[p];
int pos = p/m + (p%m ? 1 : 0);
int todel = pos*m-p;
int toadd = p-m*(pos-1);
res = max(res, all+sums[pos]-suf[pos][m-todel+1]);
res = max(res, all+sums[pos-1]+pref[pos][toadd]);
}
cout << res;
}
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 | #include <bits/stdc++.h> #define int long long using namespace std; const int MX = 4e5+9, INF = 1e18; vector<int>pen, pref[MX], suf[MX]; vector<pair<int, vector<int>>>vect = {{INF, {}}}; int sums[MX]; bool cmp(const pair<int, vector<int>>& a, const pair<int, vector<int>>& b){ return a.first > b.first; } int get(){ int x = 0; char c = getchar_unlocked(); while(c < '0' || c > '9') c = getchar_unlocked(); while(c >= '0' && c <= '9'){ x = x*10+c-'0'; c = getchar_unlocked(); } return x; } signed main(){ cin.tie(0)->sync_with_stdio(0); int n = get(), m = get(), k = get(); for(auto i = 1; i <= n; ++i){ vector<int>tmp = {0}; bool dec = 1; int sum = 0; for(auto i = 0; i < m; ++i){ int x = get(); tmp.push_back(x); sum += x; if(i && tmp[i+1] > tmp[i]) dec = 0; } if(dec) for(auto i : tmp) pen.push_back(i); else vect.push_back({sum, tmp}); } sort(pen.rbegin(), pen.rend()); while(pen.size() > k) pen.pop_back(); while(pen.size() <= k) pen.push_back(0); reverse(pen.begin(), pen.end()); int all = 0; for(auto i : pen) all += i; int res = all; sort(vect.begin(), vect.end(), cmp); int N = vect.size()-1; for(auto i = 1; i <= N; ++i) sums[i] = sums[i-1]+vect[i].first; for(auto i = 0; i <= m; ++i) pref[N+1].push_back(0); for(auto i = N; i; --i){ int sum = 0; pref[i].push_back(0); for(auto j = 1; j <= m; ++j){ sum += vect[i].second[j]; pref[i].push_back(max(pref[i+1][j], sum)); } } for(auto i = 0; i <= m; ++i) suf[0].push_back(INF); suf[0].push_back(0); for(auto i = 1; i <= N; ++i){ for(auto j = 0; j <= m; ++j) suf[i].push_back(INF); suf[i].push_back(0); int sum = 0; for(auto j = m; j; --j){ sum += vect[i].second[j]; suf[i][j] = min(suf[i-1][j], sum); } } for(auto p = 1; p <= min(k, m*N); ++p){ all -= pen[p]; int pos = p/m + (p%m ? 1 : 0); int todel = pos*m-p; int toadd = p-m*(pos-1); res = max(res, all+sums[pos]-suf[pos][m-todel+1]); res = max(res, all+sums[pos-1]+pref[pos][toadd]); } cout << res; } |
English