alexjercan/bugnet huggingface dataset:
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5;
const double INF = 1e9;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  vector<vector<double>> dp(N + 1, vector<double>(MAX, INF));
  for (int i = 1; i < MAX; i++) {
    dp[0][i] = 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < MAX; j++) {
      for (int k = j; k < MAX; k += j) {
        dp[i + 1][k] =
            min(dp[i + 1][k], max(dp[i][j], (double)abs(k - a[i]) / a[i]));
      }
    }
  }
  double res = INF;
  for (int i = 1; i < MAX; i++) {
    res = min(res, dp[N][i]);
  }
  cout << fixed << setprecision(10) << res << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5;
using ld = long double;
const ld INF = 1e9;

int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  vector<vector<ld>> dp(N + 1, vector<ld>(MAX, INF));
  for (int i = 1; i < MAX; i++) {
    dp[0][i] = 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < MAX; j++) {
      for (int k = j; k < MAX; k += j) {
        dp[i + 1][k] =
            min(dp[i + 1][k], max(dp[i][j], (ld)abs(k - a[i]) / a[i]));
      }
    }
  }
  ld res = INF;
  for (int i = 1; i < MAX; i++) {
    res = min(res, dp[N][i]);
  }
  cout << fixed << setprecision(10) << res << endl;
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 200001;
double dp[20][MAX];

int main() {
  int N;
  cin >> N;

  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < MAX; ++j)
      dp[i][j] = -1;
  }

  for (int i = 0; i < N; ++i) {
    for (int j = 1; j < MAX; ++j) {
      dp[i][j] = max(dp[i][j], fabs(a[i] - j) / (double)a[i]);
      // cout << i << ' ' << j << ' ' << dp[i][j] << endl;

      if (i + 1 < N) {
        for (int k = j; k < MAX; k += j) {
          if (dp[i + 1][k] < 0 || dp[i + 1][k] > dp[i][j]) {
            dp[i + 1][k] = dp[i][j];
          }
        }
        for (int k = j; k >= 0; k -= j) {
          if (dp[i + 1][k] < 0 || dp[i + 1][k] > dp[i][j]) {
            dp[i + 1][k] = dp[i][j];
          }
        }
      }
    }
  }

  double ans = 1e10;
  for (int i = 1; i < MAX; ++i) {
    ans = min(ans, dp[N - 1][i]);
  }

  printf("%.8f\\n", ans);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 200000;
double dp[20][MAX];
bool valid[20][MAX];

int main() {
  int N;
  cin >> N;

  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];

  for (int i = 0; i < N; ++i) {
    for (int j = 1; j < MAX; ++j) {
      dp[i][j] = max(dp[i][j], fabs(a[i] - j) / (double)a[i]);
      valid[i][j] = 1;
      // cout << i << ' ' << j << ' ' << dp[i][j] << endl;

      if (i + 1 < N) {
        for (int k = j; k < MAX; k += j) {
          if (!valid[i + 1][k] || dp[i + 1][k] > dp[i][j]) {
            dp[i + 1][k] = dp[i][j];
            valid[i + 1][k] = 1;
          }
        }
        for (int k = j; k >= 0; k -= j) {
          if (!valid[i + 1][k] || dp[i + 1][k] > dp[i][j]) {
            dp[i + 1][k] = dp[i][j];
            valid[i + 1][k] = 1;
          }
        }
      }
    }
  }

  double ans = 1e10;
  for (int i = 1; i < MAX; ++i) {
    ans = min(ans, dp[N - 1][i]);
  }

  printf("%.8f\\n", ans);

  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX 100
#define INF (1 << 29)

int N, cost[MAX][MAX];

int get_sat(set<int> &inv) {
  int sat = 0;
  for (auto &i : inv) {
    int s = INF;
    for (auto &j : inv) {
      if (i == j)
        continue;
      s = min(s, cost[i][j]);
    }
    sat += s;
  }
  return sat;
}

map<set<int>, int> memo;

