local n, m, a = io.read("*n", "*n", "*n") local t = {} for i = 1, m do t[i] = {} t[i].l, t[i].r, t[i].p = io.read("*n", "*n", "*n") end table.sort(t, function(x, y) if x.r ~= y.r then return x.r < y.r else return x.l < y.l end end) local mma = math.max local ret = {} local inf = -100000000000000 for i = 1, n do ret[i] = inf end local curmax = {} local curpos = 1 for i = 1, n do curmax[i] = 0 end for i = 1, m do local l, r, p = t[i].l, t[i].r, t[i].p local cost = r == n and 0 or a if l == 1 then ret[r] = mma(ret[r], p - cost) else -- print(i, l, r, p, ret[l - 1]) if ret[l - 1] == inf then -- ret[r] = mma(ret[r], -a + p - cost) ret[l - 1] = curmax[l - 1] - a end ret[r] = mma(ret[r], ret[l - 1] + p - cost) end if curpos ~= r then for k = curpos + 1, r do curmax[k] = mma(curmax[k], curmax[k - 1]) end curpos = r end curmax[r] = mma(curmax[r], ret[r]) end local tot = 0 for i = 1, n do -- print(i, ret[i]) tot = mma(tot, ret[i]) end print(tot)