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>
#pragma GCC optimize("Ofast")
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
int n;
ll a[N];
ll b[N];
ll d[N];
ll g[N];
ll t[8 * N];
map < ll , int > m;
vector < pair < ll , pair < ll , ll > > > v;
ll get( int x , int l , int r , int tl , int tr )
{
if( tl > tr )return 1e18;
if( l == tl && r == tr ){
return t[x];
}
int mid = (l + r) / 2;
return min( get( x * 2 , l , mid , tl , min(tr , mid) ) ,
get( x * 2 + 1 , mid + 1 , r , max(tl , mid + 1) , tr ) );
}
void upd( int x , int l , int r , int pos , ll val )
{
if( l == r ){
t[x] = min( t[x] , val );
return;
}
int mid = (l + r) / 2;
if( mid >= pos )upd( x * 2 , l , mid , pos , val );
else upd( x * 2 + 1 , mid + 1 , r , pos , val );
t[x] = min( t[x * 2] , t[x * 2 + 1] );
}
void solve()
{
cin >> n;
ll ans = 0;
for( int i = 1; i <= n; i++ ){
ll x,y,z;
cin >> x >> y >> z;
ans = max( ans , y );
v.push_back({x , {y , z}});
}
for( int i = 1; i <= 8 * n; i++ ){
t[i] = 1e18;
}
sort( v.begin() , v.end() );
vector < int > o;
for( int i = 0; i < (int)v.size(); i++ ){
d[i] = v[i].se.se;
if( i > 0 )d[i] += d[i - 1] , g[i] = g[i - 1];
g[i] += v[i].se.fi;
a[i] = d[i - 1] - v[i].fi;
b[i] = d[i] - v[i].fi;
o.push_back(a[i]);
o.push_back(b[i]);
}
int G = 0;
sort( o.begin() , o.end() );
for( int i = 0; i < (int)o.size(); i++ ){
if( m.find(o[i]) == m.end() ){
m[o[i]] = ++G;
}
}
for( int i = 0; i < (int)v.size(); i++ ){
ll x = get( 1 , 1 , G , 1 , m[b[i]] );
//cout << i << " " << x << " " << m[a[i]] << " " << m[b[i]] << " " << g[i] << "\n";
ans = max( ans , g[i] - x );
upd( 1 , 1 , G , m[a[i]] , (i > 0 ? g[i - 1] : 0) );
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int t = 1;//cin >> t;
while( t-- ){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |