class segtree: def __init__(self,n): self.size=1 self.height=0 while self.size>h self.lazy[2*y]+=self.lazy[y] self.lazy[2*y+1]+=self.lazy[y] self.dat[y]+=self.lazy[y] self.lazy[y]=0 def bottom(self,x): x+=self.size while x>1: x//=2 self.dat[x]=max(self.dat[2*x]+self.lazy[2*x],self.dat[2*x+1]+self.lazy[2*x+1]) N,K=map(int,input().split()) Ax=set() Ay=set() Tx={} Ty={} L=[] for i in range(N): x,y,w=map(int,input().split()) L.append((2*x,2*y,w)) Ax.add(2*x) Ay.add(2*y) Ax.add(2*x+2*K+1) Ay.add(2*y+2*K+1) Ax=list(Ax) Ay=list(Ay) Ax.sort() Ay.sort() Mx=len(Ax) My=len(Ay) for i in range(Mx): Tx[Ax[i]]=i for i in range(My): Ty[Ay[i]]=i Gx=[[] for i in range(Mx)] for i in range(N): x,y,w=L[i][:] px,py=Tx[x],Ty[y] px2,py2=Tx[x+2*K+1],Ty[y+2*K+1] Gx[px].append((py,+w)) Gx[px].append((py2,-w)) Gx[px2].append((py,-w)) Gx[px2].append((py2,w)) result=0 Z=segtree(My) for i in range(Mx): for B in Gx[i]: pos,w=B[:] Z.propagate(pos) Z.propagate(My-1) Z.lazyup(pos,My,w) Z.bottom(pos) Z.bottom(My-1) Z.propagate(0) Z.propagate(My-1) ans=Z.querry(0,My) result=max(result,ans) print(result)