#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Stos {
vector<ll> pref;
ll suma;
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<ll> pula_malejace;
vector<Stos> stosy_rosnace;
for(int i = 0; i < n; i++){
vector<ll> temp(m);
for(int j = 0; j < m; j++) cin >> temp[j];
if(m > 1 && temp[0] < temp[m - 1]){
ll s = 0;
vector<ll> p(m + 1, 0);
for(int j = 0; j < m; j++){
s += temp[j];
p[j + 1] = s;
}
stosy_rosnace.push_back({p, s});
}else{
for (ll x : temp) pula_malejace.push_back(x);
}
}
sort(stosy_rosnace.begin(), stosy_rosnace.end(), [](const Stos& a, const Stos& b) {
return a.suma > b.suma;
});
sort(pula_malejace.rbegin(), pula_malejace.rend());
vector<ll> pref_pula(pula_malejace.size()+1, 0);
for (int i = 0; i < (int)pula_malejace.size(); i++) pref_pula[i + 1]= pref_pula[i] + pula_malejace[i];
ll max_wynik = 0;
ll sumPelne = 0;
for(int x = 0; x <= (int)stosy_rosnace.size(); x++){
ll left = (ll)k - (ll)x * m;
if(left < 0) break;
ll reszta = 0;
int i_min = max(0LL , left-(ll)pula_malejace.size());
int i_max = min((ll)m, left);
if (i_min <= i_max){
if(x < (int)stosy_rosnace.size()){
reszta = max(stosy_rosnace[x].pref[i_min] + pref_pula[left - i_min],
stosy_rosnace[x].pref[i_max] + pref_pula[left - i_max]);
}else{
if(left <= (ll)pula_malejace.size()){
reszta = pref_pula[left];
} else continue;
}
}else{
if(x <(int)stosy_rosnace.size()) sumPelne += stosy_rosnace[x].suma;
continue;
}
max_wynik = max(max_wynik, sumPelne + reszta);
if(x < (int)stosy_rosnace.size()) sumPelne += stosy_rosnace[x].suma;
}
cout << max_wynik << endl;
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; struct Stos { vector<ll> pref; ll suma; }; int main() { ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0); int n, m, k; cin >> n >> m >> k; vector<ll> pula_malejace; vector<Stos> stosy_rosnace; for(int i = 0; i < n; i++){ vector<ll> temp(m); for(int j = 0; j < m; j++) cin >> temp[j]; if(m > 1 && temp[0] < temp[m - 1]){ ll s = 0; vector<ll> p(m + 1, 0); for(int j = 0; j < m; j++){ s += temp[j]; p[j + 1] = s; } stosy_rosnace.push_back({p, s}); }else{ for (ll x : temp) pula_malejace.push_back(x); } } sort(stosy_rosnace.begin(), stosy_rosnace.end(), [](const Stos& a, const Stos& b) { return a.suma > b.suma; }); sort(pula_malejace.rbegin(), pula_malejace.rend()); vector<ll> pref_pula(pula_malejace.size()+1, 0); for (int i = 0; i < (int)pula_malejace.size(); i++) pref_pula[i + 1]= pref_pula[i] + pula_malejace[i]; ll max_wynik = 0; ll sumPelne = 0; for(int x = 0; x <= (int)stosy_rosnace.size(); x++){ ll left = (ll)k - (ll)x * m; if(left < 0) break; ll reszta = 0; int i_min = max(0LL , left-(ll)pula_malejace.size()); int i_max = min((ll)m, left); if (i_min <= i_max){ if(x < (int)stosy_rosnace.size()){ reszta = max(stosy_rosnace[x].pref[i_min] + pref_pula[left - i_min], stosy_rosnace[x].pref[i_max] + pref_pula[left - i_max]); }else{ if(left <= (ll)pula_malejace.size()){ reszta = pref_pula[left]; } else continue; } }else{ if(x <(int)stosy_rosnace.size()) sumPelne += stosy_rosnace[x].suma; continue; } max_wynik = max(max_wynik, sumPelne + reszta); if(x < (int)stosy_rosnace.size()) sumPelne += stosy_rosnace[x].suma; } cout << max_wynik << endl; return 0; } |
English