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 int long long
using namespace std;
int n;
pair<int, int> t[501010];
int L[501010];
int rmq[50][501010];
int gt(int l, int r) {
int x = L[r - l + 1];
return min(rmq[x][l], rmq[x][r - (1 << x) + 1]);
}
int ans = -1010101010101010101;
int32_t main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> t[i].first >> t[i].second;
sort(t + 1, t + 1 + n, [](pair<int, int> q, pair<int, int> w) {
return q.first > w.first;
});
// cout << endl;
// for (int i = 1; i <= n; i++)
// cout << t[i].first << " " << t[i].second << endl;
// cout << endl;
L[1] = 0;
for (int i = 2; i <= n; i++)
L[i] = L[i / 2] + 1;
for (int i = 1; i <= n; i++)
rmq[0][i] = t[i].first;
for (int i = 1; i <= L[n]; i++) {
for (int j = 1; j + (1 << i) - 1 <= n; j++) {
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
}
int sum = 0;
for (int i = 1; i <= n; i++)
sum += t[i].second;
for (int i = 1; i <= n; i++) {
// cout << i << " -- " << sum << " " << t[i].first << " " << gt(i, n) << endl;
ans = max(ans, sum - t[i].first + gt(i + 1, n));
sum -= t[i].second;
}
cout << ans;
return 0;
}
/**
3
2 3
11 2
4 5
*/
# | 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... |