이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <cmath>
typedef long long llong;
const int MAXN = 500000 + 10;
const llong INF = 1e16;
int n;
llong idx[MAXN];
llong a[MAXN], b[MAXN];
llong a2[MAXN], b2[MAXN];
llong prefixB[MAXN];
void solve()
{
std::iota(idx + 1, idx + 1 + n, 1);
std::sort(idx + 1, idx + 1 + n, [&](int x, int y)
{
return std::make_pair(a[x], b[x]) < std::make_pair(a[y], b[y]);
});
for (int i = 1 ; i <= n ; ++i)
{
a2[i] = a[idx[i]];
b2[i] = b[idx[i]];
}
for (int i = 1 ; i <= n ; ++i)
{
a[i] = a2[i];
b[i] = b2[i];
}
llong ans = -INF, max = -INF;
for (int i = 1 ; i <= n ; ++i)
{
prefixB[i] = prefixB[i-1] + b[i];
ans = std::max(ans, prefixB[i] - a[i] + max);
max = std::max(max, a[i] - prefixB[i-1]);
}
std::cout << ans << '\n';
}
void read()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> a[i] >> b[i];
}
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
# | 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... |