int dfs(set<int> inv) {
  int res = 0;
  if (memo.count(inv) > 0) {
    return memo[inv];
  }

  if (inv.size() >= 2) {
    res = get_sat(inv);
  }
  for (int i = 0; i < N; i++) {
    bool ok = 1;
    for (auto &j : inv) {
      if (cost[i][j] == 0) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      set<int> ninv = inv;
      ninv.insert(i);
      res = max(res, dfs(ninv));
    }
  }
  return (memo[inv] = res);
}

int main() {
  int M, a, b, c;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    cost[a][b] = cost[b][a] = c;
  }
  cout << dfs(set<int>()) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 100
#define INF (1 << 29)

int N, cost[MAX][MAX];

int get_sat(set<int> &inv) {
  int sat = 0;
  for (auto &i : inv) {
    int s = INF;
    for (auto &j : inv) {
      if (i == j)
        continue;
      s = min(s, cost[i][j]);
    }
    sat += s;
  }
  return sat;
}

int dfs(set<int> inv) {
  int res = 0;
  if (inv.size() >= 2) {
    res = get_sat(inv);
  }
  for (int i = 0; i < N; i++) {
    bool ok = 1;
    for (auto &j : inv) {
      if (cost[i][j] == 0) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      set<int> ninv = inv;
      ninv.insert(i);
      res = max(res, dfs(ninv));
    }
  }
  return res;
}

