Problem p03180 (C++)

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vll>;
using vb = vector<bool>;

const int INF = 2e9 + 5;
const ll LLINF = (1LL << 60) + 5;

/*
  filippos
  Contest : AtCoder DP contest
  Task : U - Grouping
*/

int main() {
  cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0);

  int n;
  cin >> n;
  vvi v(n, vi(n));
  for (auto &x : v)
    for (auto &i : x)
      cin >> i;

  vll mask_value(1 << n, 0);
  for (int mask = 0; mask < (1 << n); mask++) {
    ll &value = mask_value[mask];
    vi indexes;
    for (int i = 0; i < n; i++)
      if (mask & (1 << i))
        indexes.push_back(i);
    for (int i = 0; i < indexes.size(); i++)
      for (int j = i + 1; j < indexes.size(); j++)
        value += v[indexes[i]][indexes[j]];
  }

  vll memo(1 << n, -1);
  function<ll(int)> dp = [&](int state) {
    ll &ans = memo[state];
    if (ans != -1)
      return ans;
    ans = 0;
    // iterate over all masks is too slow (2^2N = 2^32)
    // so we iterate only over valid masks (should be less lol)
    vi indexes;
    for (int i = 0; i < n; i++)
      if (!(state & (1 << i)))
        indexes.push_back(i);
    // iterate over submasks
    for (int sub = 1; sub < (1 << indexes.size()); sub++) {
      int mask = 0;
      for (int j = 0; j < indexes.size(); j++) {
        if (sub & (1 << j)) {
          mask |= (1 << indexes[j]);
        }
      }
      // dp for each mask
      ans = max(ans, mask_value[mask] + dp(state | mask));
    }
    return ans;
  };

  cout << dp(0);

  return cout << endl, 0;
}

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vll>;
using vb = vector<bool>;

const int INF = 2e9 + 5;
const ll LLINF = (1LL << 60) + 5;

/*
  filippos
  Contest : AtCoder DP contest
  Task : U - Grouping
*/

int main() {
  cin.tie(0), cin.sync_with_stdio(0), cout.tie(0), cout.sync_with_stdio(0);

  int n;
  cin >> n;
  vvi v(n, vi(n));
  for (auto &x : v)
    for (auto &i : x)
      cin >> i;

  vll mask_value(1 << n, 0);
  for (int mask = 0; mask < (1 << n); mask++) {
    ll &value = mask_value[mask];
    vi indexes;
    for (int i = 0; i < n; i++)
      if (mask & (1 << i))
        indexes.push_back(i);
    for (int i = 0; i < indexes.size(); i++)
      for (int j = i + 1; j < indexes.size(); j++)
        value += v[indexes[i]][indexes[j]];
  }

  vll memo(1 << n, -1);
  function<ll(int)> dp = [&](int state) {
    ll &ans = memo[state];
    if (ans != -1)
      return ans;
    ans = 0;
    // iterate over all masks is too slow (2^2N = 2^32)
    // so we iterate only over valid masks (should be less lol)
    vi indexes;
    for (int i = 0; i < n; i++)
      if (!(state & (1 << i)))
        indexes.push_back(i);
    // iterate over submasks
    for (int sub = 1; sub < (1 << indexes.size()); sub++) {
      int mask = 0;
      for (int j = 0; j < indexes.size(); j++)
        mask |= ((((sub >> j) & 1)) << indexes[j]);
      // dp for each mask
      ans = max(ans, mask_value[mask] + dp(state | mask));
    }
    return ans;
  };

  cout << dp(0);

  return cout << endl, 0;
}