Codeforces Round 872 (Div. 2) 1825B - LuoTianyi and the Table Solution
Problem Link : https://codeforces.com/contest/1825/problem/B
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n , m ;
- cin >> n >> m ;
- if ( n > m ) swap(n , m);
- vector<int>a(n*m) ;
- for ( int i = 0 ; i < n * m ; i++ ){
- cin >> a[i] ;
- }
- sort(a.begin(),a.end()) ;
- int mnf = a[0] ;
- int mns = a[1] ;
- int mxf = a[n*m - 1] ;
- int mxs = a[n*m - 2] ;
- int ans1 = ((n*m)- n) * (mxf-mnf) ;
- for ( int i = 1 ; i <= n - 1 ; i++) {
- ans1 +=(mxs-mnf) ;
- }
- int ans2 = ((n*m)- n) * (mxf-mnf) ;
- for ( int i = 1 ; i <= n - 1 ; i++) {
- ans2 +=(mxf-mns) ;
- }
- cout << max(ans1,ans2) << "\n";
- }
- }
No comments