#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
#include <random>
#include <chrono>
#include <fstream>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
struct trio{
int x, g, e;
};
int ans=0;
vector<trio> vect;
bool custom(trio a, trio b){
return a.x<b.x;
}
void dnc(int l, int r){
if (l>r)return;
int mid=(l+r)/2, mx=0;
vector<pii> left, right;
for (int i=mid+1, sume=0, sumg=0; i<=r; ++i)sume+=vect[i].e, sumg+=vect[i].g, right.pb(mp(sume-vect[i].x+vect[mid].x, sumg));
for (int i=mid-1, sume=0, sumg=0; i>=l; --i)sume+=vect[i].e, sumg+=vect[i].g, left.pb(mp(sume+vect[i].x-vect[mid].x, sumg));
for (auto a:right)if (vect[mid].e+a.fi>=0)mx=max(mx, a.se);
for (auto a:left)if (vect[mid].e+a.fi>=0)mx=max(mx, a.se);
for (int i=right.size()-2; i>=0; --i)right[i].fi=max(right[i].fi, right[i+1].fi);
reverse(right.begin(), right.end());
for (int i=0; i<left.size(); ++i){
auto it = upper_bound(right.begin(), right.end(), mp(-left[i].se-vect[mid].e, 0ll))-1;
if (it->fi+left[i].se+vect[mid].e>=0)mx=max(mx, left[i].se+it->se);
}
ans=max(ans, vect[mid].g+mx);
if (l==r)return;
dnc(l, mid-1);
dnc(mid+1, r);
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vect.resize(n);
for (int i=0; i<n; ++i)cin>>vect[i].x>>vect[i].g>>vect[i].e;
sort(vect.begin(), vect.end(), custom);
dnc(0, n-1);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |