Dang_Quan_10_Tin

TL HSGHN L9 2022-2023

Jan 12th, 2023
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using ll = long long;
  5.  
  6. constexpr int N = 5e5 + 5;
  7. int n, id[N];
  8. ll a[N], b[N], c[N], d[N];
  9.  
  10. void Read()
  11. {
  12.     cin >> n;
  13.     for (int i = 1; i <= n; ++i)
  14.         cin >> a[id[i] = i] >> b[i];
  15. }
  16.  
  17. void Solve()
  18. {
  19.     sort(id + 1, id + n + 1, [&](const int &x, const int &y)
  20.          { return a[x] < a[y]; });
  21.  
  22.     ll ans = 0;
  23.  
  24.     for (int i = 1; i <= n; ++i)
  25.     {
  26.         c[i] = c[i - 1] + b[id[i]];
  27.         d[i] = max(d[i - 1], a[id[i]] - c[i - 1]);
  28.  
  29.         ans = max(ans, (c[i] - a[id[i]]) + d[i]);
  30.     }
  31.  
  32.     cout << ans;
  33. }
  34.  
  35. int32_t main()
  36. {
  37.     ios::sync_with_stdio(0);
  38.     cin.tie(0);
  39.     cout.tie(0);
  40.     Read();
  41.     Solve();
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment