fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int t;
  5. cin >> t;
  6. for (int i = 0; i < t; i++) {
  7. int n, m;
  8. cin >> n >> m;
  9. vector<vector<int>> a(n, vector<int>(m));
  10. for (int i = 0; i < n; i++) {
  11. for (int j = 0; j < m; j++) { cin >> a[i][j]; }
  12. }
  13.  
  14. long long ans = 0;
  15. for (int i = 0; i < m; i++) {
  16. vector<long long> temp;
  17. long long sum = 0;
  18. for (int j = 0; j < n; j++) {
  19. temp.push_back(a[j][i]);
  20. sum += a[j][i];
  21. }
  22. long long curr = 0;
  23. sort(temp.begin(), temp.end());
  24. for (int j = 0; j < n; j++) {
  25. curr += temp[j];
  26. ans += llabs((sum - curr) - (n - 1 - j) * temp[j]);
  27. }
  28. }
  29.  
  30. cout << ans << "\n";
  31. }
  32. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty