DZY Loves Modification

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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:

  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.

Examples

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次操作,一个常数p,ans = 0;对于每次操作,可以任选一行或一列, 则ans += 这行(列)的数字和,然后这行(列)上的每个数字都-=p

 //2016.8.19
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define ll __int64
#define inf 999999999 using namespace std; ll rsum[], csum[], rres[], cres[];
priority_queue<ll> pq1;
priority_queue<ll> pq2; int main()
{
ll n, m, k, p;//被数据恶心到了,这行类型改成ll才过
ll tmp, val;
while(cin>>n>>m>>k>>p)
{
memset(rsum, , sizeof(rsum));
memset(csum, , sizeof(csum));
memset(rres, , sizeof(rres));
memset(cres, , sizeof(cres));
for(int i =; i < n; i++)
{
for(int j = ; j < m; j++)
{
scanf("%I64d", &val);
rsum[i] += val;//rsum[i]表示第i行之和
csum[j] += val;//csum[i]表示第i列之和
}
}
while(!pq1.empty())pq1.pop();
for(int i = ; i < n; i++)
{
pq1.push(rsum[i]);
}
for(int i = ; i <= k; i++)
{
tmp = pq1.top();
pq1.pop();
rres[i] = rres[i-]+tmp;//rres[i]表示选了i行的值
tmp-=p*m;
pq1.push(tmp);
}
while(!pq2.empty())pq2.pop();
for(int i = ; i < m; i++)
{
pq2.push(csum[i]);
}
for(int i = ; i <= k; i++)
{
tmp = pq2.top();
pq2.pop();
cres[i] = cres[i-]+tmp;//cres[i]表示选了i列的值
tmp-=p*n;
pq2.push(tmp);
}
ll happy = inf;
happy = -happy*happy;
for(int i = ; i <= k; i++)
happy = max(happy, rres[i]+cres[k-i]-p*i*(k-i));
cout<<happy<<endl;
} return ;
}

CodeForces 446B的更多相关文章

  1. codeforces 446B(优先队列)

    题目链接:http://codeforces.com/problemset/problem/446/B #include<bits/stdc++.h> using namespace st ...

  2. CodeForces 446B DZY Loves Modification

    题意: k次操作  每次选择一行或一列  得到所选数字的和  并将所选数字同一时候减去p  问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p  那么假设选了i次 ...

  3. 11.1 正睿停课训练 Day14

    目录 2018.11.1 正睿停课训练 Day14 A 字符串 B 取数游戏(贪心) C 魔方(模拟) 考试代码 B C 2018.11.1 正睿停课训练 Day14 时间:3.5h 期望得分:100 ...

  4. 【组队赛三】-D 优先队列 cf446B

    DZY Loves Modification Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Sub ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. Java从键盘输入

    package my;import java.util.Scanner; public class MyJava { /**     * @param args     */    public st ...

  2. Linux下VNC配置多个桌面和修改密码 不会当系统重启vnc失效

    1:vncserver 2:iptables -I INPUT -p tcp --dport 5901 -j ACCEPT   客户端方式 3:iptables -I INPUT -p tcp --d ...

  3. HDU 5677 ztr loves substring

    Manacher+二维费用多重背包 二进制优化 这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解. 多重背包做的时候需要二进制优化. #include< ...

  4. Java语言的学习

    众所周知,Java是上个世纪的语言产物,到现在已经有多个分支,Java和OC.Swift一样都是面向对象的语言,目前学习Java是想接触一下后台的开发,当然iOS也不会丢掉,毕竟多学一点不是坏事. 今 ...

  5. html元素被隐藏在后面

    当有些html元素是由于某些控件生成的,然后它被隐藏在其他元素的后面,但是它一开始是隐藏的,你无法直接在html或js里修改它的属性:z-index,使其可以放在更前面,这时可以在css里重新定义控件 ...

  6. Android L(5.0)源码之图形与图像处理之绘图——Canvas

    最近在研究android 5.0的gallery模块,学习了相关的知识点,准备写点博客总结一下,有时间了会补充完整

  7. 用weka来做Logistic Regression

    1.首先下载安装weka http://www.cs.waikato.ac.nz/ml/weka/downloading.html 2.打开weka,选择第一项Explorer 3.准备数据集文件,在 ...

  8. tp框架命名空间

    命名空间:相当于虚拟的目录在tp里面主要为了实现自动加载类 TP框架下有一个初始命名空间(相当于根目录)初始命名空间:ThinkPHP\Library 在初始命名空间下又包含很多根命名空间这些根命名空 ...

  9. 利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)

    源:利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开 //---------------------------------------------------------------- ...

  10. 普通项目如何转换成Maven项目 --转载自百度知道

    右键普通Java项目,在弹出的菜单中选择[Configure]-[Convert to Maven Project]: 2 在弹出的对话框中输入项目的groupId, artifactId和versi ...