#include<bits/stdc++.h>
//#define int  long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP     ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe  int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define mp make_pair
#define checktime   cerr << (double)clock() / CLOCKS_PER_SEC * 1000  << " ms";
using namespace std;
#define getchar_unlocked getchar
inline int scan()
{
    char c = getchar_unlocked();
    int x = 0;
    while (c < '0' || c > '9')
    {
        c = getchar_unlocked();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x << 1) + (x << 3) + c - '0';
        c = getchar_unlocked();
    }
    return x;
}
void phongbeo();
const int inf = 1e9;
const ll mod2 = 998244353;
const int  mod1 = 998244353;
const ll base=67;
int add(int x,int y)
{
    if(x+y>=mod2) x-=mod2;
    if(x+y<0)x+=mod2;
    return x+y;
}
struct icd
{
    long double a;
    int b;
};
struct ib
{
    int a;
    int b;
};
struct ic
{
    int a, b, c;
};
struct id
{
    int a, b, c, d;
};
struct ie
{
    int a, b, c, d, e;
};
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int  i, s10, s12,k1,k2,k3,s11,lim,w,l,r ;
int kk;
int el = 19;
main()
{
    if(fopen(task".inp","r"))
    {
        fin(task);
        fou(task);
    }
    NHP
    /// cin>>s1;
    // modwwe
    phongbeo();
    // checktime
}
int c[510];
int a[510];
int dp[505][505][505][2];
int v[510];
bool check(int x,int y)
{
    if(x==0||y==0) return 1;
    if(x>n||y>n) return 0;
    if(c[x]==c[y]||a[x]==a[y]) return 1;
    return 0;
}
void phongbeo()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>c[i]>>a[i]>>v[i];
    for(int i=0; i<=n; i++)
        for(int j=i+1; j<=n+1; j++)
            for(int z=i+2; z<=n+2; z++)
                for(int f=0; f<=1; f++)
                    dp[i][j][z][f]=-inf;
    s4=max(v[1],v[3]);
    if(check(1,2))
        dp[2][3][4][1]=v[1];
    if(check(1,4))
        dp[2][3][4][0]=v[1];
    if(check(3,4))
        dp[1][2][4][0]=v[3];
    if(check(3,1))
        dp[1][2][4][1]=v[3];
    for(int i=1; i<=n; i++)
    {
        for(int j=i+1; j<=n; j++)
            for(int z=j+1; z<=n+1; z++)
            {
                if(check(z,z+1))
                    dp[i][j][z+1][0]=max(dp[i][j][z+1][0],dp[i][j][z][0]+v[z]);
                if(check(i,z))
                    dp[i][j][z+1][1]=max(dp[i][j][z+1][1],dp[i][j][z][0]+v[z]);
                if(check(i,j))
                    dp[j][z][z+1][1]=max(dp[j][z][z+1][1],dp[i][j][z][1]+v[i]);
                if(check(i,z+1))
                    dp[j][z][z+1][0]=max(dp[j][z][z+1][0],dp[i][j][z][1]+v[i]);
            }
    }
    for(int i=0; i<=n; i++)
        for(int j=i+1; j<=n+1; j++)
            for(int z=j+1; z<=n+2; z++)
                for(int f=0; f<=1; f++)
                {
                    int cc=v[z];
                    if(f) cc=v[i];
                    s4=max(s4,dp[i][j][z][f]+cc);
                }
    cout<<s4;
}
Compilation message (stderr)
cardgame2.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   73 | main()
      | ^~~~
cardgame2.cpp: In function 'int main()':
cardgame2.cpp:11:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
cardgame2.cpp:77:9: note: in expansion of macro 'fin'
   77 |         fin(task);
      |         ^~~
cardgame2.cpp:12:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define fou(x) freopen(x".ans","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
cardgame2.cpp:78:9: note: in expansion of macro 'fou'
   78 |         fou(task);
      |         ^~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |