坑点: 数组只能开5000 * 5000, 故前缀和只能在原数组上求…
/**
* author: roccoshi
* created: 2021-07-19 16:41:16
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int pi = 3.1415927;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int ninf = 0xc0c0c0c0;
const int maxn = 5000 + 5;
const int maxm = 200000 + 5;
int limit = 5001;
int g[maxn][maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, r;
cin >> n >> r;
int x, y, w;
while (n--) {
cin >> x >> y >> w;
g[x + 1][y + 1] += w;
}
// 求前缀和
for (int i = 1; i <= limit + 1; ++i) {
for (int j = 1; j <= limit + 1; ++j) {
g[i][j] += g[i - 1][j] + g[i][j - 1] -g[i - 1][j - 1];
}
}
if (r > limit) r = limit;
int ans = 0;
for (int i = r; i <= limit; ++i) {
for (int j = r; j <= limit; ++j) {
ans = max(ans, g[i][j] - g[i][j - r] - g[i - r][j] + g[i - r][j - r]);
}
}
cout << ans << endl;
return 0;
}