This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
using ll = long long;
constexpr int INF = (numeric_limits<int>::max()) / 2;
constexpr ll INFLL = (numeric_limits<ll>::max()) / 2;
template<class T>
istream &operator>>(istream &is, vector<T> &a) {
for (auto &i: a) {
is >> i;
}
return is;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector<pair<ll, ll>> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i].first >> v[i].second;
}
sort(all(v));
vector<ll> pref(n + 1);
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1] + v[i - 1].second;
}
ll ans = -INFLL, mn = INFLL;
auto add = [&](int i) {
if (i + 1 < n) {
mn = min(mn, pref[i] - v[i].first);
}
};
add(0);
for (int i = 1; i <= n; ++i) {
// for (int j = 0; j < i; ++j) {
// ans = max(ans, pref[i] - pref[j] - (v[i - 1].first - v[j].first));
// }
ans = max(ans, pref[i] - v[i - 1].first - mn);
add(i);
}
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... |