Problem p03664 (C++)

Buggy submission (status: Runtime Error):
/*input
4 6
1 2 100
3 1 100
2 4 100
4 3 100
1 4 100
3 2 100
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}

//}}}
const int maxn = 15;
const int INF = 0x3f3f3f3f;

int es[1 << maxn];
int adj[maxn][maxn];
int dp[maxn][1 << maxn];
int main() {
  IOS;
  int n, m;
  cin >> n >> m;
  ll tt = 0;
  REP(i, m) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    adj[u][v] = adj[v][u] = d;
    tt += d;
  }
  for (int i = 1; i < (1 << n); i++) {
    int kk = i & -i;
    if ((kk ^ i) != i - kk)
      exit(0);
    int tmp = __lg(i & -i);
    // if(tmp>=n) exit(0);
    int meow = i ^ (1 << tmp);
    es[i] = es[i ^ (1 << tmp)];
    for (int j = tmp + 1; j < n; j++)
      if (i & (1 << j)) {
        es[i] += adj[tmp][j];
      }
  }
  REP(i, n) REP(j, 1 << n) dp[i][j] = -INF;
  REP(mask, 1 << n) for (int i = 0; i < n; i++) if (mask & (1 << i)) {
    if (i == 0)
      dp[i][mask] = es[mask];
    else {
      int pre = mask ^ (1 << i);
      for (int sub = pre; sub > 0; sub = (sub - 1) & pre) {
        REP(j, n) if (sub & (1 << j)) {
          if (adj[j][i])
            dp[i][mask] =
                max(dp[i][mask], dp[j][sub] + es[sub ^ mask] + adj[j][i]);
        }
      }
    }
  }
  cout << tt - dp[n - 1][(1 << n) - 1] << '\n';
  return 0;
}


Fixed submission:
/*input
4 6
1 2 100
3 1 100
2 4 100
4 3 100
1 4 100
3 2 100
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}

//}}}
const int maxn = 15;
const int INF = 0x3f3f3f3f;

int es[1 << maxn];
int adj[maxn][maxn];
int dp[maxn][1 << maxn];
int main() {
  IOS;
  int n, m;
  cin >> n >> m;
  ll tt = 0;
  REP(i, m) {
    int u, v, d;
    cin >> u >> v >> d;
    u--;
    v--;
    adj[u][v] = adj[v][u] = d;
    tt += d;
  }
  for (int i = 1; i < (1 << n); i++) {
    int kk = i & -i;

    int tmp = 0;
    while (kk % 2 == 0) {
      kk /= 2;
      tmp++;
    }
    // if(tmp>=n) exit(0);
    int meow = i ^ (1 << tmp);
    es[i] = es[i ^ (1 << tmp)];
    for (int j = tmp + 1; j < n; j++)
      if (i & (1 << j)) {
        es[i] += adj[tmp][j];
      }
  }
  REP(i, n) REP(j, 1 << n) dp[i][j] = -INF;
  REP(mask, 1 << n) for (int i = 0; i < n; i++) if (mask & (1 << i)) {
    if (i == 0)
      dp[i][mask] = es[mask];
    else {
      int pre = mask ^ (1 << i);
      for (int sub = pre; sub > 0; sub = (sub - 1) & pre) {
        REP(j, n) if (sub & (1 << j)) {
          if (adj[j][i])
            dp[i][mask] =
                max(dp[i][mask], dp[j][sub] + es[sub ^ mask] + adj[j][i]);
        }
      }
    }
  }
  cout << tt - dp[n - 1][(1 << n) - 1] << '\n';
  return 0;
}