int main() {
  int M, a, b, c;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    cost[a][b] = cost[b][a] = c;
  }
  cout << dfs(set<int>()) << endl;
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> dim(N);
  vector<vector<int>> edge(N, vector<int>(N, 0));
  for (int i = 0; i < M; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    edge[u][v] = edge[v][u] = c;
    dim[u]++, dim[v]++;
  }
  int ans = 0;
  for (int s = 2; s <= 14; s++) {
    vector<int> cand;
    for (int i = 0; i < N; i++) {
      if (dim[i] >= s - 1) {
        cand.push_back(i);
      }
    }
    if (cand.size() < s) {
      break;
    }
    const int sz = cand.size();
    vector<bool> mask(sz, false);
    fill(mask.end() - s, mask.end(), true);
    auto score = [&](const vector<int> &node) {
      int ans = 0;
      for (const int i : node) {
        int mini = 1 << 30;
        for (const int j : node) {
          if (i == j) {
            continue;
          }
          if (edge[i][j] == 0) {
            return 0;
          }
          mini = min(mini, edge[i][j]);
        }
        ans += mini;
      }
      return ans;
    };
    do {
      vector<int> node;
      for (int i = 0; i < sz; i++) {
        if (mask[i]) {
          node.push_back(cand[i]);
        }
      }
      ans = max(ans, score(node));
    } while (next_permutation(mask.begin(), mask.end()));
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> dim(N);
  vector<vector<int>> edge(N, vector<int>(N, 0));
  for (int i = 0; i < M; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    edge[u][v] = edge[v][u] = c;
    dim[u]++, dim[v]++;
  }
  int ans = 0;
  for (int s = 2; s <= 14; s++) {
    vector<int> cand;
    for (int i = 0; i < N; i++) {
      if (dim[i] >= s - 1) {
        cand.push_back(i);
      }
    }
    if (cand.size() < s) {
      break;
    }
    vector<bool> mask(N, false);
    fill(mask.end() - s, mask.end(), true);
    auto score = [&](const vector<int> &node) {
      int ans = 0;
      for (const int i : node) {
        int mini = 1 << 30;
        for (const int j : node) {
          if (i == j) {
            continue;
          }
          if (edge[i][j] == 0) {
            return 0;
          }
          mini = min(mini, edge[i][j]);
        }
        ans += mini;
      }
      return ans;
    };
    do {
      vector<int> node;
      for (int i = 0; i < N; i++) {
        if (mask[i]) {
          node.push_back(cand[i]);
        }
      }
      ans = max(ans, score(node));
    } while (next_permutation(mask.begin(), mask.end()));
  }
  cout << ans << endl;
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdlib.h>
#include <vector>
#define inf 100000000

using namespace std;

int n, m;
int g[105][105];

bool used[105];
vector<int> vec;
int table[20][20];

int calc() {
  int mi, ret = 0;
  for (int i = 0; i < vec.size(); i++) {
    mi = inf;
    for (int j = 0; j < vec.size(); j++) {
      if (i == j)
        continue;
      mi = min(mi, g[vec[i]][vec[j]]);
    }
    if (mi == inf)
      mi = 0;
    ret += mi;
  }
  return ret;
}

int dfs() {
  int ret = calc();
  int mi = inf;

  if (vec.size() >= 14)
    return ret;
  for (int i = vec.back() + 1; i <= n; i++) {
    for (int j = 0; j < vec.size(); j++) {
      if (g[vec[j]][i] == 0)
        goto NG;
    }
    vec.push_back(i);
    ret = max(ret, dfs());
    vec.pop_back();
  NG:;
  }
  return ret;
}

int main(void) {
  cin >> n >> m;
  int u, v, f;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> f;
    g[u][v] = g[v][u] = f;
  }

  for (int i = 0; i <= 19; i++) {
    for (int j = 0; j <= 19; j++) {
      table[i][j] = inf;
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    vec.push_back(i);
    ans = max(ans, dfs());
    vec.pop_back();
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
#include <vector>
#define inf 100000000

using namespace std;

int n, m;
vector<int> G[105];
int g[105][105];

bool used[105];
vector<int> vec;
int table[15][15];

int calc(int r) {
  int ret = 0;
  for (int i = 1; i <= r; i++) {
    if (table[r][i] != inf)
      ret += table[r][i];
  }
  return ret;
}

int dfs(int v, int num) {
  if (num >= 14)
    return 0;

  for (int i = 0; i < vec.size(); i++) {
    if (g[vec[i]][v] == 0)
      return 0;
  }
  vec.push_back(v);
  used[v] = true;

  int mi = inf;
  for (int i = 1; i <= num; i++) {
    table[num + 1][i] = min(table[num][i], g[vec[i - 1]][v]);
    mi = min(mi, g[vec[i - 1]][v]);
  }
  table[num + 1][num + 1] = mi;

  int ret = 0;
  for (int i = 0; i < G[v].size(); i++) {
    if (used[G[v][i]])
      continue;
    ret = max(ret, dfs(G[v][i], num + 1));
  }
  ret = max(ret, calc(num + 1));
  vec.pop_back();
  used[v] = false;

  return ret;
}

int main(void) {
  cin >> n >> m;
  int u, v, f;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> f;
    G[u].push_back(v);
    G[v].push_back(u);
    g[u][v] = g[v][u] = f;
  }

  for (int i = 0; i <= 14; i++) {
    for (int j = 0; j <= 14; j++) {
      table[i][j] = inf;
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i, 0));
  }

  cout << ans << endl;
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int m[110][110];
int now[110];
int n;
int dfs(int a, int b) {
  if (n == a) {
    if (b < 2)
      return 0;
    int val = 0;
    for (int i = 0; i < b; i++) {
      int t = 9999999;
      for (int j = 0; j < b; j++) {
        if (i != j)
          t = min(t, m[now[i]][now[j]]);
      }
      val += t;
    }
    return val;
  }
  int ret = dfs(a + 1, b);
  bool ok = true;
  for (int i = 0; i < b; i++)
    if (!m[now[i]][a])
      ok = false;
  if (ok) {
    now[b] = a;
    ret = max(ret, dfs(a + 1, b + 1));
  }
  return ret;
}
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  n = a;
  for (int i = 0; i < b; i++) {
    int p, q, r;
    scanf("%d%d%d", &p, &q, &r);
    p--;
    q--;
    m[p][q] = m[q][p] = r;
  }
  printf("%d\\n", dfs(0, 0));
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int ret;
int g[100][100];
int use[100][100];
int n;
void solve(int a) {
  // for(int i=0;i<n;i++)printf("%d ",use[a-1][i]);
  // printf("\\n");
  for (int i = a; i < n; i++) {
    bool ok = true;
    for (int j = 0; j < i && ok; j++) {
      if (a && use[a - 1][j] && !g[i][j])
        ok = false;
    }
    for (int j = 0; j <= i; j++)
      use[i][j] = 0;
    if (a == 0) {
      use[i][i] = 9999999;
      solve(i + 1);
    } else {
      use[i][i] = 9999999;
      for (int j = 0; j < i; j++)
        if (use[a - 1][j]) {
          use[i][i] = min(use[i][i], g[i][j]);
          use[i][j] = min(use[a - 1][j], g[i][j]);
        }
      int val = 0;
      for (int j = 0; j <= i; j++)
        if (use[i][j])
          val += use[i][j];
      ret = max(ret, val);
      solve(i + 1);
    }
  }
}
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  n = a;
  if (a == 1) {
    printf("0\\n");
    return 0;
  }
  for (int i = 0; i < b; i++) {
    int c, d, e;
    scanf("%d%d%d", &c, &d, &e);
    c--;
    d--;
    g[c][d] = g[d][c] = e;
  }
  solve(0);
  printf("%d\\n", ret);
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int edges[110][110];
bool picked[110];
int n, m;

// ??????u??\\?????????????????????????±?(u??????
// ?????????????????????????±????????¨????????????§????????????????????????????????\\?????????????????°??????
// (= ???????????????????????¢??????????????????
int _solve(int u) {
  // ?????¨?????¶??????????????¢????¨????
  int ret = 0;
  for (int i = 0; i < n; ++i)
    if (picked[i]) {
      int score = 999999999;
      for (int j = 0; j < n; ++j)
        if (i != j && picked[j]) {
          score = min(score, edges[i][j]);
        }
      ret += score;
    }

  for (int i = u; i < n; ++i)
    if (!picked[i]) { // ?¬?????????¶?????????????£?
      // ????????????????????¨????????¨????????£?????????
      bool ok = true;
      for (int k = 0; k < n; ++k)
        if (picked[k]) {
          if (edges[i][k] == 0) {
            ok = false;
            break;
          }
        }
      if (ok) {
        picked[i] = true;
        ret = max(ret, _solve(i + 1));
        picked[i] = false;
      }
    }
  return ret;
}

int solve() {
  int ret = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j)
      if (edges[i][j]) { // ?????????2??????????????¶
        picked[i] = picked[j] = true;
        ret = max(ret, _solve(max(i, j) + 1));
        picked[i] = picked[j] = false;
      }
  }
  return ret;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, f;
    cin >> u >> v >> f;
    u--;
    v--;
    edges[u][v] = edges[v][u] = f;
  }

  cout << solve() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int edges[110][110];
bool picked[110];
int n, m;

int _solve() {
  // ?????¨?????¶??????????????¢????¨????
  int ret = 0;
  for (int i = 0; i < n; ++i)
    if (picked[i]) {
      int score = 999999999;
      for (int j = 0; j < n; ++j)
        if (i != j && picked[j]) {
          score = min(score, edges[i][j]);
        }
      ret += score;
    }

  for (int i = 0; i < n; ++i)
    if (!picked[i]) { // ?¬?????????¶?????????????£?
      // ????????????????????¨????????¨????????£?????????
      bool ok = true;
      for (int k = 0; k < n; ++k)
        if (picked[k]) {
          if (edges[i][k] == 0) {
            ok = false;
            break;
          }
        }
      if (ok) {
        picked[i] = true;
        ret = max(ret, _solve());
        picked[i] = false;
      }
    }
  return ret;
}

int solve() {
  int ret = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j)
      if (edges[i][j]) { // ?????????2??????????????¶
        picked[i] = picked[j] = true;
        ret = max(ret, _solve());
        picked[i] = picked[j] = false;
      }
  }
  return ret;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, f;
    cin >> u >> v >> f;
    u--;
    v--;
    edges[u][v] = edges[v][u] = f;
  }

  cout << solve() << endl;

  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int NMax = 150;

int A[NMax][NMax];
int used[NMax];
int N, M;

int bkt(int x, int y) {
  if (x >= N) {
    if (y < 2)
      return 0;
    int ans = 0;
    for (int i = 0; i < y; ++i) {
      int Min = INT_MAX;

      for (int j = 0; j < y; ++j)
        if (i != j)
          Min = min(Min, A[used[i]][used[j]]);

      ans = ans + Min;
    }

    return ans;
  }

  int ans = bkt(x + 1, y);
  int Ok = 0;

  for (int i = 0; i < y; ++i)
    if (!A[used[i]][x])
      Ok = 1;

  if (!Ok) {
    used[y] = x;
    ans = max(ans, bkt(x + 1, y + 1));
  }

  return ans;
}

