CONSTRAINT –
PROBLEM - Tournament - Problems - CodeChef
#include<bits/stdc++.h>
using namespace std;
/*#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << endl;
//use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define trace(...)
#endif*/
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
unsigned long long n;
cin>>n;
vector<unsigned long long> v;
int r=0;
for(unsigned long long i=1;i<=n;i++){
unsigned long long g;
cin>>g;
v.push_back(g);
}
int no_matches = (n*(n-1))/2;
for(unsigned long long i=1;i<n;i++){
for(long long j=i+1;j<=n;j++){
if(v[i-1]>=v[j-1]){
r += (v[i-1]-v[j-1]);
}
else{
r += (v[j-1]-v[i-1]);
}
}
}
cout<<r;
}