๐Ÿ“ฆ ved015 / Codeforces-solution

๐Ÿ“„ casino1200.cpp ยท 46 lines
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#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    int t;
    cin >> t;
    while(t--){
        long long n,m;
        cin >> n >> m;
        vector<vector<long long> > grid(n,vector<long long>(m,0));
        for(long long i = 0; i < n; i++){
            for(long long j = 0; j < m; j++){
                long long a;
                cin >> a;
                grid[i][j] = a;
            }
        }

        long long ans = 0;
        for(long long k = 0; k < m; k++){
            vector<long long> column;
            for(long long j = 0; j < n; j++){
                column.push_back(grid[j][k]);
            }
            sort(column.begin(),column.end());

            long long prefixsum = 0;
            long long totsum = 0;
            for(auto &it : column){
                totsum += it;
            }
            for(long long i = 0; i < n; i++){
                ans += totsum - ((n - i)*column[i]) - prefixsum;
                prefixsum += column[i];
            }
        }
        if(n == 1){
            cout << 0 << endl;
            continue;
        }
        cout << ans << endl;

    }
    return 0;
}