#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; template > struct erasable_priority_queue { priority_queue, Comp> q, q_erase; void push(T x) { q.push(move(x)); } template void emplace(Args&&... args) { q.emplace(forward(args)...); } void erase(T x) { q_erase.push(move(x)); } decltype(auto) top() { assert(!empty()); expose_top(); return q.top(); } void pop() { assert(!empty()); expose_top(); q.pop(); } int size() { return int(q.size()) - int(q_erase.size()); } bool empty() { return !size(); } void expose_top() { while (!q_erase.empty() && q_erase.top() == q.top()) { q_erase.pop(); q.pop(); } } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, s, h; cin >> n >> s >> h; struct S { int x, y, z; }; vector d(n); for (auto& [x, y, z] : d) cin >> x >> y >> z; VL z_acc(n + 1); rep(i, n) z_acc[i+1] = z_acc[i] + d[i].z; constexpr int INF = 1001001001; d.emplace_back(2 * INF, 2 * INF, 0); VL dp(n + 1); int ptr1 = 0, ptr2 = 0; erasable_priority_queue q; rep(i, n) { chmax(dp[i+1], dp[i]); q.emplace(dp[i] - z_acc[i]); while (d[i].y - d[ptr1].x > s) { q.erase(dp[ptr1] - z_acc[ptr1]); ptr1++; } if (!q.empty()) { while (d[ptr2].x - d[i].y < h) ptr2++; chmax(dp[ptr2], q.top() + z_acc[i+1]); } } cout << dp[n] << '\n'; }