#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair; using pll = pair; using vi = vector; using vl = vector; using vvi = vector; using vvl = vector; const ll INF = 1LL << 60; const ll MOD = 1000000007; template bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template struct SegmentTree { using F = function; int n; vector dat; F func; T UNIT; SegmentTree(int n_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) { n = 1; // full binary tree: num of leaves = n = 2^k >= n_ while (n < n_) n *= 2; dat.assign(2 * n - 1, UNIT); } SegmentTree(vector v_, F func_, T UNIT_) : func(func_), UNIT(UNIT_) { n = 1; while (n < v_.size()) n *= 2; dat.assign(2 * n - 1, UNIT); for (int i = 0; i < v_.size(); ++i) { dat[n - 1 + i] = v_[i]; } for (int i = n - 2; i >= 0; --i) { dat[i] = func(dat[2 * i + 1], dat[2 * i + 2]); } } void update(int k, T a) { // leaves are at index n-1 to 2*n-2 k += n - 1; dat[k] = a; while (k > 0) { // k -> parent node k = (k - 1) / 2; // func(child nodes) dat[k] = func(dat[2 * k + 1], dat[2 * k + 2]); } } T query(int a, int b, T k, int l, int r) { if (r <= a || b <= l) return UNIT; if (a <= l && r <= b) return dat[k]; else { T vl = query(a, b, 2 * k + 1, l, (l + r) / 2); T vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return func(vl, vr); } } // get result of func() in [a, b) T query(int a, int b) { return query(a, b, 0, 0, n); } }; int main() { ll n, m, a; cin >> n >> m >> a; vector>> rlp(n); for (int i = 0; i < m; ++i) { ll l, r, p; cin >> l >> r >> p; l--; r--; rlp[r].emplace_back(l, p); } auto f = [](ll l, ll r) { return max(l, r); }; SegmentTree dp(n + 1, f, -INF); // max score when you add line at n dp.update(0, 0); // vl dp(n + 1, 0); // 0 1 2 3 4 5 // 0 1 2 3 4 5 for (int i = 0; i < n; ++i) { if (rlp[i].size() == 0) { ll temp = dp.query(0, i + 1) - a; dp.update(i + 1, temp); } else { ll tt = 0; if (i != n - 1) tt -= a; for (auto &lp : rlp[i]) { ll temp = dp.query(lp.first, lp.first + 1) + lp.second; if (i != n - 1) temp -= a; chmax(tt, temp); } dp.update(i + 1, tt); } } ll ret = dp.query(0, n + 1); cout << ret << "\n"; return 0; }