https://www.luogu.org/problemnew/show/P2280
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,r,dp[5101][5101];
void read(int &n)
{
char c='+';int x=0;bool flag=0;
while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
while(c>='0'&&c<='9')
{x=x*10+c-48;c=getchar();}
flag==1?n=-x:n=x;
}
int ans=0;
int main()
{
read(n);read(r);
for(int i=1;i<=n;i++)
{
int x,y,z;
read(x);read(y);read(z);
dp[x+1][y+1]=z;
}
for(int i=1;i<=5001;i++)
for(int j=1;j<=5001;j++)
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+dp[i][j];
for(int i=0;i<=5001-r;i++)
for(int j=0;j<=5001-r;j++)
ans=max(ans,dp[i+r][j+r]-dp[i+r][j]-dp[i][j+r]+dp[i][j]);
printf("%d",ans);
return 0;
}