#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXNM = 3e5 + 7;
struct inSeq{
vector<ll> pref;
ll sum;
};
ll n, m, k, x;
vector<inSeq> prefIn;//sumy prefiksowe ciagow rosnacych
vector<ll> prefSum;//sumy prefiksowe sum ciagow rosnacych
vector<ll> pref;//zlaczone malejace i posrtowane(sumy z tego)
void preprocess(){
cin >> n >> m >> k;
pref.push_back(0);
for(int i = 0; i < n; i++){
vector<ll> seq(m);
for(int j = 0; j < m; j++) cin >> seq[j];
if(seq[0] >= seq[m-1]){//ciag malejacy
for(int j = 0; j < m; j++) pref.push_back(seq[j]);
}
else{//ciag rosnacy
inSeq tmp;
tmp.pref.assign(m+1, 0);
for(int j = 0; j < m; j++) tmp.pref[j+1] = tmp.pref[j] + seq[j];
tmp.sum = tmp.pref[m];
prefIn.push_back(tmp);
}
}
//ciagi malejace - merge
sort(pref.begin()+1, pref.end(), greater<ll>());
for(int i = 1; i < pref.size(); i++) pref[i] += pref[i-1];
//ciagi rosnace - sort
sort(prefIn.begin(), prefIn.end(), [](const inSeq & i1, const inSeq & i2){
return i1.sum > i2.sum;
});
prefSum.assign(prefIn.size() + 1, 0);
for(int i = 0; i < prefIn.size(); i++) prefSum[i + 1] = prefSum[i] + prefIn[i].sum;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
preprocess();
ll ans = 0;
x = prefIn.size();//liczba ciagow rosnacych
//bierzemy i pelnych ciagow rosnaych - reszte dopychamy malejacymi
for(int i = 0; i <= x; i++){
ll left = k - (ll)i * m;
if(left >= 0 && left < (ll)pref.size())
ans = max(ans, prefSum[i] + pref[left]);
}
//mamy dokladnie jeden nie pelen ciag rosnacy
vector<vector<ll>> maxi1(m, vector<ll>(x+1, 0));//maks z wziecia t elementow z ciagu ktorego suma jest poza najlepszymi i
vector<vector<ll>> maxi2(m, vector<ll>(x+1, 0));//maks z wziecia t elementow z ciagu ktorego suma jest w najlepszych i
for(int t = 1; t < m; t++){
ll maxi = -1e18;
for(int i = x - 1; i >= 0; i--){
maxi = max(maxi, prefIn[i].pref[t]);
maxi1[t][i] = maxi;
}
maxi = -1e18;
for(int i = 0; i < x; i++){
maxi = max(maxi, prefIn[i].pref[t] - prefIn[i].sum);
maxi2[t][i] = maxi;
}
}
for(int i = 0; i <= x; i++){//i pelnych stosow, t resztki z i+1ego
for(int t = 1; t < m; t++){
ll left = k - (ll)i * m - t;
if(left < 0 || left >= pref.size()) continue;
if(i < x)//niepelny stos bierzemy z odrzuconych
ans = max(ans, prefSum[i] + maxi1[t][i] + pref[left]);
if(i + 1 <= x)
ans = max(ans, prefSum[i+1] + maxi2[t][i] + pref[left]);
}
}
cout << ans;
}
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 | #include <bits/stdc++.h> using namespace std; #define ll long long const int MAXNM = 3e5 + 7; struct inSeq{ vector<ll> pref; ll sum; }; ll n, m, k, x; vector<inSeq> prefIn;//sumy prefiksowe ciagow rosnacych vector<ll> prefSum;//sumy prefiksowe sum ciagow rosnacych vector<ll> pref;//zlaczone malejace i posrtowane(sumy z tego) void preprocess(){ cin >> n >> m >> k; pref.push_back(0); for(int i = 0; i < n; i++){ vector<ll> seq(m); for(int j = 0; j < m; j++) cin >> seq[j]; if(seq[0] >= seq[m-1]){//ciag malejacy for(int j = 0; j < m; j++) pref.push_back(seq[j]); } else{//ciag rosnacy inSeq tmp; tmp.pref.assign(m+1, 0); for(int j = 0; j < m; j++) tmp.pref[j+1] = tmp.pref[j] + seq[j]; tmp.sum = tmp.pref[m]; prefIn.push_back(tmp); } } //ciagi malejace - merge sort(pref.begin()+1, pref.end(), greater<ll>()); for(int i = 1; i < pref.size(); i++) pref[i] += pref[i-1]; //ciagi rosnace - sort sort(prefIn.begin(), prefIn.end(), [](const inSeq & i1, const inSeq & i2){ return i1.sum > i2.sum; }); prefSum.assign(prefIn.size() + 1, 0); for(int i = 0; i < prefIn.size(); i++) prefSum[i + 1] = prefSum[i] + prefIn[i].sum; } int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); preprocess(); ll ans = 0; x = prefIn.size();//liczba ciagow rosnacych //bierzemy i pelnych ciagow rosnaych - reszte dopychamy malejacymi for(int i = 0; i <= x; i++){ ll left = k - (ll)i * m; if(left >= 0 && left < (ll)pref.size()) ans = max(ans, prefSum[i] + pref[left]); } //mamy dokladnie jeden nie pelen ciag rosnacy vector<vector<ll>> maxi1(m, vector<ll>(x+1, 0));//maks z wziecia t elementow z ciagu ktorego suma jest poza najlepszymi i vector<vector<ll>> maxi2(m, vector<ll>(x+1, 0));//maks z wziecia t elementow z ciagu ktorego suma jest w najlepszych i for(int t = 1; t < m; t++){ ll maxi = -1e18; for(int i = x - 1; i >= 0; i--){ maxi = max(maxi, prefIn[i].pref[t]); maxi1[t][i] = maxi; } maxi = -1e18; for(int i = 0; i < x; i++){ maxi = max(maxi, prefIn[i].pref[t] - prefIn[i].sum); maxi2[t][i] = maxi; } } for(int i = 0; i <= x; i++){//i pelnych stosow, t resztki z i+1ego for(int t = 1; t < m; t++){ ll left = k - (ll)i * m - t; if(left < 0 || left >= pref.size()) continue; if(i < x)//niepelny stos bierzemy z odrzuconych ans = max(ans, prefSum[i] + maxi1[t][i] + pref[left]); if(i + 1 <= x) ans = max(ans, prefSum[i+1] + maxi2[t][i] + pref[left]); } } cout << ans; } |
English