Problem p03664 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
// typedef complex<D> P;
#define F first
#define S second
const ll MOD = 1000000007;
// const ll MOD=998244353;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  int i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;
  const int sz = 1 << N;
  vector<vector<int>> edge(N, vector<int>(N, 0));
  vector<vector<bool>> edge2(N, vector<bool>(N, false));
  ll uku = 0;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    uku += c;
    a--;
    b--;
    edge[a][b] = c;
    edge[b][a] = c;
    edge2[a][b] = true;
    edge2[b][a] = true;
  }
  vector<ll> sum(sz, 0);
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        if ((i >> j & 1) && (i >> k & 1)) {
          sum[i] += edge[j][k];
        }
      }
    }
  }
  const ll INF = 1e15;
  const int msk = sz - 1;
  vector<vector<ll>> dp(N, vector<ll>(sz, -INF));
  for (int i = 1; i < sz; i += 2) {
    dp[0][i] = sum[i];
  }
  for (int i = 0; i < sz; i++) {
    int all = msk ^ i;
    for (int k = all; k > 0; k = (k - 1) & all) {
      assert((i & k) == 0);
      for (int u = 0; u < N; u++) {
        if ((i >> u & 1) == 0) {
          continue;
        }
        for (int v = 0; v < N; v++) {
          if ((k >> v & 1) && edge2[u][v]) {
            dp[v][i | k] = max(dp[v][i | k], dp[u][i] + sum[k] + edge[u][v]);
          }
        }
      }
    }
  }
  // for(auto &I:dp){for(auto &J:I){cout<<" "<<(J<0?-1:J);}cout<<endl;}
  cout << uku - dp.back().back() << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
// typedef complex<D> P;
#define F first
#define S second
const ll MOD = 1000000007;
// const ll MOD=998244353;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  int i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M;
  cin >> N >> M;
  const int sz = 1 << N;
  vector<vector<int>> edge(N, vector<int>(N, 0));
  vector<vector<bool>> edge2(N, vector<bool>(N, false));
  ll uku = 0;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    uku += c;
    a--;
    b--;
    edge[a][b] = c;
    edge[b][a] = c;
    edge2[a][b] = true;
    edge2[b][a] = true;
  }
  vector<ll> sum(sz, 0);
  for (int i = 0; i < sz; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = j + 1; k < N; k++) {
        if ((i >> j & 1) && (i >> k & 1)) {
          sum[i] += edge[j][k];
        }
      }
    }
  }
  const ll INF = 1e15;
  const int msk = sz - 1;
  vector<vector<ll>> dp(N, vector<ll>(sz, -INF));
  for (int i = 1; i < sz; i += 2) {
    dp[0][i] = sum[i];
  }
  for (int i = 0; i < sz; i++) {
    int all = msk ^ i;
    for (int u = 0; u < N; u++) {
      if ((i >> u & 1) == 0) {
        continue;
      }
      for (int k = all; k > 0; k = (k - 1) & all) {
        for (int v = 0; v < N; v++) {
          if ((k >> v & 1) && edge2[u][v]) {
            dp[v][i | k] = max(dp[v][i | k], dp[u][i] + sum[k] + edge[u][v]);
          }
        }
      }
    }
  }
  // for(auto &I:dp){for(auto &J:I){cout<<" "<<(J<0?-1:J);}cout<<endl;}
  cout << uku - dp.back().back() << endl;

  return 0;
}