fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll;
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8. ll sum = 0;
  9. vector<ll> p;
  10. for(int i = 0 ; i< n ; i++){
  11. int a , b;
  12. cin>>a>>b;
  13. sum += b*n - a;
  14. p.push_back(b-a);
  15. }
  16. sort(p.begin(),p.end());
  17. reverse(p.begin(),p.end());
  18. for(int j = 1; j<= n ; j++){
  19. sum = sum + j*p[j-1];
  20. }
  21. cout<<sum<<endl;
  22. return 0;
  23. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
2 3
4 5
6 7 
8 9
4 8
stdout
154