#include<bits/stdc++.h>
using namespace std;
int n,m;
int f[5010][5010];
int q(int a,int b)
{
if(a>b)
return a;
return b;
}
int main()
{
cin>>n>>m;
int x,y,v;
int xmax=-1e9,ymax=-1e9;
for(int i=0;i<n;++i)
{
cin>>x>>y>>v;
f[x][y]=v;
xmax=q(xmax,x);
ymax=q(ymax,y);
}
xmax++;
ymax++;
//cout<<xmax<<endl<<ymax<<endl;
/*for(int i=0;i<xmax;++i)
{
for(int j=0;j<ymax;++j)
cout<<f[i][j];
cout<<endl;
}*/
for(int j=1;j<=ymax;++j)
f[0][j]=f[0][j]+f[0][j-1];
for(int i=1;i<=xmax;++i)
f[i][0]=f[i][0]+f[i-1][0];
for(int i=1;i<=xmax;++i)
{
for(int j=1;j<=ymax;++j)
{
f[i][j]=f[i][j]+f[i-1][j]+f[i][j-1]-f[i-1][j-1];
}
}
/*for(int i=0;i<xmax;++i)
{
for(int j=0;j<ymax;++j)
cout<<f[i][j]<<" ";
cout<<endl;
}*/
if(m>=xmax&&m>=ymax)//这个判断应该不用加
{
cout<<f[xmax][ymax];
return 0;
}
int responsible=-1e9;
for(int i=m;i<=xmax;++i)
{
for(int j=m;j<=ymax;++j)
{
responsible=q(responsible,f[i][j]-f[i-m][j]-f[i][j-m]+f[i-m][j-m]);
}
}
cout<<responsible;
return 0;
}
不清楚错在哪,求大佬帮忙