#include<bits/stdc++.h>
using namespace std;
#define int long long
int n , D;
struct Station{
int x ,a , b;
};
vector<Station> w;
bool cmp(Station u , Station v){
if(u.x != v.x){
return u.x < v.x;
}
else if(u.b != v.b){
return u.b>v.b;
}
else{
return u.a > v.a;
}
}
bool check(int f){
int fuel = f;
int curr = 0;
for(int i =0 ; i < n ;i ++){
int d =w[i].x - curr;
if(fuel < d){
return false;
}
if(f <= w[i].b){
fuel -= d;
fuel += w[i].a;
curr = w[i].x;
}
}
int d = D - curr;
if(fuel < d){
return false;
}
return true;
}
int solve(){
int l = 0, h = D , ans = LLONG_MAX/4;
for(int i = 0; i <= D ; i++){
if(check(i)){
ans = i;
break;
}
}
return ans;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
cin>>n>>D;
w.resize(n);
for(int i = 0 ; i < n ;i++){
cin>>w[i].x>>w[i].a>>w[i].b;
}
sort(w.begin() , w.end() , cmp);
// for(int i = 0 ; i < n ;i++){
// cout<<w[i].x << " " << w[i].a << " " << w[i].b<<"\n";
// }
cout<<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... |
| # | 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... |