fork download
  1. #include <bits/stdc++.h>
  2. #include <chrono>
  3. using namespace std;
  4. using namespace chrono;
  5. // "AJEET JAIN"----"JAI JINENDRA"
  6. /* "णमो अरिहंताणं",
  7.   "णमो सिद्धाणं",
  8.   "णमो आयरियाणं",
  9.   "णमो उवज्झायाणं",
  10.   "णमो लोए सव्वसाहूणं",
  11.   "",
  12.   "एसो पंच नमोक्कारो, सव्व पावप्पणासणो",
  13.   "मंगलाणं च सव्वेसिं, पडमं हवै मंगलं", */
  14.  
  15.  
  16. // Aliases to op
  17. using ll = long long;
  18. using ull = unsigned long long;
  19. using ld = double;
  20. using vll = vector<ll>;
  21.  
  22.  
  23. // Constants
  24. constexpr ll INF = 4e18;
  25. constexpr ld EPS = 1e-9;
  26. constexpr ll MOD = 1e9 + 7;
  27.  
  28.  
  29.  
  30. // Macros
  31. #define F first
  32. #define S second
  33. #define all(x) begin(x), end(x)
  34. #define allr(x) rbegin(x), rend(x)
  35. #define py cout<<"YES\n";
  36. #define pn cout<<"NO\n";
  37. #define forn(i,n) for(int i=0;i<n;i++)
  38. #define for1(i,n) for(int i=1;i<=n;i++)
  39.  
  40. // #define insert push_back
  41. #define pb push_back
  42. #define MP make_pair
  43. #define endl '\n'
  44.  
  45. /*
  46.   remove substring or subarray ---> try to think about sliding w
  47.  
  48.   */
  49.  
  50. /*
  51.  
  52.   Golden Rule
  53.  
  54.   1) problem is easy
  55.   2) proofs is easy
  56.   3) implementation is easy
  57.  
  58.   /*
  59.   ROUGH --
  60.  
  61.   ai*(j - 1) + bi*(n - j) = min
  62.   ai and bi for every i is constant we can play with position
  63.   expand form = ai*j - ai + bi*n -bi*j
  64.   rearrange -> (ai - bi)*j + bi*n - ai
  65.   sum of all i from 1 <= i <= n -->> ((a1 - b1)j1 + b1*n - a1) + ((a2 - b2)j2 + b1*n - a2)......+ ((an - bn)jn + bn*n - an)
  66.   => (a1 - b1)j1 + (a2 - b2)j2 ... + (an - bn)jn + (b1 + b2 + .... bn) - (a1 + a2 + .... an)
  67.   now just choose ji according to (ai - bi)
  68.  
  69.   */
  70.  
  71. void AJNJ(){
  72. int n;
  73. cin >> n;
  74. int x = n;
  75. vector<ll> v;
  76. ll sum_a = 0 , sum_b = 0;
  77. while(x--){
  78. ll a , b;
  79. cin >> a >> b;
  80. sum_a += a;
  81. sum_b += b;
  82. v.push_back(a - b);
  83. }
  84. sort(allr(v));
  85. ll ans = 0;
  86. for(int i = 0 ; i < v.size() ; i++){
  87. ans += v[i]*(i + 1);
  88. }
  89.  
  90. cout << ans + ((n*sum_b) - sum_a) << endl;
  91.  
  92. }
  93.  
  94.  
  95. int main(){
  96. ios::sync_with_stdio(0);
  97. cin.tie(0);
  98. cout.tie(0);
  99. int T = 1;
  100. // cin>>T;
  101. auto start1 = high_resolution_clock::now();
  102. while(T--){
  103. AJNJ();
  104. }
  105. auto stop1 = high_resolution_clock::now();
  106. auto duration = duration_cast<microseconds>(stop1 - start1);
  107. cerr << "Time: " << duration . count() / 1000 << " ms" << endl;
  108.  
  109. return 0;
  110. }
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
-3439090394622728964
stderr
Time: 1 ms