Problem p01423 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define inf (1LL << 50)

int N, M;
vector<vector<long long int>> bridge;

long long int solve(vector<long long int> D, long long int idx) {
  long long int ret = 0;
  for (int i = idx; i < N; i++) {
    auto next = D;

    for (int j = 0; j < N; j++) {
      if (i != j && D[j] != -1) {
        if (D[i] == -1)
          D[i] = bridge[i][j];
        D[i] = min(D[i], bridge[i][j]);
        D[j] = min(D[j], bridge[i][j]);
      }
    }
    long long int plus = 0;
    for (int j = 0; j < N; j++) {
      long long int x = D[j];
      long long int y = next[j];
      if (x == -1)
        x = 0;
      if (y == -1)
        y = 0;
      plus += x - y;
    }
    if (plus >= 0) {
      ret = max(ret, solve(D, i + 1));
    }
    D = next;
  }
  long long int sum = 0;
  for (int i = 0; i < N; i++) {
    if (D[i] != -1) {
      sum += D[i];
    }
  }
  return max(sum, ret);
}

int main() {
  cin >> N >> M;
  bridge = vector<vector<long long int>>(N, vector<long long int>(N));
  vector<pair<long long int, long long int>> bridgepair(M);
  for (int i = 0; i < M; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    bridge[a][b] = c;
    bridge[b][a] = c;
    bridgepair[i] = (make_pair(a, b));
  }
  long long int ans = 0;
  for (int i = 0; i < M; i++) {
    auto x = vector<long long int>(N, -1);
    x[bridgepair[i].first] = bridge[bridgepair[i].first][bridgepair[i].second];
    x[bridgepair[i].second] = bridge[bridgepair[i].first][bridgepair[i].second];
    ans = max(ans, solve(x, 0));
    //		bridge[bridgepair[i].first][bridgepair[i].second] = -inf;
    //		bridge[bridgepair[i].second][bridgepair[i].first] = -inf;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define inf (1LL << 50)

int N, M;
vector<vector<long long int>> bridge;

long long int solve(vector<long long int> D, long long int idx) {
  long long int ret = 0;
  for (int i = idx; i < N; i++) {
    auto next = D;

    for (int j = 0; j < N; j++) {
      if (i != j && D[j] != -1) {
        if (D[i] == -1)
          D[i] = bridge[i][j];
        D[i] = min(D[i], bridge[i][j]);
        D[j] = min(D[j], bridge[i][j]);
      }
    }
    long long int plus = 0;
    for (int j = 0; j < N; j++) {
      long long int x = D[j];
      long long int y = next[j];
      if (x == -1)
        x = 0;
      if (y == -1)
        y = 0;
      plus += x - y;
    }
    if (plus >= 0) {
      ret = max(ret, solve(D, i + 1));
    }
    D = next;
  }
  long long int sum = 0;
  for (int i = 0; i < N; i++) {
    if (D[i] != -1) {
      sum += D[i];
    }
  }
  return max(sum, ret);
}

int main() {
  cin >> N >> M;
  bridge = vector<vector<long long int>>(N, vector<long long int>(N));
  vector<pair<long long int, long long int>> bridgepair(M);
  for (int i = 0; i < M; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    bridge[a][b] = c;
    bridge[b][a] = c;
    bridgepair[i] = (make_pair(a, b));
  }
  long long int ans = 0;
  for (int i = 0; i < M; i++) {
    auto x = vector<long long int>(N, -1);
    x[bridgepair[i].first] = bridge[bridgepair[i].first][bridgepair[i].second];
    x[bridgepair[i].second] = bridge[bridgepair[i].first][bridgepair[i].second];
    ans = max(ans, solve(x, 0));
    bridge[bridgepair[i].first][bridgepair[i].second] = -inf;
    //		bridge[bridgepair[i].second][bridgepair[i].first] = -inf;
  }
  cout << ans << endl;
}