10分
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <functional>
#include <climits>
#define quickio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
using namespace std;
typedef long long ll;
//const int N = 1e4 + 5;
const int M = 5e3 + 5;
//struct zb
//{
// int x;
// int y;
// int w;
//}zb[N];
int S[M][M];
void solve()
{
int n, m;
cin >> n >> m;
int x, y, w;
int maxx = 0, maxy = 0;
for (int i = 0; i < n; i++)
{
//cin >> zb[i].x >> zb[i].y >> zb[i].w;
cin >> x >> y >> w;
x++;
y++;
if (x > maxx)
maxx = x;
if (y > maxy)
maxy = y;
//S[x][y] = w;//错了!!!!!!!!!!,可能两个目标在同一位置啊,看题啊!!!!!!!!!!!!!11
//可能存在多个目标在同一位置上的情况。
S[x][y] += w;
}
int Max = 0;
for (int i = 1; i <= maxx; i++)
{
for (int j = 1; j <= maxy; j++)
{
S[x][y] += S[x - 1][y] + S[x][y - 1] - S[x - 1][y - 1];
}
}
for (int i = m/**/; i <= maxx; i++)
{
for (int j = m/**/; j <= maxy; j++)
{
int num = S[i][j] - S[i - m][j] - S[i][j - m] + S[i - m][j - m];
if (num > Max)
Max = num;
}
}
cout << Max << endl;
}
int main()
{
solve();
return 0;
}