int main() {
  cin >> N >> M;

  for (int i = 0; i < M; ++i) {
    int u, v, f;

    cin >> u >> v >> f;
    A[u - 1][v - 1] = A[v - 1][u - 1] = f;
  }

  int ans = bkt(0, 0);
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <fstream>
#include <iostream>

using namespace std;
ifstream fin("date.in");

int n, m;
long a[101][101];
int v[101], viz[101];
long maxim;

void bkt(int poz, int sol) {
  if (poz == sol + 1) {
    long minim = 0, suma = 0;
    for (int j = 1; j <= sol; ++j) {
      minim = a[v[1]][v[j]];
      for (int i = 2; i <= sol; ++i)
        minim = min(minim, a[v[i]][v[j]]);
      suma += minim;
    }
    maxim = max(maxim, suma);
  } else {
    for (int i = v[poz - 1] + 1; i <= n - sol + poz; ++i) {
      if (!viz[i]) {
        v[poz] = i;
        viz[i] = 1;
        bkt(poz + 1, sol);
        viz[i] = 0;
      }
    }
  }
}

int main() {
  cin >> n >> m;
  if (m == 0) {
    cout << 0 << "\\n";
    return 0;
  }
  int u = 0, v = 0;
  long f = 0;
  char s[21];
  cin.get();
  for (int j = 1; j <= m; ++j) {
    // cin>>u>>v>>f;
    cin.getline(s, 20);
    int i = 0;
    u = v = f = 0;
    while (s[i] != ' ') {
      u = u * 10 + s[i] - '0';
      ++i;
    }
    ++i;
    while (s[i] != ' ') {
      v = v * 10 + s[i] - '0';
      ++i;
    }
    ++i;
    while (s[i] != NULL) {
      f = f * 10 + s[i] - '0';
      ++i;
    }
    a[u][v] = a[v][u] = f;
  }

  for (int i = 1; i <= n; ++i)
    a[i][i] = 1000001;

  for (int j = 1; j <= n; ++j) {
    long minim = a[1][j];
    for (int i = 2; i <= n; ++i)
      minim = min(minim, a[i][j]);
    maxim += minim;
  }

  for (int i = 1; i < n; ++i) {
    for (int j = i + 1; j <= n; ++j)
      maxim = max(maxim, min(a[i][i], a[i][j]) + min(a[j][i], a[j][j]));
  }

  for (int i = 3; i < n; i++)
    bkt(1, i);

  cout << maxim << "\\n";
  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef __int128 State;

int N, M;
int G[110][110];

int calc(State s) {
  int res = 0;
  for (int i = 0; i < N; i++)
    if ((s >> i) & 1) {
      int x = 1e9;
      for (int j = 0; j < N; j++)
        if (i != j && (s >> j) & 1) {
          x = min(x, G[i][j]);
        }
      res += x;
    }
  return res;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--;
    y--;
    G[x][y] = G[y][x] = w;
  }

  int ans = 0;
  set<State> states;
  for (int i = 0; i < N; i++) {
    states.insert((State)1 << i);
  }

  while (states.size()) {
    set<State> tmp;
    for (auto u : states) {
      for (int i = 0; i < N; i++)
        if (!((u >> i) & 1)) {
          bool ok = true;
          for (int j = 0; j < N; j++)
            if ((u >> j) & 1) {
              ok &= G[i][j] != 0;
            }
          if (ok) {
            State v = u;
            v |= ((State)1 << i);
            ans = max(ans, calc(v));
            tmp.insert(v);
          }
        }
    }
    states = tmp;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef __int128 State;

int N, M;
int G[110][110];

int calc(State s) {
  int res = 0;
  for (int i = 0; i < N; i++)
    if ((s >> i) & 1) {
      int x = 1e9;
      for (int j = 0; j < N; j++)
        if (i != j && (s >> j) & 1) {
          x = min(x, G[i][j]);
        }
      res += x;
    }
  return res;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    x--;
    y--;
    G[x][y] = G[y][x] = w;
  }

  int ans = 0;
  set<State> states;
  for (int i = 0; i < N; i++) {
    states.insert(1LL << i);
  }

  while (states.size()) {
    set<State> tmp;
    for (auto u : states) {
      for (int i = 0; i < N; i++)
        if (!((u >> i) & 1)) {
          bool ok = true;
          for (int j = 0; j < N; j++)
            if ((u >> j) & 1) {
              ok &= G[i][j] != 0;
            }
          if (ok) {
            State v = u;
            v |= (1 << i);
            ans = max(ans, calc(v));
            tmp.insert(v);
          }
        }
    }
    states = tmp;
  }
  cout << ans << endl;

  return 0;
}
Problem p01423 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define S second
#define INF (1e9)
#define N 105
using namespace std;

typedef pair<int, int> P;

int n, m, d[N][N];
int used[N], ans = 0;

vector<P> idx;

set<vector<int>> memo;

void dfs(int x, vector<int> G) {

  if (x >= 2) {

    int sum = 0;

    for (int i = 0; i < G.size(); i++) {

      int mincost = INF;

      for (int j = 0; j < G.size(); j++) {

        if (G[i] != G[j])
          mincost = min(mincost, d[G[i]][G[j]]);
      }

      if (mincost != INF)
        sum += mincost;
    }

    ans = max(ans, sum);
  }

  sort(G.begin(), G.end());

  if (memo.count(G))
    return;
  memo.insert(G);

  if (x * (x - 1) > m * m)
    return;

  for (int i = 0; i < n; i++) {

    if (used[idx[i].S])
      continue;

    used[idx[i].S] = 1;

    int mincost = INF;

    vector<int> nG = G;

    nG.push_back(idx[i].S);

    for (int j = 0; j < nG.size(); j++)

      if (idx[i].S != nG[j] && used[nG[j]])
        mincost = min(mincost, d[idx[i].S][nG[j]]);

    if (!x || (mincost && mincost != INF))
      dfs(x + 1, nG);

    used[idx[i].S] = 0;
  }
}

int main() {

  cin >> n >> m;

  int a, b, f;

  for (int i = 0; i < n; i++)
    idx.push_back(P(0, i));

  for (int i = 0; i < m; i++) {

    cin >> a >> b >> f;

    d[a - 1][b - 1] = d[b - 1][a - 1] = f;

    idx[a - 1].first++;
    idx[b - 1].first++;
  }

  sort(idx.begin(), idx.end());

  vector<int> init;

  dfs(0, init);

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define S second
#define INF (1e9)
#define N 105
using namespace std;

typedef pair<int, int> P;

int n, m, d[N][N];
int used[N], ans = 0;

vector<P> idx;

void dfs(int x, vector<int> G) {

  if (x >= 2) {

    int sum = 0;

    for (int i = 0; i < G.size(); i++) {

      if (!used[G[i]])
        continue;

      int mincost = INF;

      for (int j = 0; j < G.size(); j++) {

        if (!used[G[j]])
          continue;

        if (G[i] != G[j])
          mincost = min(mincost, d[G[i]][G[j]]);
      }

      if (mincost != INF)
        sum += mincost;
    }

    ans = max(ans, sum);
  }

  if (x * (x - 1) > m)
    return;

  for (int i = 0; i < n; i++) {

    if (used[idx[i].S])
      continue;

    used[idx[i].S] = 1;

    int mincost = INF;

    vector<int> nG = G;

    nG.push_back(idx[i].S);

    for (int j = 0; j < G.size(); j++)

      if (idx[i].S != G[j] && used[G[j]])
        mincost = min(mincost, d[idx[i].S][G[j]]);

    if (!x || (mincost && mincost != INF))
      dfs(x + 1, nG);

    used[idx[i].S] = 0;
  }
}

int main() {

  cin >> n >> m;

  int a, b, f;

  for (int i = 0; i < n; i++)
    idx.push_back(P(0, i));

  for (int i = 0; i < m; i++) {

    cin >> a >> b >> f;

    d[a - 1][b - 1] = d[b - 1][a - 1] = f;

    idx[a - 1].first++;
    idx[b - 1].first++;
  }

  sort(idx.begin(), idx.end());

  vector<int> init;

  dfs(0, init);

  cout << ans << endl;

  return 0;
}