#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RFOR(i,p,n) for(int i=(p);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define gc getchar_unlocked
#define pc putchar_unlocked
#define pn putchar_unlocked('\n')
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef V<int> vi;
typedef V<ll> vll;
typedef const int ci;
typedef const ll cll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf = 2.1e9;
cll infll = 4.5e18;
int I(){
int z;
int c = gc();
while(c < '0' || c > '9') c = gc();
for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0';
return z;
}
ll LL(){
ll z;
int c = gc();
while(c < '0' || c > '9') c = gc();
for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0';
return z;
}
void O(ll a){
static int tab[20];
int it = 0;
do{
tab[it++] = int('0'+a%10);
} while(a /= 10);
while(it) pc(tab[--it]);
}
vll wypukle(ci n, ci m, C V<vll> &tab){
V<pli> sumy(n);
REP(i, n){
sumy[i].fi = 0;
REP(j, m) sumy[i].fi += tab[i][j];
sumy[i].se = i;
}
sort(rall(sumy));
V pref(n, vll(m+1));
REP(i, n) REP(j, m) pref[i][j+1] = pref[i][j]+tab[sumy[i].se][j];
vll ret(n*m+1, 0);
FOR(j, 0, m){
ll suma = 0;
ll mini = infll;
REP(i, n){
suma += sumy[i].fi;
chmin(mini, sumy[i].fi-pref[i][j]);
chmax(ret[i*m+j], suma-mini);
}
}
FOR(j, 0, m){
ll suma = 0;
REP(i, n) suma += sumy[i].fi;
ll maks = 0;
RFOR(i, n-1, 0){
suma -= sumy[i].fi;
chmax(maks, pref[i][j]);
chmax(ret[i*m+j], suma+maks);
}
}
return ret;
}
vll wklesle(ci n, ci m, C V<vll> &tab){
vll ret;
ret.reserve(n*m);
for(C vll &vec : tab) for(cll x : vec) ret.eb(x);
sort(rall(ret));
ret.insert(ret.begin(), 0);
FOR(i, 1, n*m) ret[i] += ret[i-1];
return ret;
}
void answer(){
ci n = I();
ci m = I();
ci k = I();
V<vll> rosnace, malejace;
rosnace.reserve(n);
malejace.reserve(n);
REP(i, n){
vll vec(m);
REP(j, m) vec[j] = LL();
bool czyros = true;
REP(j, m-1) czyros &= vec[j]<=vec[j+1];
if(czyros) rosnace.eb(vec);
else malejace.eb(vec);
}
vll a = wklesle(ssize(malejace), m, malejace);
vll b = wypukle(ssize(rosnace), m, rosnace);
ll wyn = 0;
FOR(i, 0, k) if(i < ssize(a) && k-i < ssize(b)) chmax(wyn, a[i]+b[k-i]);
O(wyn), pn;
}
int main(){
int tt = 1;
while(tt--) answer();
}
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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define RFOR(i,p,n) for(int i=(p);i>=(n);--i) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define ssize(x) int((x).size()) #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define C const #define gc getchar_unlocked #define pc putchar_unlocked #define pn putchar_unlocked('\n') using namespace std; typedef long long ll; typedef __int128_t lll; typedef V<int> vi; typedef V<ll> vll; typedef const int ci; typedef const ll cll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; void chmin(auto &a, auto b){a=min(a,b);} void chmax(auto &a, auto b){a=max(a,b);} ci inf = 2.1e9; cll infll = 4.5e18; int I(){ int z; int c = gc(); while(c < '0' || c > '9') c = gc(); for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0'; return z; } ll LL(){ ll z; int c = gc(); while(c < '0' || c > '9') c = gc(); for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0'; return z; } void O(ll a){ static int tab[20]; int it = 0; do{ tab[it++] = int('0'+a%10); } while(a /= 10); while(it) pc(tab[--it]); } vll wypukle(ci n, ci m, C V<vll> &tab){ V<pli> sumy(n); REP(i, n){ sumy[i].fi = 0; REP(j, m) sumy[i].fi += tab[i][j]; sumy[i].se = i; } sort(rall(sumy)); V pref(n, vll(m+1)); REP(i, n) REP(j, m) pref[i][j+1] = pref[i][j]+tab[sumy[i].se][j]; vll ret(n*m+1, 0); FOR(j, 0, m){ ll suma = 0; ll mini = infll; REP(i, n){ suma += sumy[i].fi; chmin(mini, sumy[i].fi-pref[i][j]); chmax(ret[i*m+j], suma-mini); } } FOR(j, 0, m){ ll suma = 0; REP(i, n) suma += sumy[i].fi; ll maks = 0; RFOR(i, n-1, 0){ suma -= sumy[i].fi; chmax(maks, pref[i][j]); chmax(ret[i*m+j], suma+maks); } } return ret; } vll wklesle(ci n, ci m, C V<vll> &tab){ vll ret; ret.reserve(n*m); for(C vll &vec : tab) for(cll x : vec) ret.eb(x); sort(rall(ret)); ret.insert(ret.begin(), 0); FOR(i, 1, n*m) ret[i] += ret[i-1]; return ret; } void answer(){ ci n = I(); ci m = I(); ci k = I(); V<vll> rosnace, malejace; rosnace.reserve(n); malejace.reserve(n); REP(i, n){ vll vec(m); REP(j, m) vec[j] = LL(); bool czyros = true; REP(j, m-1) czyros &= vec[j]<=vec[j+1]; if(czyros) rosnace.eb(vec); else malejace.eb(vec); } vll a = wklesle(ssize(malejace), m, malejace); vll b = wypukle(ssize(rosnace), m, rosnace); ll wyn = 0; FOR(i, 0, k) if(i < ssize(a) && k-i < ssize(b)) chmax(wyn, a[i]+b[k-i]); O(wyn), pn; } int main(){ int tt = 1; while(tt--) answer(); } |
English