[Team Competition 3]-D priority queue cf446b

Source: Internet
Author: User
Dzy loves Modification
Time Limit: 2000 ms memory limit: 262144kb 64bit Io format: % i64d & % i64u
Submit

Status

Practice

Codeforces 446b
Description
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 cocould get after executing Ming 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 cocould get.

Sample Input
Input
2 2 2 2
1 3
2 4
Output
11
Input
2 2 5 2
1 3
2 4
Output
11
Hint
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
<Span style = "color: # 3333ff;">/* _______________________________________________________________________________________ Author: grant yuan time: 2014.7.21 algorithm: priority_queue explain: first, the rows and columns are solved in the range of 1 to K respectively, and then a maximum value satisfying I and K-I is obtained. Note that the final result minus the repeated calculation is calculated, data larger than int type will also be tested. Optional */# include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <queue> # include <functional> using namespace STD; # define INF 999999999; priority_queue <__ int64> q1; priority_queue <__ int64> Q2 ;__ int64 M, N, K, P, h; __int64 A [1003] [1003] ;__ int64 S1 [1003], S2 [1003] ;__ int64 sum1 [1000003], sum2 [1000003] ;__ int64 res, ANS, HH; int main () {CIN >>n> m> K> P; ans = inf; ans =-ans * ans; memset (S1, 0, sizeof (S1); memset (S2, 0, sizeof (S2); memset (sum1, 0, sizeof (sum1); memset (sum2, 0, sizeof (sum2); While (! Q1.empty () q1.pop (); While (! Q2.empty () q2.pop (); For (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) {scanf ("% i64d", & A [I] [J]); S1 [I] + = A [I] [J]; s2 [J] + = A [I] [J];} For (INT I = 0; I <n; I ++) q1.push (S1 [I]); for (INT I = 0; I <m; I ++) q2.push (s2 [I]); For (INT I = 1; I <= K; I ++) {H = q1.top (); q1.pop (); if (I = 1) sum1 [I] = H; else sum1 [I] = sum1 [I-1] + h; h-= m * P; q1.push (h);} For (INT I = 1; I <= K; I ++) {H = q2.top (); q2.pop (); if (I = 1) sum2 [I] = H; else sum2 [I] = sum2 [I-1] + H; H-= N * P; q2.push (h );} for (INT I = 0; I <= K; I ++) {res = sum1 [I] + sum2 [k-I]; ans = max (ANS, res-I * p * (k-I);} cout <ans <Endl; return 0 ;}</span>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.