CF372C Watching Fireworks is Fun

Description

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 \leq i \leq m$) launching is on time $t_{i}$ at section $a_{i}$. If you are at section $x$($1 \leq x \leq 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 Format

The first line contains three integers $n$,$m$,$d$($1 \leq n \leq 150000; 1 \leq m \leq 300; 1 \leq d \leq n$). Each of the next $m$ lines contains integers $a_{i}$,$b_{i}$,$t_{i}$($1 \leq a_{i} \leq n; 1 \leq b_{i} \leq 10^{9}; 1 \leq t_{i} \leq 10^{9}$). The $i$-th line contains description of the $i$-th launching. It is guaranteed that the condition $t_{i} \leq t_{i+1}$($1 \leq i \lt m$) will be satisfied.

Output Format

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.