#include<algorithm>#include<cstring>#include<iostream>usingnamespacestd;typedeflonglongll;constintmaxn=150000+10;constintmaxm=300+10;llf[2][maxn];lla[maxm],b[maxm],t[maxm];intn,m,d;intque[maxn];intfl=1;voidinit(){memset(f,207,sizeof(f));memset(que,0,sizeof(que));for(inti=1;i<=n;i++)f[0][i]=0;fl=1;}voiddp(){init();for(inti=1;i<=m;i++){intl=1,r=0,k=1;for(intj=1;j<=n;j++){// 在这里使用了单调队列的优化,推式子详见上面for(;k<=min(1ll*n,j+d*(t[i]-t[i-1]));k++){while(l<=r&&f[fl^1][que[r]]<=f[fl^1][k])r--;que[++r]=k;}while(l<=r&&que[l]<max(1ll,j-d*(t[i]-t[i-1])))l++;f[fl][j]=f[fl^1][que[l]]-abs(a[i]-j)+b[i];}fl^=1;}}intmain(){cin>>n>>m>>d;for(inti=1;i<=m;i++)cin>>a[i]>>b[i]>>t[i];// then dpdp();llans=-1e18;for(inti=1;i<=n;i++)ans=max(ans,f[fl^1][i]);cout<<ans<<endl;return0;}