fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9. #include <stack>
  10. #include <numeric>
  11. #include <climits>
  12.  
  13. using namespace std;
  14.  
  15. typedef long long int ll;
  16.  
  17. ll solve() {
  18. int n;
  19. cin>>n;
  20. vector<pair<int,int>> arr(n);
  21. for(int i=0;i<n;i++){
  22. cin>>arr[i].first;
  23. cin>>arr[i].second;
  24. }
  25.  
  26. vector<ll> diff(n);
  27.  
  28. for(int i=0;i<n;i++){
  29. diff[i] = arr[i].first - arr[i].second;
  30. }
  31.  
  32. sort(diff.rbegin(),diff.rend());
  33.  
  34. ll ans=0;
  35. ll sum_a=0;
  36. ll sum_b=0;
  37.  
  38.  
  39.  
  40. for(int i=1;i<=n;i++){
  41. ans = ans+ diff[i-1]*i;
  42. sum_a = sum_a + arr[i-1].first;
  43. sum_b = sum_b + arr[i-1].second;
  44.  
  45. }
  46.  
  47. ll final_ans = ans + n*sum_b - sum_a;
  48. return final_ans;
  49.  
  50.  
  51. }
  52.  
  53. int main() {
  54. ios_base::sync_with_stdio(false);
  55. cin.tie(NULL);
  56.  
  57.  
  58. cout<<solve()<<endl;
  59.  
  60.  
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 5328KB
stdin
3
4 2
2 3
6 1
stdout
12