Description:
Accept:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
struct Node
{
int a, b, h;
Node(){a=0, b=0, h=0;}
}node[maxn];
int n, len=0, g[maxn][maxn], dp[maxn];
void beg(int x, int y, int z)
{
if(x>y)swap(x, y);
node[len].a=x;
node[len].b=y;
node[len].h=z;
len++;
}
int f(int ind)
{
if(ind==len)return 0;
if(dp[ind]!=-1)return dp[ind];
int t[maxn], maxx=-1;
memset(t, 0, sizeof(t));
for(int i=0;i<len;i++)
{
if(g[ind][i])
t[i]=f(i)+node[i].h;
}
for(int i=0;i<len;i++)maxx=max(maxx, t[i]);
dp[ind]=maxx;
return maxx;
}
int main()
{
while (cin>>n)
{
len=0;
memset(g, 0, sizeof(g));
memset(dp, -1, sizeof(dp));
for(int i=0;i<n;i++)
{
int p, q, r;
cin>>p>>q>>r;
beg(p, q, r);
beg(p, r, q);
beg(q, r, p);
}
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
if(node[i].a<node[j].a&&node[i].b<node[j].b)g[i][j]=1;
}
}
// for(int i=0;i<len;i++)
// {
// for(int j=0;j<len;j++)
// {
// cout<<g[i][j]<<" ";
// }
// cout<<endl;
// }
// for(int i=0;i<len;i++)
// cout<<node[i].a<<" "<<node[i].b<<" "<<node[i].h<<endl;
// cout<<endl;
int maxx=-1;
for(int i=0;i<len;i++)
{
int tmp=f(i)+node[i].h;
maxx=max(maxx, tmp);
}
cout<<maxx<<endl;
}
return 0;
}