Problem p01423 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()

template <typename T, typename E>
ostream &operator<<(std::ostream &os, const std::pair<T, E> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m && n + m) {
    int score[110][110];
    memset(score, 0, sizeof(score));

    REP(i, m) {
      int x, y, f;
      cin >> x >> y >> f;
      x--, y--;
      score[x][y] = score[y][x] = f;
    }

    int res = 0;
    REP(i, n) {
      vector<int> fs;
      REP(j, n) if (score[i][j] > 0) { fs.push_back(j); }

      int size = fs.size();
      if (size * (size - 1) / 2 <= m) {

        REP2(mask, 1, 1 << size) {

          vector<int> vs(1, i);
          REP(j, size) if (mask & (1 << j)) { vs.push_back(fs[j]); }

          int sum = 0;
          REP(j, vs.size()) {
            int add = 1e9;
            REP(k, vs.size()) if (j != k) {
              add = min(add, score[vs[j]][vs[k]]);
            }
            sum += add;
          }
          res = max(res, sum);
        }
      }
    }
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()

template <typename T, typename E>
ostream &operator<<(std::ostream &os, const std::pair<T, E> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m && n + m) {
    int score[110][110];
    memset(score, 0, sizeof(score));

    REP(i, m) {
      int x, y, f;
      cin >> x >> y >> f;
      x--, y--;
      score[x][y] = score[y][x] = f;
    }

    int res = 0;
    REP(i, n) {
      vector<int> fs;
      REP(j, n) if (score[i][j] > 0) { fs.push_back(j); }

      int size = fs.size();
      if (size * (size - 1) / 2 <= m) {

        REP2(mask, 1, 1 << size) {

          vector<int> vs(1, i);
          REP(j, mask) if (mask & (1 << j)) { vs.push_back(fs[j]); }

          int sum = 0;
          REP(j, vs.size()) {
            int add = 1e9;
            REP(k, vs.size()) if (j != k) {
              add = min(add, score[vs[j]][vs[k]]);
            }
            sum += add;
          }
          res = max(res, sum);
        }
      }
    }
    cout << res << endl;
  }

  return 0;
}