fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5. const int MAX_N = 1e6 + 5;
  6. pair<long long, long long> arr[MAX_N];
  7.  
  8.  
  9. bool cmp(pair<long long, long long> x, pair<long long, long long> y)
  10. {
  11. return max(x.first, x.first + y.first - x.second) < max(y.first, x.first + y.first - y.second);
  12. }
  13.  
  14. int main()
  15. {
  16. ios_base::sync_with_stdio(false);
  17. cin.tie(0);
  18. cout.tie(0);
  19.  
  20. while(cin >> n)
  21. {
  22. for (int i = 0; i < n; ++i)
  23. cin >> arr[i].first >> arr[i].second;
  24. sort(arr, arr + n, cmp);
  25.  
  26. long long ans = 0, s = 0;
  27. for (int i = 0; i < n; ++i)
  28. {
  29. long long need = arr[i].first - s;
  30. if (need > 0)
  31. {
  32. ans += need;
  33. s += need;
  34. }
  35. s += arr[i].second - arr[i].first;
  36. }
  37. cout << ans << '\n';
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 4472KB
stdin
Standard input is empty
stdout
Standard output is empty