#include <bits/stdc++.h>
using namespace std;
int n, m, d;
pair<long long, pair<long long, long long> > event[400];
long long score[200000];
long long dp[2][200000];
long long rmax[20][200000];
long long my_abs(long long a) {
  if (a >= 0) return a;
  return -a;
}
void make_rmax() {
  for (int i = 1; i <= n; i++) {
    rmax[0][i] = dp[0][i];
  }
  for (int k = 1; k <= 19; k++) {
    for (int i = 1; i <= n - (1 << k) + 1; i++) {
      rmax[k][i] = max(rmax[k - 1][i], rmax[k - 1][i + (1 << (k - 1))]);
    }
  }
}
long long get_rmax(int lo, int hi) {
  int i = 0;
  while ((1 << i) <= (hi - lo + 1)) i++;
  i--;
  return max(rmax[i][lo], rmax[i][hi - (1 << i) + 1]);
}
void step_dp(int tstep) {
  long long dist = (long long)d * (long long)tstep;
  make_rmax();
  for (int i = 1; i <= n; i++) {
    dp[0][i] =
        score[i] + get_rmax(max(1LL, i - dist), min((long long)n, i + dist));
  }
}
int main() {
  scanf("%d %d %d", &n, &m, &d);
  for (int i = 0; i < m; i++) {
    int a, b, t;
    scanf("%d %d %d", &a, &b, &t);
    event[i] = pair<long long, pair<long long, long long> >(
        t, pair<long long, long long>(a, b));
  }
  sort(event, event + m);
  memset(dp, 0, sizeof(dp));
  int curt = 1;
  for (int i = 0; i < m; i++) {
    memset(score, 0, sizeof(score));
    int newt = event[i].first;
    int j = i;
    while (j + 1 < m && event[i].first == event[j + 1].first) j++;
    for (int k = i; k <= j; k++) {
      for (int a = 1; a <= n; a++) {
        score[a] += event[k].second.second - my_abs(event[k].second.first - a);
      }
    }
    step_dp(newt - curt);
    curt = newt;
    i = j;
  }
  long long best = -(long long)1e18;
  for (int i = 1; i <= n; i++) {
    best = max(best, dp[0][i]);
  }
  cout << best << "\n";
}
