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

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 59;

unordered_map<int64, int> prime_factor(int64 n) {
  unordered_map<int64, int> ret;
  for (int64 i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return (ret);
}

int Q;
pair<unordered_map<int64, int>, int> vv[300];

int64 rec(vector<pair<int, int>> &conv, vector<int> &candidate, int idx,
          bool flag) {
  int64 ret = 0;

  if (candidate.empty()) {
    return (ret);
  }

  if (flag) {
    for (auto &j : candidate)
      ret += vv[j].second;
  }

  if (idx < conv.size()) {

    if (conv[idx].first >= 17 && flag) {

      vector<int> used(Q, 0);
      int64 calc = 0;
      for (int i = idx; i < conv.size(); i++) {
        int64 beet = 0;
        if (conv[idx].first == 17) {
          int64 beet2 = 0;
          for (auto &j : candidate) {
            if (vv[j].first.count(conv[i].first)) {
              if (vv[j].first[conv[i].first] == 2)
                beet += vv[j].second;
              else
                beet2 += vv[j].second, beet += vv[j].second;
              used[j] = true;
            }
          }
          calc += max(0LL, max(beet, beet2));
          continue;
        }

        for (auto &j : candidate) {
          if (vv[j].first.count(conv[i].first)) {
            beet += vv[j].second;
            used[j] = true;
          }
        }
        calc += max(0LL, beet);
      }
      for (auto &j : candidate) {
        if (!used[j])
          calc += vv[j].second;
      }
      ret = max(ret, calc);

    } else {
      set<vector<int>> used;
      for (int i = 0; i <= conv[idx].second + 1; i++) {
        vector<int> nt;
        for (int j : candidate) {
          if (vv[j].first.count(conv[idx].first)) {
            if (vv[j].first[conv[idx].first] > i)
              continue;
          }
          nt.emplace_back(j);
        }
        if (used.count(nt))
          continue;
        used.emplace(nt);
        ret = max(ret, rec(conv, nt, idx + 1, flag | (i > 0)));
      }
    }
  }
  return (ret);
}

int main() {
  cin >> Q;
  map<int, int> mx;
  for (int i = 0; i < Q; i++) {
    int m, x;
    cin >> m >> x;
    auto latte = prime_factor(m);
    for (auto &p : latte) {
      mx[p.first] = max(mx[p.first], p.second);
    }
    vv[i] = {latte, x};
  }
  vector<pair<int, int>> convert(begin(mx), end(mx));
  vector<int> candidate(Q);
  iota(begin(candidate), end(candidate), 0);
  cout << rec(convert, candidate, 0, false) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 59;

unordered_map<int64, int> prime_factor(int64 n) {
  unordered_map<int64, int> ret;
  for (int64 i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return (ret);
}

int Q;
pair<unordered_map<int64, int>, int> vv[300];

int64 rec(vector<pair<int, int>> &conv, vector<int> &candidate, int idx,
          bool flag) {
  int64 ret = 0;

  if (candidate.empty()) {
    return (ret);
  }

  if (flag) {
    for (auto &j : candidate)
      ret += vv[j].second;
  }

  if (idx < conv.size()) {

    if (conv[idx].first >= 19 && flag) {

      vector<int> used(Q, 0);
      int64 calc = 0;
      for (int i = idx; i < conv.size(); i++) {
        int64 beet = 0;
        for (auto &j : candidate) {
          if (vv[j].first.count(conv[i].first)) {
            beet += vv[j].second;
            used[j] = true;
          }
        }
        calc += max(0LL, beet);
      }
      for (auto &j : candidate) {
        if (!used[j])
          calc += vv[j].second;
      }
      ret = max(ret, calc);

    } else {
      set<vector<int>> used;
      for (int i = 0; i <= conv[idx].second + 1; i++) {
        vector<int> nt;
        for (int j : candidate) {
          if (vv[j].first.count(conv[idx].first)) {
            if (vv[j].first[conv[idx].first] > i)
              continue;
          }
          nt.emplace_back(j);
        }
        if (used.count(nt))
          continue;
        used.emplace(nt);
        ret = max(ret, rec(conv, nt, idx + 1, flag | (i > 0)));
      }
    }
  }
  return (ret);
}

int main() {
  cin >> Q;
  map<int, int> mx;
  for (int i = 0; i < Q; i++) {
    int m, x;
    cin >> m >> x;
    auto latte = prime_factor(m);
    for (auto &p : latte) {
      mx[p.first] = max(mx[p.first], p.second);
    }
    vv[i] = {latte, x};
  }
  vector<pair<int, int>> convert(begin(mx), end(mx));
  vector<int> candidate(Q);
  iota(begin(candidate), end(candidate), 0);
  cout << rec(convert, candidate, 0, false) << endl;
}