Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF (1LL << 60)
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(kbrni,n)cout<<" "<<a[kbrni];cout<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define svecp(v) cout<<#v<<":";each(kbrni,v)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 2005;

struct edge
{
    int to,kind;
};

vector<edge> G[MAX_N];
bool visit[MAX_N];

void dfs(vi& fac,int u,int p,bool& loop)
{
    visit[u] = true;
    rep(i,len(G[u])){
        int v = G[u][i].to;
        if(v != p){
            if(!visit[v]){
                fac.pb(G[u][i].kind);
                dfs(fac,v,u,loop);
            }else if(!loop){
                fac.pb(G[u][i].kind);
                loop = true;
            }
        }
    }
}

ll dp1[MAX_N][MAX_N][2];
ll dp2[MAX_N][MAX_N][2][2];
ll res[MAX_N][MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m,K;
    cin >> n >> m >> K;
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        --a,--b;
        G[a].pb((edge){b,c}),G[b].pb((edge){a,c});
    }
    vvl vec;
    rep(i,n){
        if(!visit[i] && len(G[i]) <= 2){
            if(len(G[i]) == 0){
                visit[i] = true;
                vl hoge = {0LL,0LL};
                vec.pb(hoge);
                continue;
            }
            vector<int> fac;
            bool loop = false;
            dfs(fac,i,-1,loop);
            // svec(fac);
            if(loop){
                int sz = len(fac);
                rep(i,sz){
                    rep(j,sz+1){
                        rep(k,2){
                            rep(l,2){
                                dp2[i][j][k][l] = -INF;
                            }
                        }
                    }
                }
                dp2[0][0][0][0] = dp2[0][1][1][1] = 0;
                rep(i,sz-1){
                    rep(j,i+2){
                        rep(k,2){
                            rep(l,2){
                                if(i == sz-2){
                                    if(k == 1 && l == 1){
                                        if(fac[i] != 0 && fac[i+1] != 0){
                                            cmx(dp2[i+1][j+1][1][l],dp2[i][j][k][l]+fac[i]+fac[i+1]);
                                            // cout << i << " " << j << " " << l << " " << dp2[i+1][j+1][1][l] << "\n";
                                        }
                                    }else{
                                        cmx(dp2[i+1][j][0][l],dp2[i][j][k][l]);
                                        if(k == 0){
                                            cmx(dp2[i+1][j+1][1][l],dp2[i][j][k][l]);
                                        }else{
                                            if(fac[i] != 0){
                                                cmx(dp2[i+1][j+1][1][l],dp2[i][j][k][l]+fac[i]);
                                            }
                                        }
                                    }
                                }else{
                                    cmx(dp2[i+1][j][0][l],dp2[i][j][k][l]);
                                    if(k == 0){
                                        cmx(dp2[i+1][j+1][1][l],dp2[i][j][k][l]);
                                    }else{
                                        if(fac[i] != 0){
                                            cmx(dp2[i+1][j+1][1][l],dp2[i][j][k][l]+fac[i]);
                                            // cout << i << " " << j << " " << l << " " << dp2[i][j][k][l] << " " << dp2[i+1][j+1][1][l] << "\n";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                vl hoge;
                rep(j,sz+1){
                    ll mx = -INF;
                    rep(k,2){
                        rep(l,2){
                            cmx(mx,dp2[sz-1][j][k][l]);
                        }
                    }
                    hoge.pb(mx);
                }
                // svec(hoge);
                vec.pb(hoge);
            }else{
                int sz = len(fac);
                rep(i,sz+1){
                    rep(j,sz+2){
                        rep(k,2){
                            dp1[i][j][k] = -INF;
                        }
                    }
                }
                dp1[0][0][0] = dp1[0][1][1] = 0;
                rep(i,sz){
                    rep(j,i+2){
                        rep(k,2){
                            // if(i == sz-1 && j == 2 && k == 1){
                            //     cout << i << " " << j << " " << k << " " << dp1[i+1][j+1][1] << " " << dp1[i][j][k] << "\n";
                            // }
                            cmx(dp1[i+1][j][0],dp1[i][j][k]);
                            if(k == 0){
                                cmx(dp1[i+1][j+1][1],dp1[i][j][k]);
                            }else{
                                if(fac[i] != 0){
                                    cmx(dp1[i+1][j+1][1],dp1[i][j][k]+fac[i]);
                                    // cout << i << " " << j << " " << k << " " << dp1[i+1][j+1][k] << "\n";
                                }
                            }
                        }
                    }
                }
                vl hoge;
                rep(j,sz+2){
                    ll mx = -INF;
                    rep(k,2){
                        // if(j == sz+1){
                        //     show(dp1[sz][j][k]);
                        // }
                        cmx(mx,dp1[sz][j][k]);
                    }
                    hoge.pb(mx);
                }
                vec.pb(hoge);
            }
        }
    }
    int sz = len(vec);
    rep(i,sz+1){
        rep(j,K+1){
            res[i][j] = -INF;
        }
    }
    res[0][0] = 0;
    rep(i,sz){
        rep(j,K+1){
            rep(k,min(len(vec[i]),K+1-j)){
                cmx(res[i+1][j+k],res[i][j]+vec[i][k]);
            }
        }
    }
    if(res[sz][K] < -(1LL << 50)){
        cout << "Impossible\n";
    }else{
        cout << res[sz][K] << "\n";
    }
    return 0;
}



Compiler output:
a.cc: In function 'void dfs(vi&, int, int, bool&)':
a.cc:50:5: error: reference to 'visit' is ambiguous
   50 |     visit[u] = true;
      |     ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:46:6: note:                 'bool visit [2005]'
   46 | bool visit[MAX_N];
      |      ^~~~~
a.cc:54:17: error: reference to 'visit' is ambiguous
   54 |             if(!visit[v]){
      |                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:46:6: note:                 'bool visit [2005]'
   46 | bool visit[MAX_N];
      |      ^~~~~
a.cc: In function 'int main()':
a.cc:83:13: error: reference to 'visit' is ambiguous
   83 |         if(!visit[i] && len(G[i]) <= 2){
      |             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:46:6: note:                 'bool visit [2005]'
   46 | bool visit[MAX_N];
      |      ^~~~~
a.cc:85:17: error: reference to 'visit' is ambiguous
   85 |                 visit[i] = true;
      |                 ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:46:6: note:                 'bool visit [2005]'
   46 | bool visit[MAX_N];
      |      ^~~~~