Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 58;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n, m;
vvi g;

int dfs(int I, vi a) {
  int res = 0, S = a.size();
  bool b = 1;
  if (S >= 2) {
    int sum = 0;
    for (int i = 0; i < S; i++) {
      int M = inf;
      for (int j = 0; j < S; j++)
        if (i != j) {
          int c = g[a[i]][a[j]];
          if (c == 0)
            b = 0;
          M = min(M, c);
        }
      sum += M;
    }
    res = max(res, sum);
  }
  if (b && S < 14) {
    for (int i = I; i < n; i++) {
      a.push_back(i);
      res = max(res, dfs(i + 1, a));
      a.pop_back();
    }
  }
  return res;
}

int main() {
  cin >> n >> m;
  g = vvi(n, vi(n));
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    g[u][v] = c;
    g[v][u] = c;
  }
  vi a;
  cout << dfs(0, a) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 58;
const double pi = acos(-1);
const double eps = 1e-7;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n, m;
vvi g;

int dfs(int I, vi a) {
  int res = 0, S = a.size();
  if (S >= 2) {
    int sum = 0;
    for (int i = 0; i < S; i++) {
      int M = inf;
      for (int j = 0; j < S; j++)
        if (i != j)
          M = min(M, g[a[i]][a[j]]);
      sum += M;
    }
    if (S == 3 && a[0] == 6 && a[1] == 42 && a[2] == 72)
      cout << sum << endl;
    res = max(res, sum);
  }
  if (S < 14) {
    for (int i = I; i < n; i++) {
      a.push_back(i);
      res = max(res, dfs(i + 1, a));
      a.pop_back();
    }
  }
  return res;
}

int main() {
  cin >> n >> m;
  g = vvi(n, vi(n));
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    g[u][v] = c;
    g[v][u] = c;
  }
  vi a;
  cout << dfs(0, a) << endl;
}