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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> graph(n, vector<int>(n));
  rep(i, 0, m) {
    int u, v, f;
    cin >> u >> v >> f;
    --u;
    --v;
    graph[u][v] = graph[v][u] = f;
  }

  int ans = 0;
  rep(i, 2, inf) {
    if (i * (i - 1) / 2 > m)
      continue;
    map<set<int>, int> memo;
    map<set<int>, bool> done;
    function<int(int, set<int>)> rec = [&](int num, set<int> s) {
      int &res = memo[s];
      if (done[s])
        return res;
      done[s] = true;
      if (num == 0) {
        for (int v : s) {
          int tmp = inf;
          for (int u : s) {
            if (u == v)
              continue;
            tmp = min(tmp, graph[v][u]);
          }
          res += tmp;
        }
        return res;
      }
      rep(v, 0, n) {
        if (s.find(v) != s.end())
          continue;
        bool ok = true;
        for (int u : s) {
          if (graph[v][u] != 0)
            continue;
          ok = false;
          break;
        }
        if (!ok)
          continue;
        set<int> s_ = s;
        s_.insert(v);
        res = max(res, rec(num - 1, s_));
      }
      return res;
    };
    int tmp = rec(i, set<int>());
    if (tmp == 0)
      break;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

Error: MLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
template <typename T> using vec = std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 62;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> graph(n, vector<int>(n));
  rep(i, 0, m) {
    int u, v, f;
    cin >> u >> v >> f;
    --u;
    --v;
    graph[u][v] = graph[v][u] = f;
  }

  map<pair<int, set<int>>, int> memo;
  map<pair<int, set<int>>, bool> done;
  function<int(int, set<int>)> rec = [&](int num, set<int> s) {
    auto p = make_pair(num, s);
    int &res = memo[p];
    if (done[p])
      return res;
    done[p] = true;
    if (num == 0) {
      for (int v : s) {
        int tmp = inf;
        for (int u : s) {
          if (u == v)
            continue;
          tmp = min(tmp, graph[v][u]);
        }
        res += tmp;
      }
      return res;
    }
    rep(v, 0, n) {
      if (s.find(v) != s.end())
        continue;
      bool ok = true;
      for (int u : s) {
        if (graph[v][u] != 0)
          continue;
        ok = false;
        break;
      }
      if (!ok)
        continue;
      set<int> s_ = s;
      s_.insert(v);
      res = max(res, rec(num - 1, s_));
    }
    return res;
  };

  int ans = 0;
  rep(i, 2, inf) {
    if (i * (i - 1) / 2 > m)
      continue;
    int tmp = rec(i, set<int>());
    if (tmp == 0)
      break;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}