代码:
#include <bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
int n, m;
int x, y, v[5005][5005] = {0}, a[5005][5005] = {0}, maxx = 0, maxy = 0;
int read() {
int x = 0, sign = 1;
char ch = getchar_unlocked();
for (; !isdigit(ch); ch = getchar_unlocked()) if (ch == '-') sign = -1;
for (; isdigit(ch); ch = getchar_unlocked()) x = x * 10 + ch - '0';
return x * sign;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
n = read();
m = read();
for (int i = 1; i <= n; i++) {
x = read() + 1;
y = read() + 1;
a[x][y] += read();
v[x][y] = v[x - 1][y] + v[x][y - 1] - v[x - 1][y - 1] + a[x][y];
maxx = std::max(maxx, x);
maxy = std::max(maxy, y);
}
if (m > maxx) maxx = m;
if (m > maxy) maxy = m;
int ans = 0;
for (int i = m; i <= maxx + 1 - m; i++) {
for (int j = m; j <= maxy + 1 - m; j++) {
int cur = v[i][j] - v[i - m][j] - v[i][j - m] + v[i - m][j - m];
ans = std::max(ans, cur);
}
}
printf("%d", ans);
return 0;
}