#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
using namespace std;
#define sped_up ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const ll INF = (ll)1e9 + 1, INFL = (ll)1e18 + 1;
const ll mod = (ll)1e9 + 7, MAXN = (ll)5e5 + 1;
pair <ll, ll> t[MAXN * 4];
vector <pair <ll, ll>> v1;
ll a[MAXN], b[MAXN];
void build (ll v, ll tl, ll tr) {
if (tl == tr) {
t[v] = {0, v1[tl - 1].first};
return;
}
ll tm = (tl + tr) / 2;
build (v * 2, tl, tm);
build (v * 2 + 1, tm + 1, tr);
if (t[v * 2].first + t[v * 2].second > t[v * 2 + 1].first + t[v * 2 + 1].second) t[v] = t[v * 2];
else t[v] = t[v * 2 + 1];
}
pair <ll, ll> get (ll v, ll tl, ll tr, ll l, ll r) {
if (l <= tl && tr <= r) return t[v];
if (l > tr || tl > r) return {-INFL, 0};
ll tm = (tl + tr) / 2;
pair <ll, ll> a = get (v * 2, tl, tm, l, r);
pair <ll, ll> b = get (v * 2 + 1, tm + 1, tr, l, r);
if (a.first + a.second > b.first + b.second) return a;
else return b;
}
void upd (ll v, ll tl, ll tr, ll pos, ll val) {
if (tl == tr) {
t[v].first = val;
return;
}
ll tm = (tl + tr) / 2;
if (pos <= tm) upd (v * 2, tl, tm, pos, val);
else upd (v * 2 + 1, tm + 1, tr, pos, val);
if (t[v * 2].first + t[v * 2].second > t[v * 2 + 1].first + t[v * 2 + 1].second) t[v] = t[v * 2];
else t[v] = t[v * 2 + 1];
}
int main () {
ll n;
cin >> n;
vector <pair <ll, ll>> v;
for (int i = 1; i <= n; i++) {
ll a, b;
cin >> a >> b;
v.push_back({a, b});
}
sort (v.begin(), v.end());
ll cnt = v[0].second;
for (int i = 1; i < v.size(); i++) {
if (v[i].first == v[i - 1].first) cnt += v[i].second;
else {
v1.push_back({v[i - 1].first, cnt});
cnt = v[i].second;
}
}
ll mx = 0;
v1.push_back({v[v.size() - 1].first, cnt});
build (1, 1, v1.size());
for (int i = 0; i < v1.size(); i++) {
pair <ll, ll> gt = get(1, 1, v1.size(), 1, i + 1);
mx = max(mx, gt.first + v1[i].second - v1[i].first + gt.second);
upd(1, 1, v1.size(), i + 1, gt.first + v1[i].second - v1[i].first + gt.second);
}
cout << mx;
}
# | 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... |