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>
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define int long long
using namespace std;
#ifdef LOCAL
#define WOSAOJI freopen("in.txt", "r", stdin);
#else
#define WOSAOJI ios_base::sync_with_stdio(0), cin.tie(0);
#endif
#define chmax(a, b) (a) = (a) > (b) ? (a) : (b)
#define chmin(a, b) (a) = (a) < (b) ? (a) : (b)
const int MAXN = 5e5 + 5;
int n, s;
pair<int, int> a[MAXN];
int p_min[MAXN], p_max[MAXN];
int pre[MAXN];
signed main() {
WOSAOJI
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].F >> a[i].S;
s += a[i].S;
}
sort(a + 1, a + 1 + n);
int cur = s - a[n].F + a[1].F;
for (int i = 1; i < n; i++) {
p_min[i] = a[i + 1].F - a[i].F - a[i].S;
p_min[i] += p_min[i - 1];
pre[i] = max(pre[i - 1], p_min[i]);
}
for (int i = n; i > 1; i--) {
p_max[i] = a[i].F - a[i].S - a[i - 1].F;
p_max[i] += p_max[i + 1];
}
for (int i = 1; i <= n; i++) {
//cout << p_min[i] << " \n"[i == n];
}
for (int i = 1; i <= n; i++) {
//cout << p_max[i] << " \n"[i == n];
}
int ans = max(0ll, cur);
for (int i = 2; i <= n + 1; i++) {
chmax(ans, cur + p_max[i] + pre[i - 2]);
}
cout << ans << '\n';
}
# | 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... |