C - Remembering the Days next_permutation解法

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N,M;
    cin>>N>>M;

    VV A(N, VI(N, -1));

    rep(_,M){
      ll a,b,c;cin>>a>>b>>c;
      a--;b--;
      A[a][b]=c;
      A[b][a]=c;
    }

    ll ma=0;
    VI I;
    rep(i,N)I.push_back(i);

    do{
      // Iの順に行く
      ll sum=0;
      rep(i,N-1){
        ll from = I[i];
        ll to = I[i+1];
        if(A[from][to]==-1){
          sum=-inf;
        }else{
          sum += A[from][to];
        }
        chmax(ma,sum); // 毎回取る
      }
    }while(next_permutation(ALL(I)));

    p(ma);
   
    return 0;
}