#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll> a(500005), b(500005);
bool cmp (pair<ll, ll> c, pair<ll, ll> d) {
if (c.first > d.first) return true;
else if (c.first < d.first) return false;
else {
if (b[c.second] > b[d.second]) return true;
else return false;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
ll n;
cin >> n;
vector<pair<ll, ll>> v;
for (ll i = 0; i < n; i++) {
cin >> a[i] >> b[i];
v.push_back({b[i] - a[i], i});
}
sort(v.begin(), v.end(), cmp);
ll ans = 0, sum = 0, mx = -1e18, mn = 1e18;
for (ll i = 0; i < n; i++) {
sum += b[v[i].second];
mx = max(mx, a[v[i].second]);
mn = min(mn, a[v[i].second]);
ans = max(ans, sum - (mx - mn));
}
cout << ans;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |