Codeforces Round 861 (Div. 2) 1808B - Playing in a Casino Solution
Problem Link : https://codeforces.com/contest/1808/problem/B
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n , m ; cin >> n >> m ;
- std::vector<int> v[m];
- for ( int i = 0 ; i < n ; i++){
- for ( int j = 0 ; j < m ; j++){
- int x ;
- cin >> x ;
- v[j].push_back(x) ;
- }
- }
- for( int i = 0 ; i < m ; i++){
- sort(v[i].begin(),v[i].end()) ;
- }
- int ans = 0 ;
- for ( int i = 0 ; i < m ; i++) {
- vector<int>r(n,0) ;
- r[n-1] = v[i][n-1] ;
- for ( int j = n - 2 ; j >= 0 ; j--) {
- r[j] = r[j+1] + v[i][j] ;
- }
- for ( int j = 0 ; j < n - 1 ; j++) {
- int cnt = r[j+1] - (n -1 -j)*v[i][j] ;
- ans +=cnt ;
- }
- }
- cout << ans << endl ;
- }
- return 0 ;
- }
No comments