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 x first
#define y second
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
pair<long long, long long> a[n];
for (int i = 0; i < n; i++) cin >> a[i].x >> a[i].y;
sort(a, a + n);
long long dp1[n], dp2[n];
fill(dp1, dp1 + n, 0);
fill(dp2, dp2 + n, 0);
dp1[0] = a[0].x + a[0].y;
for (int i = 1; i < n; i++) dp1[i] = max(dp1[i - 1] + a[i].y, a[i].x + a[i].y);
dp2[n - 1] = a[n - 1].y - a[n - 1].x;
for (int i = n - 2; i >= 0; i--) dp2[i] = max(dp2[i + 1] + a[i].y, a[i].y - a[i].x);
long long ans = 0;
for (int i = 0; i < n; i++) ans = max(ans, a[i].y);
for (int i = 0; i < n - 1; i++) ans = max(ans, dp1[i] + dp2[i + 1]);
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... |