-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMaximum cost.cpp
69 lines (56 loc) · 1.52 KB
/
Maximum cost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Solution by Rahul Surana
***********************************************************
Question Not available 50% test cases passing
***********************************************************
*/
#include<bits/stdc++.h>
using namespace std;
long long solve (int N, int M, vector<vector<int> > matrix) {
for(int i = 0 ; i < N; i++){
sort(matrix[i].begin(),matrix[i].end());
}
long long ans = 0;
for(int i = 0 ; i < N; i++){
int mans = 0;
for(int k = 0; k < M; k++){
int d = 0;
int x = matrix[i][k];
for(int j = 0; j< N; j++){
int z = 1;
while(z <= M && matrix[j][M-z] > x) z++;
if(z == M+1) d+= x;
else d+= x- matrix[j][M-z];
}
mans = max(mans,d);
}
ans += mans;
// cout << ans <<" ";
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i = 0; t_i < T; t_i++)
{
int N;
cin >> N;
int M;
cin >> M;
vector<vector<int> > matrix(N, vector<int>(M));
for (int i_matrix = 0; i_matrix < N; i_matrix++)
{
for(int j_matrix = 0; j_matrix < M; j_matrix++)
{
cin >> matrix[i_matrix][j_matrix];
}
}
long long out_;
out_ = solve(N, M, matrix);
cout << out_;
cout << "\n";
}
}