[CodeForces - 447D] D - DZY Loves Modification

D - DZY Loves Modification

As we know, DZY loves playing games. One day DZY decided to play with a n × mmatrix. To be more precise, he decided to modify the matrix with exactly koperations.

Each modification is one of the following:

  1. 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.
  2. 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.

Example

Input
2 2 2 2
1 3
2 4
Output
11
Input
2 2 5 2
1 3
2 4
Output
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次操作,每次操作可以选择一行(列),然后把ans累加上这一行(列)的数的和,然后把这一行(列)的每一个数都减去一个固定的值.

要求最大化ans.

我们设R[i]为选择i个(次)行的最优解,C[i]为选择i个(次)列的最优解,由于选择的总次数为K,所以

ans=max(ans,R[i]+C[K-i]).然而这是不对的,因为在考虑R和C的时候是相对独立的,不受另一个因素的影响,而实际上,选择i个行,j个列,会产生i*(K-i)个交点,由于交点的存在,所以还要减去i*(K-i)*p(那个固定值).

所以ans=max(ans,R[i]+C[K-1]-i*(K-i)*p)(要注意数据类型)

那么我们现在来关注如何构造出R和C.由于贪心的想法,我们肯定挑目前和最大的行(列),那么我们可以用两个优先队列来维护一下不就好了.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=1005,maxK=1000005;
 8 int sum_R[maxn],sum_C[maxn];
 9 long long R[maxK],C[maxK];
10 int n,m,K,p,a[maxn][maxn];
11 long long ans;
12 struct node{
13     int x,index;
14     bool operator < (const node u) const {return x<u.x;}
15 };
16 priority_queue <node> Q_R,Q_C;
17 int read(){
18     int x=0,f=1; char ch=getchar();
19     while (ch<'0'||ch>'9'){if (ch=='-') f=-f; ch=getchar();}
20     while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
21     return x*f;
22 }
23 int main(){
24     n=read(),m=read(),K=read(),p=read(),ans=0;
25     for (int i=1; i<=n; i++)
26         for (int j=1; j<=m; j++) a[i][j]=read();
27     for (int i=1; i<=n; i++){
28         sum_R[i]=0;
29         for (int j=1; j<=m; j++) sum_R[i]+=a[i][j];
30     }
31     for (int i=1; i<=m; i++){
32         sum_C[i]=0;
33         for (int j=1; j<=n; j++) sum_C[i]+=a[j][i];
34     }
35     R[0]=C[0]=0;
36     for (int i=1; i<=n; i++){node P; P.x=sum_R[i],P.index=i; Q_R.push(P);}
37     for (int i=1; i<=K; i++){
38         node P=Q_R.top(); Q_R.pop();
39         R[i]=R[i-1]+P.x,P.x-=m*p,Q_R.push(P);
40     }
41     for (int i=1; i<=m; i++){node P; P.x=sum_C[i],P.index=i; Q_C.push(P);}
42     for (int i=1; i<=K; i++){
43         node P=Q_C.top(); Q_C.pop();
44         C[i]=C[i-1]+P.x,P.x-=n*p,Q_C.push(P);
45     }
46     ans=-123456789012345;
47     for (int i=0; i<=K; i++) ans=max(ans,R[i]+C[K-i]-(long long)i*(K-i)*p);
48     printf("%lld",ans);
49     return 0;
50 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7215799.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值