#include <bits/stdc++.h>
#include <chrono>
#define ll long long int
#define endl '\n'
using namespace std;
using namespace std::chrono;
const ll INF = 4e18;
ll n, d;
vector<tuple<ll,ll,ll>> v;
bool valid(ll x) {
ll curr = x;
ll last = 0;
for (auto &[a, b, c] : v) {
ll dist = a - last;
last = a;
curr -= dist;
if (curr < 0) return false;
if (x <= c) {
curr += b;
if (curr > INF) curr = INF;
}
}
return curr >= (d - last);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
auto start = high_resolution_clock::now();
cin >> n >> d;
v.resize(n);
for (int i = 0; i < n; i++) {
cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]);
}
sort(v.begin(), v.end());
ll l = 0, r = d, ans = d;
while (l <= r) {
ll mid = l + (r - l) / 2;
if (valid(mid)) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
cout << ans << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken: " << duration.count() << " µs\n";
return 0;
}
| # | 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... |