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;
const int maxn = 500005;
using ll = long long;
pair<ll, ll> p[maxn];
ll t[maxn << 2];
void upd(int node, int b, int e, int i, ll val){
if(i > e || i < b)return;
if(b == i && i == e){
t[node] = val;
return;
}
int mid = (b + e) >> 1;
upd(2*node, b, mid, i, val);
upd(2*node + 1, mid + 1, e, i, val);
t[node] = max(t[2*node], t[2*node + 1]);
}
ll query(int node, int b, int e, int i, int j){
if(i > j)return 0;
if(i > e || j < b)return 0;
if(b >= i && j >= e)return t[node];
int mid = (b + e) >> 1;
return max(query(2*node, b, mid, i, j), query(2*node + 1, mid + 1, e, i, j));
}
int main(int argc, char const *argv[])
{
// freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%lld %lld", &p[i].first, &p[i].second);
}
sort(p + 1, p + n + 1);
vector<ll> pref(n + 1);
for(int i = 1; i <= n; i++){
pref[i] = pref[i - 1] + p[i].second;
}
ll ans = -1e18;
for(int i = 1; i <= n; i++){
upd(1, 1, n, i, p[i].first - pref[i - 1]);
}
for(int i = 1; i <= n; i++){
ans = max(ans, pref[i] - p[i].first + query(1, 1, n, 1, i));
}
cout << ans << endl;
return 0;
}
Compilation message (stderr)
art.cpp: In function 'int main(int, const char**)':
art.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
art.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld", &p[i].first, &p[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |