fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4.  
  5. int main() {
  6. // your code goes here
  7. ll n;
  8. cin>>n;
  9. ll sum = 0;
  10. vector<ll>p;
  11. for(ll i = 0; i < n; i++){
  12. ll a,b;
  13. cin>>a>>b;
  14. sum = sum - a + (b * n);
  15. p.push_back(a-b);
  16. } // Calculate the constant part in sum variable and store the a-b in array.
  17.  
  18. sort(p.begin(),p.end());reverse(p.begin(),p.end()); // sort them in descending order
  19. for(ll j=1;j<=n;j++){
  20. sum = sum + j*p[j-1]; // Now find the variable part sum.
  21. }
  22.  
  23. cout<<sum;cout<<"\n";
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5324KB
stdin
4
2 4
3 3
7 1
2 3
stdout
25