import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class SegmentTree(object): #N:処理する区間の長さ def __init__(self, N): self.N0 = 2 ** (N - 1).bit_length() self.data = [-INF] * (2 * self.N0) #k番目の値をxに更新 def update(self, k, x): k += self.N0 - 1 self.data[k] = x while k > 0: k = (k - 1) // 2 self.data[k] = max(self.data[2 * k + 1], self.data[2 * k + 2]) #区間[l, r]の最大値 def query(self, l, r): L = l + self.N0; R = r + self.N0 + 1 m = -INF while L < R: if R & 1: R -= 1 m = max(m, self.data[R - 1]) if L & 1: m = max(m, self.data[L - 1]) L += 1 L >>= 1; R >>= 1 return m #処理内容 def main(): N, M, A = getlist() line = [] for i in range(M): l, r, p = getlist() line.append([r, l, p]) line.sort() # セグ木でDP Seg = SegmentTree(N + 2) Seg.update(0, 0) for i in range(M): r, l, p = line[i] if r != N: newScore = max(Seg.query(0, l - 1) - 2 * A + p, Seg.query(l - 1, l - 1) - A + p, Seg.query(r, r)) else: newScore = max(Seg.query(0, l - 1) - A + p, Seg.query(l - 1, l - 1) + p, Seg.query(r, r)) Seg.update(r, newScore) ans = max(Seg.data) print(ans) if __name__ == '__main__': main()