// 河南省实验中学 高中 21级 关天泽
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define re register
const int INF = 0x3f3f3f3f;
using namespace std;
using namespace std;
int n, ans = -INF;
int x[1010], y[1010], c[1010];
vector<int> d[1010];
int nc[1010];
int flag[1010];
int dfs(int v){
flag[v] = 1;
nc[v] = c[v];
for(re int i = 0; i < d[v].size(); i++){
int u = d[v][i];
if(flag[u]) continue;
dfs(u);
nc[v] += max(nc[u], 0);
ans = max(ans, nc[v]);
}
}
int main(){
freopen("subset.in", "r", stdin);
freopen("subset.out", "w", stdout);
cin >> n;
for(re int i = 0; i < n ;i++){
cin >> x[i] >> y[i] >> c[i];
}
for(re int i = 0; i < n; i++){
for(re int j = 0; j < n; j++){
if(i == j)continue;
if(fabs(x[i] - x[j]) + fabs(y[i] - y[j]) > 1)continue;
d[i].push_back(j);
d[j].push_back(i);
}
}
dfs(0);
cout << ans << endl;
}