class SegTree: def __init__(self, init_val, ide_ele, segfunc): self.n = len(init_val) self.num = 2**(self.n-1).bit_length() self.ide_ele = ide_ele self.segfunc = segfunc self.seg = [ide_ele]*2*self.num # set_val for i in range(self.n): self.seg[i+self.num] = init_val[i] # built for i in range(self.num-1, 0, -1): self.seg[i] = self.segfunc(self.seg[2*i], self.seg[2*i+1]) def update(self, k, x): k += self.num self.seg[k] = x while k: k = k >> 1 self.seg[k] = self.segfunc(self.seg[2*k], self.seg[2*k+1]) def query(self, l, r): if r <= l: return self.ide_ele l += self.num r += self.num lres = self.ide_ele rres = self.ide_ele while l < r: if r & 1: r -= 1 rres = self.segfunc(self.seg[r], rres) if l & 1: lres = self.segfunc(lres, self.seg[l]) l += 1 l = l >> 1 r = r >> 1 res = self.segfunc(lres, rres) return res def __str__(self): # for debug arr = [self.query(i,i+1) for i in range(self.n)] return str(arr) import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline INF = 10**18 def main(): n, m, a = map(int, input().split()) L = [0]*n R = [[] for i in range(n)] for i in range(m): l, r, p = map(int, input().split()) l, r = l-1, r-1 R[r].append((l, p)) seg = SegTree([-INF]*(n+2), -INF, max) seg.update(0, 0) for i in range(n): for l, p in R[i]: t = max(seg.query(0, l)-a, seg.query(l, l+1)) if i != n-1: seg.update(i+1, max(seg.query(i+1, i+2), t+p-a)) else: seg.update(i+1, max(seg.query(i+1, i+2), t+p)) #print(seg) print(seg.query(0, n+1)) if __name__ == '__main__': main()