#include #define rep(i,n) for(int i=0;i ; const ll INF = 1e18; const int MOD = 1000000007; int main(){ int n,m,a; cin >> n >> m >> a; vector> section(m,vector(3,0)); vector before(m,-1); vector

dp(m+1); rep(i,m){ ll l,r,p; cin >> l >> r >> p; --l; section[i][0] = r; section[i][1] = l; section[i][2] = p; } sort(section.begin(),section.end()); rep(i,m){ int l = -1,r = i; while(r - l > 1){ int mid = (l+r)/2; if(section[mid][0] <= section[i][1]) l = mid; else r = mid; } before[i] = l; } dp[0] = P(0,0); for(int i=1;i<=m;i++){ dp[i] = dp[i-1]; if(before[i-1] == -1){ ll res = section[i-1][2]; int c = 2; if(section[i-1][1] == 0) --c; if(section[i-1][0] == n) --c; res -= a * c; if(res >= dp[i].first){ dp[i] = P(res,section[i-1][0]); } } else{ ll res = dp[before[i-1]+1].first + section[i-1][2]; int c = 2; if(dp[before[i-1]+1].second == section[i-1][1]) --c; if(section[i-1][0] == n) --c; res -= c * a; if(res >= dp[i].first){ dp[i] = P(res,section[i-1][0]); } } } //rep(i,m) cout << section[i][0] << " " << section[i][1] << " " << section[i][2] << endl; //rep(i,m) cout << before[i] << " " << endl; //rep(i,m+1) cout << dp[i].first << " " << dp[i].second << endl; cout << dp[m].first << endl; return 0; }