First, we need to know that the order of selecting rows and columns is irrelevant.
Two arrays, row [I] and Col [J], are used to indicate the maximum value that can be obtained by selecting only row I and the maximum value that can be obtained by selecting only column J.
This is maintained using a priority queue. After a row (column) is not selected, the sum of the row (column) minus the corresponding Np (MP) is added to the queue.
If the number of rows selected for enumeration is I, the number of columns selected is k-I, and ANS = row [I] + Col [k-I]-(k-I) * I * P;
Since the sequence is irrelevant, we can see that the I-I row is selected first, I * P is subtracted each time a column is selected, and K-I column is selected, that is, minus (k-I) * I * P
// # Pragma comment (linker, "/Stack: 102400000,102400000 ") // head # include <cstdio> # include <cstring> # include <vector> # include <iostream> # include <algorithm> # include <queue> # include <string> # include <set> # include <stack> # include <map> # include <cmath> # include <cstdlib> using namespace STD; // loop # define Fe (I, a, B) for (INT I = (a); I <= (B); ++ I) # define fed (I, b, A) for (INT I = (B); I> = (a); -- I) # define re P (I, n) for (INT I = 0; I <(n); ++ I) # define CLR (A, value) memset (A, value, sizeof (A) // STL # define Pb push_back // input # define RI (n) scanf ("% d", & N) # define RII (n, m) scanf ("% d", & N, & M) # define riii (n, m, k) scanf ("% d", & N, & M, & K) # define RS (s) scanf ("% s", S) # define ff (I, a, B) for (INT I = (); I <(B); ++ I) # define FD (I, B, A) for (INT I = (B)-1; I> = (); -- I) # define CPY (a, B) memcpy (a, B, sizeo F (a) # define FC (it, c) for (_ typeof (c). Begin () it = (c). Begin (); it! = (C ). end (); It ++) # define eq (A, B) (FABS (a)-(B) <= 1e-10) # define all (c) (c ). begin (), (c ). end () # define SZ (V) (INT) v. size () # define RIV (n, m, K, P) scanf ("% d", & N, & M, & K, & P) # define RV (n, m, k, p, q) scanf ("% d", & N, & M, & K, & P, & Q) # define wi (n) printf ("% d \ n", n) # define WS (s) printf ("% s \ n", S) # define sqr (x) x * xtypedef vector <int> VI; typedef unsigned long ull; typedef long Ll; const int INF = 0x3f3f3f; const int maxn = 1010; const double EPS = 1e-10; const ll mod = 1e9 + 7; int IPT [maxn] [maxn]; ll row [maxn * maxn], Col [maxn * maxn]; ll rtol [maxn], ctol [maxn]; int main () {int n, m, K, P; while (~ Riv (n, m, k, p) {priority_queue <ll> r, C; int Radd = 0, CADD = 0; CLR (rtol, 0), CLR (ctol, 0); Fe (I, 1, n) Fe (J, 1, m) {RI (IPT [I] [J]); rtol [I] + = IPT [I] [J]; ctol [J] + = IPT [I] [J];} Fe (I, 1, n) R. push (rtol [I]); Fe (J, 1, m) C. push (ctol [J]); row [0] = 0, Col [0] = 0; Fe (I, 1, k) {ll x = R. top (), Y = C. top (); R. pop (), C. pop (); R. push (X-M * P); C. push (Y-N * P); row [I] = row [I-1] + X; Col [I] = Col [I-1] + Y ;} // fe (I, 0, k) // cout <row [I] + Col [k-I] <Endl; ll ans =-1e18; Fe (I, 0, k) ans = max (ANS, row [I] + Col [k-I]-(LL) I * (k-I) * P ); cout <ans <Endl;} return 0;}/* 2 2 2 4 21 2 3 102 3 5 22 2 22 2 2 */