func_code_string: #include <bits/stdc++.h>   using namespace std;   long long ans;   int a[305], b[305], c[305], dp[150005], DP[150005], i, j, k, n, m, d, l, r, J,       MIN;   struct node {     int x;     int y;   };   node st[150005];   int main() {     scanf( %d%d%d , &n, &m, &d);     for (i = 1; i <= m; i++) {       scanf( %d%d%d , &a[i], &b[i], &c[i]);       ans = ans + b[i];     }     for (j = 1; j <= n; j++) dp[j] = abs(a[1] - j);     for (i = 2; i <= m; i++) {       for (j = 1; j <= n; j++) DP[j] = 1453266144;       r = 0;       l = 1;       for (j = 1; j <= n; j++) {         st[++r].x = j;         st[r].y = dp[j];         while (r > l && st[r].y < st[r - 1].y) {           r--;           st[r] = st[r + 1];         }         while (l < r && (j - st[l].x - 1) / d + 1 > c[i] - c[i - 1]) l++;         DP[j] = min(DP[j], st[l].y + abs(a[i] - j));       }       r = 0;       l = 1;       J = j;       for (j = n; j >= 1; j--) {         st[++r].x = j;         st[r].y = dp[j];         while (r > l && st[r].y < st[r - 1].y) {           r--;           st[r] = st[r + 1];         }         while (l < r && (st[l].x - j - 1) / d + 1 > c[i] - c[i - 1]) l++;         DP[j] = min(DP[j], st[l].y + abs(a[i] - j));       }       for (j = 1; j <= n; j++) dp[j] = DP[j];     }     MIN = 1453266144;     for (j = 1; j <= n; j++) MIN = min(MIN, dp[j]);     cout << ans - MIN;     return 0;   }   