http://tyvj.cn/p/1884
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 51;
int f[maxn][maxn][maxn][maxn];
//设f[i][j][h][k]为第一条路径到达map[i][j], 第二条路径到达map[h][k]时的最优值
/* f[i][j][h][k] = max{f[i][j][h][k],
f[i-1][j][h-1][k],
f[i-1][j][h][k-1],
f[i][j-1][h-1][k],
f[i][j-1][h][k-1]} + (当i=j&&h=k时: map[i][j])(否则: map[i][j]+map[h][k])*/
int map[110][110];
int main ()
{
int n;
scanf("%d", &n);
memset(map, 0, sizeof(map));
int x, y, p;
while (scanf("%d%d%d", &x, &y, &p) && (x || y || p)) {
map[x][y] = p;
}
memset(f, 0, sizeof(f));
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
for (int h=1;h<=n;h++)
for (int k=1;k<=n;k++)
{
f[i][j][h][k] = max(f[i][j][h][k], f[i-1][j][h-1][k]);
f[i][j][h][k] = max(f[i][j][h][k], f[i-1][j][h][k-1]);
f[i][j][h][k] = max(f[i][j][h][k], f[i][j-1][h-1][k]);
f[i][j][h][k] = max(f[i][j][h][k], f[i][j-1][h][k-1]);
f[i][j][h][k] += map[i][j];
if (i!=h && j!=k) f[i][j][h][k] += map[h][k];
}
printf("%d\n", f[n][n][n][n]);
return 0;
}