#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define VI vector<int>
#define PII pair<int,int>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define lint long long int
#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; }
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; }
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}
#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue
#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
using namespace std;
vector<lint> v[300005];
vector<lint> dobre;
VI zle;
lint zle_wynik[300005];
lint wez_dobre(int ile) {
ile = min(ile, (int) dobre.size());
if (ile == 0) return 0;
return dobre[ile-1];
}
lint wez_zle(int ile, int m) {
ile = min(ile, m * ((int) zle.size()));
if (ile == 0) return 0;
return zle_wynik[ile-1];
}
HEAP<pair<lint, int> > heap[300005];
HEAP<lint> hop[300005];
int zabrane[300005];
pair<lint, int> get(int r) {
pair<lint, int> ja = heap[r].top();
if (zabrane[ja.nd] != 1) {
return ja;
}
heap[r].pop();
return get(r);
}
int main() {
make3(n, m, k);
FOR(i,0,n) {
FOR(j,0,m) {
lint a;
scanf("%lld", &a);
v[i].pb(a);
}
if (v[i].back() <= v[i][0]) {
FOR(j,0,m) dobre.pb(v[i][j]);
} else {
zle.pb(i);
}
}
sort(dobre.begin(), dobre.end(), greater<lint>());
FOR(j,1,dobre.size()) dobre[j]+=dobre[j-1];
if (zle.size() == 0) {
printf("%lld\n", dobre[k-1]);
return 0;
}
// debugv(dobre);
FOR(i,0,zle.size()) {
int ja = zle[i];
FOR(j,1,m) {
v[ja][j] += v[ja][j-1];
}
}
int S = zle.size();
// debug(S);
FOR(r,0,m)
FOR(j,0,zle.size())
heap[r].push({v[zle[j]][r], zle[j]});
lint sofar = 0;
FOR(i,0,S) {
FOR(r,0,m) {
int place = i*m+r;
pair<lint, int> ja = get(r);
zle_wynik[place] = sofar + ja.st;
// druga opcja
if (i > 0) {
lint pos = sofar + get(m-1).st + hop[r].top();
zle_wynik[place] = max(zle_wynik[place], pos);
}
//cout << "USTAWIAM " << place << " na " << zle_wynik[place] << endl;
}
pair<lint, int> ja = get(m-1);
sofar += ja.st;
zabrane[ja.nd] = 1;
FOR(r,0,m) hop[r].push(-ja.st+v[ja.nd][r]);
}
lint best = 0;
FOR(j,0,k+1) {
best = max(best, wez_dobre(j) + wez_zle(k-j, m));
}
printf("%lld\n", best);
}
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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define VI vector<int> #define PII pair<int,int> #define st first #define nd second #define mp make_pair #define pb push_back #define lint long long int #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); using namespace std; vector<lint> v[300005]; vector<lint> dobre; VI zle; lint zle_wynik[300005]; lint wez_dobre(int ile) { ile = min(ile, (int) dobre.size()); if (ile == 0) return 0; return dobre[ile-1]; } lint wez_zle(int ile, int m) { ile = min(ile, m * ((int) zle.size())); if (ile == 0) return 0; return zle_wynik[ile-1]; } HEAP<pair<lint, int> > heap[300005]; HEAP<lint> hop[300005]; int zabrane[300005]; pair<lint, int> get(int r) { pair<lint, int> ja = heap[r].top(); if (zabrane[ja.nd] != 1) { return ja; } heap[r].pop(); return get(r); } int main() { make3(n, m, k); FOR(i,0,n) { FOR(j,0,m) { lint a; scanf("%lld", &a); v[i].pb(a); } if (v[i].back() <= v[i][0]) { FOR(j,0,m) dobre.pb(v[i][j]); } else { zle.pb(i); } } sort(dobre.begin(), dobre.end(), greater<lint>()); FOR(j,1,dobre.size()) dobre[j]+=dobre[j-1]; if (zle.size() == 0) { printf("%lld\n", dobre[k-1]); return 0; } // debugv(dobre); FOR(i,0,zle.size()) { int ja = zle[i]; FOR(j,1,m) { v[ja][j] += v[ja][j-1]; } } int S = zle.size(); // debug(S); FOR(r,0,m) FOR(j,0,zle.size()) heap[r].push({v[zle[j]][r], zle[j]}); lint sofar = 0; FOR(i,0,S) { FOR(r,0,m) { int place = i*m+r; pair<lint, int> ja = get(r); zle_wynik[place] = sofar + ja.st; // druga opcja if (i > 0) { lint pos = sofar + get(m-1).st + hop[r].top(); zle_wynik[place] = max(zle_wynik[place], pos); } //cout << "USTAWIAM " << place << " na " << zle_wynik[place] << endl; } pair<lint, int> ja = get(m-1); sofar += ja.st; zabrane[ja.nd] = 1; FOR(r,0,m) hop[r].push(-ja.st+v[ja.nd][r]); } lint best = 0; FOR(j,0,k+1) { best = max(best, wez_dobre(j) + wez_zle(k-j, m)); } printf("%lld\n", best); } |
English