이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int NMAX = 5e5 + 5;
int N, lg2[NMAX];
int64 pref_sum[NMAX], rmq_max[NMAX][20], ans;
pair<int64,int> a[NMAX];
void compute_lg2() {
lg2[1] = 0;
for(int i = 2; i <= N; ++i)
lg2[i] = lg2[i >> 1] + 1;
}
void compute_rmq() {
for(int i = 1; i <= N; ++i)
rmq_max[i][0] = pref_sum[i] - a[i].first;
for(int j = 1; (1 << j) <= N; ++j)
for(int i = 1; i + (1 << j) - 1 <= N; ++i)
rmq_max[i][j] = max(rmq_max[i][j - 1], rmq_max[i + (1 << (j - 1))][j - 1]);
}
int query_max(int l, int r) {
int k = lg2[r - l + 1];
int dif = (r - l + 1) - (1 << k);
return max(rmq_max[l][k], rmq_max[l + dif][k]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> N;
for(int i = 1; i <= N; ++i)
cin >> a[i].first >> a[i].second;
sort(a + 1, a + N + 1);
for(int i = 1; i <= N; ++i)
pref_sum[i] = pref_sum[i - 1] + a[i].second;
compute_lg2();
compute_rmq();
for(int i = 1; i <= N; ++i) {
int64 best = a[i].first + query_max(i, N) - pref_sum[i - 1];
ans = max(ans, best);
}
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... |