#include<bits/stdc++.h>
#define taskname "A"
using namespace std;
typedef long long ll;
const int lim = 1e5 + 5;
const ll INF = 1e18;
template<class T>void minimize(T& a, T b){
if(a > b){
a = b;
}
}
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
struct Data{
int x, g, d;
bool operator <(const Data& other){
return this->x < other.x;
}
};
int n;
Data a[lim];
namespace sub12{
void solve(){
ll ans = 0;
for(int i = 1; i <= n; i++){
ll gold = 0, energy = 0;
for(int j = i; j <= n; j++){
gold += a[j].g;
if((energy += a[j].d) >= a[j].x - a[i].x){
maximize(ans, gold);
}
}
}
cout << ans;
}
}
namespace sub3{
ll fe[lim], fg[lim];
void solve(){
ll ans = fe[0] = fg[0] = 0;
vector<int>l(n), r(n);
for(int i = 1; i <= n; i++){
fe[l[i - 1] = r[i - 1] = i] = fe[i - 1] + a[i].d;
fg[i] = fg[i - 1] + a[i].g;
}
sort(l.begin(), l.end(), [&] (int i, int j){
return fe[i] - a[i].x < fe[j] - a[j].x;
});
sort(r.begin(), r.end(), [&] (int i, int j){
return fe[i - 1] - a[i].x < fe[j - 1] - a[j].x;
});
int j = 0;
ll min_gold = INF;
for(int& i : l){
while(j < n && fe[r[j] - 1] - a[r[j]].x <= fe[i] - a[i].x){
minimize(min_gold, fg[r[j++] - 1]);
}
maximize(ans, fg[i] - min_gold);
}
cout << ans;
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].x >> a[i].g >> a[i].d;
}
sort(a + 1, a + n + 1);
if(n <= 5000){
sub12::solve();
}
else{
sub3::solve();
}
}
Compilation message (stderr)
divide.cpp: In function 'int main()':
divide.cpp:69:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |