Watching Fireworks is Fun

A festival will be held in a town’s main street. There are n sections in the main street. The sections are numbered 1 through n from left to right. The distance between each adjacent sections is 1.

In the festival m fireworks will be launched. The i-th (1 ≤ i ≤ m) launching is on time t i at section a i. If you are at section x (1 ≤ x ≤ n) at the time of i-th launching, you’ll gain happiness value b i - |a i - x| (note that the happiness value might be a negative value).

You can move up to d length units in a unit time interval, but it’s prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness.

Note that two or more fireworks can be launched at the same time.

Input

The first line contains three integers nmd (1 ≤ n ≤ 150000; 1 ≤ m ≤ 300; 1 ≤ d ≤ n).

Each of the next m lines contains integers a ib it i (1 ≤ a i ≤ n; 1 ≤ b i ≤ 109; 1 ≤ t i ≤ 109). The i-th line contains description of the i-th launching.

It is guaranteed that the condition t i ≤ t i + 1 (1 ≤ i < m) will be satisfied.

Output

Print a single integer — the maximum sum of happiness that you can gain from watching all the fireworks.

Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

input

50 3 1
49 1 1
26 1 4
6 1 10

output

-31

input

10 2 1
1 1000 4
9 1000 4

output

1992

Solution:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <memory.h>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

#define left my_left
#define right my_right

const int inf = (int)1e9;
const int N = 600010;

int a[N], b[N], t[N];
int f[N], left[N], right[N];

int main() {
int n, m, d;
scanf("%d %d %d", &n, &m, &d);
for (int i = 0; i < m; i++) scanf("%d %d %d", a + i, b + i, t + i);
  long long add = 0;
  for (int i = 0; i < m; i++) add += b[i];
  for (int i = 0; i < m; i++)
    for (int j = i + 1; j < m; j++)
      if (t[i] > t[j]) {
int tmp;
tmp = t[i]; t[i] = t[j]; t[j] = tmp;
tmp = a[i]; a[i] = a[j]; a[j] = tmp;
}
for (int i = 1; i <= n; i++) f[i] = 0;
  int when = 0;
  for (int ev = 0; ev < m; ev++) {
    long long units = (long long)d * (t[ev] - when);
    if (units > n) units = n;
int len = 2 * units + 1;
int nn = ((n + len - 1) / len) * len;
for (int i = n + 1; i <= nn; i++) f[i] = inf;
    for (int i = 1; i <= nn; i++) {
      if ((i - 1) % len == 0) left[i] = inf;
      else left[i] = left[i - 1];
      if (f[i] < left[i]) left[i] = f[i];
    }
    for (int i = nn; i >= 1; i--) {
if (i % len == 0) right[i] = inf;
else right[i] = right[i + 1];
if (f[i] < right[i]) right[i] = f[i];
    }
    for (int i = 1; i <= n; i++) {
      int lg = i - units;
      int rg = i + units;
      if (lg < 1) lg = 1;
      if (rg > nn) rg = nn;
if (lg == 1) f[i] = left[rg]; else
if (rg == nn) f[i] = right[lg]; else {
f[i] = left[rg];
if (right[lg] < f[i]) f[i] = right[lg];
      }
    }
    for (int i = 1; i <= n; i++) {
      int diff = i - a[ev];
      if (diff < 0) diff = -diff;
      f[i] += diff;
    }
    when = t[ev];
  }
  int ans = f[1];
  for (int i = 1; i <= n; i++)
    if (f[i] < ans) ans = f[i];
  cout << (add - ans) << endl;
  return 0;
}