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>
#define ll long long
#define ld long double
#define st string
#define fr first
#define se second
const ll mod=1e9+7;
using namespace std;
ll p[100100], s[100100], n, ans=0, a[100100];
bool divide(ll x)
{
    ll l=1, r=1;
    while(a[r+1]-a[l]<=x && r+1<=n)
        r++;
    ll k=0;
    //cout<<x<<" "<<l<<" "<<r<<endl;
    while(l<=n)
    {
        while(a[r+1]-a[l]<=x && r+1<=n)
            r++;
        //cout<<x<<" "<<l<<" "<<r<<endl;
        ll ener=p[r]-p[l-1];
        if(ener>=x)
        {
            k++;
            ll gold=s[r]-s[l-1];
            ans=max(ans, gold);
        }
        l++;
    }
    l=n, r=n;
    while(a[l]-a[r-1]<=x && r-1>=1)
        r--;
    //cout<<x<<" "<<l<<" "<<r<<endl;
    while(l>0)
    {
        while(a[l]-a[r-1]<=x && r-1>=1)
        r--;
        //cout<<x<<" "<<l<<" "<<r<<endl;
        ll ener=p[l]-p[r-1];
        if(ener>=x)
        {
            k++;
            ll gold=s[l]-s[r-1];
            ans=max(ans, gold);
        }
        l--;
    }
    if(k) return true;
    else return false;
}
int main()
{
    //freopen("1.txt", "r", stdin);
    //freopen("1.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    for(ll i=1; i<=n; i++)
    {
        ll g, e;
        cin>>a[i]>>g>>e;
        p[i]=e+p[i-1];
        s[i]=g+s[i-1];
    }
    ll l=0, r=1e9+1;
    while(l+1<r)
    {
        //cout<<l<<" "<<r<<endl;
        ll d=(l+r)/2;
        if(divide(d)) l=d;
        else r=d;
    }
    //cout<<l<<" "<<r<<endl;
    cout<<ans;
    return 0;
}
/*
4 6 3
1 2 1
2 4 1
2 3 2
3 1 4
1 4 3
3 4 10
*/
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |