DZY Loves Modification
As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more precise, he decided to modify the matrix with exactly k operations.
Each modification is one of the following:
- Pick some row of the matrix and decrease each element of the row by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing.
- Pick some column of the matrix and decrease each element of the column by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing.
DZY wants to know: what is the largest total value of pleasure he could get after performing exactly k modifications? Please, help him to calculate this value.
Input
The first line contains four space-separated integers n, m, k and p (1 ≤ n, m ≤ 103; 1 ≤ k ≤ 106; 1 ≤ p ≤ 100).
Then n lines follow. Each of them contains m integers representing aij (1 ≤ aij ≤ 103) — the elements of the current row of the matrix.
Output
Output a single integer — the maximum possible total pleasure value DZY could get.
Examples
2 2 2 2
1 3
2 4
11
2 2 5 2
1 3
2 4
11
Note
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:
1 1
0 0
For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:
-3 -3
-2 -2
题意:
给定n行m列的矩阵 k次操作,一个常数p,ans = 0;对于每次操作,可以任选一行或一列, 则ans += 这行(列)的数字和,然后这行(列)上的每个数字都-=p
1 //2016.8.19 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<algorithm> 6 #include<queue> 7 #define ll __int64 8 #define inf 999999999 9 10 using namespace std; 11 12 ll rsum[1005], csum[1005], rres[1000005], cres[1000005]; 13 priority_queue<ll> pq1; 14 priority_queue<ll> pq2; 15 16 int main() 17 { 18 ll n, m, k, p;//被数据恶心到了,这行类型改成ll才过 19 ll tmp, val; 20 while(cin>>n>>m>>k>>p) 21 { 22 memset(rsum, 0, sizeof(rsum)); 23 memset(csum, 0, sizeof(csum)); 24 memset(rres, 0, sizeof(rres)); 25 memset(cres, 0, sizeof(cres)); 26 for(int i =0; i < n; i++) 27 { 28 for(int j = 0; j < m; j++) 29 { 30 scanf("%I64d", &val); 31 rsum[i] += val;//rsum[i]表示第i行之和 32 csum[j] += val;//csum[i]表示第i列之和 33 } 34 } 35 while(!pq1.empty())pq1.pop(); 36 for(int i = 0; i < n; i++) 37 { 38 pq1.push(rsum[i]); 39 } 40 for(int i = 1; i <= k; i++) 41 { 42 tmp = pq1.top(); 43 pq1.pop(); 44 rres[i] = rres[i-1]+tmp;//rres[i]表示选了i行的值 45 tmp-=p*m; 46 pq1.push(tmp); 47 } 48 while(!pq2.empty())pq2.pop(); 49 for(int i = 0; i < m; i++) 50 { 51 pq2.push(csum[i]); 52 } 53 for(int i = 1; i <= k; i++) 54 { 55 tmp = pq2.top(); 56 pq2.pop(); 57 cres[i] = cres[i-1]+tmp;//cres[i]表示选了i列的值 58 tmp-=p*n; 59 pq2.push(tmp); 60 } 61 ll happy = inf; 62 happy = -happy*happy; 63 for(int i = 0; i <= k; i++) 64 happy = max(happy, rres[i]+cres[k-i]-p*i*(k-i)); 65 cout<<happy<<endl; 66 } 67 68 return 0; 69 }