prithivMLmods/Coder-Stat huggingface dataset:
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const ll MOD = (ll)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

priority_queue<int, vector<int>, greater<int>> pq;
vector<int> v;

char s[3005];
int dp[3005][3005];
int n;

int go(int o, int dd) {
  if (o == n)
    return 1;
  if (dp[o][dd] != -1)
    return dp[o][dd];

  ll ret = 0;
  int nn = n - o;

  if (s[o] == '<') {
    if (dd < nn - 1)
      ret += go(o, dd + 1);
    if (dd < nn)
      ret += go(o + 1, dd);
  } else {
    if (dd > 1)
      ret += go(o, dd - 1);
    if (dd > 0)
      ret += go(o + 1, dd - 1);
  }

  return dp[o][dd] = ret % MOD;
}

int main() {
  memset(dp, -1, sizeof(dp));

  scanf("%d", &n);
  scanf("%s", s + 1);

  ll ret = 0;
  for (int i = 0; i < n; i++)
    ret += go(1, i);

  printf("%lld", ret % MOD);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const ll MOD = (ll)1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;

priority_queue<int, vector<int>, greater<int>> pq;
vector<int> v;

char s[3005];
int dp[3005][3005];
int n;

int go(int o, int dd) {
  if (o == n)
    return 1;
  if (dp[o][dd] != -1)
    return dp[o][dd];

  ll ret = 0;
  int nn = n - o;

  if (s[o] == '<') {
    if (dd < nn - 1)
      ret += go(o, dd + 1);
    if (dd < nn)
      ret += go(o + 1, dd);
  } else {
    if (dd > 1)
      ret += go(o, dd - 1);
    if (dd > 0)
      ret += go(o + 1, dd - 1);
  }

  return dp[o][dd] = ret % MOD;
}

int main() {
  memset(dp, -1, sizeof(dp));

  scanf("%d", &n);
  scanf("%s", s + 1);

  ll ret = 0;
  for (int i = n - 1; i >= 1; i--)
    for (int j = 0; j < n - i + 1; j++)
      go(i, j);

  for (int i = 0; i < n; i++)
    ret += go(1, i);

  printf("%lld", ret % MOD);
}
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define turbo                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define vi vector<int>
#define vvi vector<vi>
#define qi queue<int>
#define ld long double

using namespace std;

/*---------------------------------------------------------///CODE///---------------------------------------------------------*/

const int N = 1e3;
const int MOD = 1e9 + 7;
int dp[N + 10][N + 10];
int pref[N + 10][N + 10];

int main() {
  turbo

      int n;
  cin >> n;

  string s;
  cin >> s;

  dp[1][1] = 1;
  pref[1][1] = 1;
  for (int i = 2; i <= s.size() + 1; i++) {
    for (int j = 1; j <= i; j++)
      (dp[i][j] += (s[i - 2] == '<' ? pref[i - 1][j - 1]
                                    : pref[i - 1][i - 1] - pref[i - 1][j - 1] +
                                          MOD)) %= MOD;

    for (int j = 1; j <= i; j++)
      pref[i][j] = (pref[i][j - 1] + dp[i][j]) % MOD;
  }

  int ans = 0;

  for (int i = 1; i <= s.size() + 1; i++)
    (ans += dp[s.size() + 1][i]) %= MOD;

  return cout << ans, 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define st first
#define nd second
#define turbo                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define vi vector<int>
#define vvi vector<vi>
#define qi queue<int>
#define ld long double

using namespace std;

/*---------------------------------------------------------///CODE///---------------------------------------------------------*/

const int N = 3e3;
const int MOD = 1e9 + 7;
int dp[N + 10][N + 10];
int pref[N + 10][N + 10];

int main() {
  turbo

      int n;
  cin >> n;

  string s;
  cin >> s;

  dp[1][1] = 1;
  pref[1][1] = 1;
  for (int i = 2; i <= s.size() + 1; i++) {
    for (int j = 1; j <= i; j++)
      (dp[i][j] += (s[i - 2] == '<' ? pref[i - 1][j - 1]
                                    : pref[i - 1][i - 1] - pref[i - 1][j - 1] +
                                          MOD)) %= MOD;

    for (int j = 1; j <= i; j++)
      pref[i][j] = (pref[i][j - 1] + dp[i][j]) % MOD;
  }

  int ans = 0;

  for (int i = 1; i <= s.size() + 1; i++)
    (ans += dp[s.size() + 1][i]) %= MOD;

  return cout << ans, 0;
}
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define For(i, n) for (int i = 0; i < n; i++)
#define mem(a, b) memset(a, b, sizeof(a))
#define int long long int
#define ld long double
#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define vpi vector<pair<int, int>>
#define vv vector<int>
#define MAXN 3005
int dp[MAXN][MAXN];
int n;
// void fun(int I , int id)
// {
//     if(id==0)return 1;
// 	if(dp[I][id]!=)
//     int t;
// 	if (str[id] == '>')
// 	{
// 		t = sum(n, id - 1) - sum(I-1,id-1);
// 	}
// 	else{
//        t=sum(I-1,id-1);
// 	}
// 	return t;
// }

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fio;

  cin >> n;
  string str;
  cin >> str;
  int nn = 0;
  // for(int i=1;i<=n;i++)
  // 	nn+=fun(i,n-1)
  // nn=fun(i,n-1);
  // cout<<nn;

  if (str[0] == '>') {
    dp[0][1] = 1;
    dp[0][2] = 1;
  } else {
    dp[0][2] = 1;
  }

  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j <= i + 2; j++) {
      if (str[i] == '<') {
        dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;
      } else {

        dp[i][j] =
            (dp[i][j - 1] + (dp[i - 1][i + 1] - dp[i - 1][j - 1]) + mod) % mod;
      }
    }
  }
  cout << dp[n - 2][n];
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define For(i, n) for (int i = 0; i < n; i++)
#define mem(a, b) memset(a, b, sizeof(a))
#define int long long int
#define ld long double
#define fio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define vpi vector<pair<int, int>>
#define vv vector<int>
#define MAXN 3005
int dp[MAXN][MAXN];
int n;
// void fun(int I , int id)
// {
//     if(id==0)return 1;
// 	if(dp[I][id]!=)
//     int t;
// 	if (str[id] == '>')
// 	{
// 		t = sum(n, id - 1) - sum(I-1,id-1);
// 	}
// 	else{
//        t=sum(I-1,id-1);
// 	}
// 	return t;
// }

int32_t main() {

  fio;

  cin >> n;
  string str;
  cin >> str;
  int nn = 0;
  // for(int i=1;i<=n;i++)
  // 	nn+=fun(i,n-1)
  // nn=fun(i,n-1);
  // cout<<nn;

  if (str[0] == '>') {
    dp[0][1] = 1;
    dp[0][2] = 1;
  } else {
    dp[0][2] = 1;
  }

  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j <= i + 2; j++) {
      if (str[i] == '<') {
        dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;
      } else {

        dp[i][j] =
            (dp[i][j - 1] + (dp[i - 1][i + 1] - dp[i - 1][j - 1]) + mod) % mod;
      }
    }
  }
  cout << dp[n - 2][n];
}
Problem p03179 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

const long long INF = 1000000007;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<pii, pii> piiii;

int n;
int comp[3011];
int dp[3011][3011];

int main() {
  scanf("%d", &n);
  string s;
  cin >> s;
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == '<')
      comp[i] = 0;
    else
      comp[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dp[i][j] = 0;
  }

  dp[1][1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      if (comp[i - 1] == 0) {
        int sum = 0;
        for (int k = 1; k < j; k++)
          sum = (sum + dp[i - 1][k]) % INF;
        dp[i][j] = sum;
      } else {
        int sum = 0;
        for (int k = i - 1; k >= j; k--)
          sum = (sum + dp[i - 1][k]) % INF;
        dp[i][j] = sum;
      }
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum = (sum + dp[n][i]) % INF;
  printf("%d\n", sum);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

const long long INF = 1000000007;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<pii, pii> piiii;

int n;
int comp[3011];
int dp[3011][3011];

int main() {
  scanf("%d", &n);
  string s;
  cin >> s;
  for (int i = 1; i < n; i++) {
    if (s[i - 1] == '<')
      comp[i] = 0;
    else
      comp[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dp[i][j] = 0;
  }

  dp[1][1] = 1;
  for (int i = 2; i <= n; i++) {
    if (comp[i - 1] == 0) {
      int sum = 0;
      for (int k = 1; k < i; k++) {
        sum = (sum + dp[i - 1][k]) % INF;
        dp[i][k + 1] = sum;
      }
    } else {
      int sum = 0;
      for (int k = i - 1; k >= 1; k--) {
        sum = (sum + dp[i - 1][k]) % INF;
        dp[i][k] = sum;
      }
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum = (sum + dp[n][i]) % INF;
  printf("%d\n", sum);
}
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
// :'(((((((((((((
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define M 1000000007LL
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(v) ((ll)v.size())
#define mp make_pair
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pii pair<ll, ll>
#define mod 1000000007LL
#define MAXN 300002
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
vector<string> v;
ll dp[2002][2003];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll i;
  dp[0][1] = (s[0] == '>');
  dp[0][2] = 1;
  for (i = 1; i < sz(s); i++) {
    for (ll j = 1; j <= i + 2; j++) {
      dp[i][j] = dp[i][j - 1];
      if (s[i] == '<')
        dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod;
      else
        dp[i][j] =
            (((dp[i][j] + dp[i - 1][i + 1]) % mod - dp[i - 1][j - 1]) % mod +
             mod) %
            mod;
    }
  }
  cout << dp[sz(s) - 1][sz(s) + 1];
  return 0;
}

Fixed submission:
// :'(((((((((((((
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define M 1000000007LL
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define sep(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
#define mll map<ll, ll>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(v) ((ll)v.size())
#define mp make_pair
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pii pair<ll, ll>
#define mod 1000000007LL
#define MAXN 300002
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
vector<string> v;
ll dp[3002][3003];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll i;
  dp[0][1] = (s[0] == '>');
  dp[0][2] = 1;
  for (i = 1; i < sz(s); i++) {
    for (ll j = 1; j <= i + 2; j++) {
      dp[i][j] = dp[i][j - 1];
      if (s[i] == '<')
        dp[i][j] = (dp[i][j] + dp[i - 1][j - 1]) % mod;
      else
        dp[i][j] =
            (((dp[i][j] + dp[i - 1][i + 1]) % mod - dp[i - 1][j - 1]) % mod +
             mod) %
            mod;
    }
  }
  cout << dp[sz(s) - 1][sz(s) + 1];
  return 0;
}
Problem p03179 (C++)

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

using namespace std;
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<double, double> PD;

#define FOR(x, b, e) for (int x = b; x <= (e); x++)
#define FORD(x, b, e) for (int x = b; x >= (e); x--)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())

#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007

#define MAXS 3010

int n;
LL dp[MAXS][MAXS];
string s;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;

  dp[1][1] = 1;

  FOR(i, 2, n) {

    FOR(j, 1, i) {
      if (s[i - 2] == '>') {
        FOR(k, j, n) {
          dp[i][j] += dp[i - 1][k];
          dp[i][j] %= MOD;
        }
      } else {
        FOR(k, 1, j - 1) {
          dp[i][j] += dp[i - 1][k];
          dp[i][j] %= MOD;
        }
      }
    }
  }

  LL wyn = 0;
  FOR(i, 1, n) {
    wyn += dp[n][i];
    wyn %= MOD;
  }

  cout << wyn;

  return 0;
}


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

using namespace std;
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<double, double> PD;

#define FOR(x, b, e) for (int x = b; x <= (e); x++)
#define FORD(x, b, e) for (int x = b; x >= (e); x--)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())

#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007

#define MAXS 3010

int n;
LL dp[MAXS][MAXS];
string s;

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;

  dp[1][1] = 1;

  FOR(i, 2, n) {

    if (s[i - 2] == '>') {
      LL suf = 0;
      FORD(j, i, 1) {
        suf += dp[i - 1][j];
        suf %= MOD;
        dp[i][j] += suf;
        dp[i][j] %= MOD;
      }
    } else {
      LL pref = 0;
      FOR(j, 1, i) {
        pref += dp[i - 1][j - 1];
        pref %= MOD;
        dp[i][j] += pref;
        dp[i][j] %= MOD;
      }
    }
  }

  LL wyn = 0;
  FOR(i, 1, n) {
    wyn += dp[n][i];
    wyn %= MOD;
  }

  cout << wyn;

  return 0;
}
Problem p03179 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

int n;
string s;

const LL MOD = 1e9 + 7;

LL memo[4005][4005][5];

LL dp(int l, int r, int indi) { // 0 = menaik, 1 menurun
  if (l < 0 || r < 0)
    return 0;
  if ((l + r) == 0)
    return 1;
  LL &ret = memo[l][r][indi];
  if (ret != -1)
    return ret;
  int tot = l + r;
  if (indi == 0)
    ret = dp(l + 1, r - 1, indi);
  else
    ret = dp(l - 1, r + 1, indi);
  int id = n - tot;
  //	s[id] = '>';
  if (s[id] == '>')
    ret += dp(l - 1, r, 1);
  else
    ret += dp(l, r - 1, 0);
  ret %= MOD;
  //	cout<<l<<" "<<r<<" "<<indi<<endl;
  return ret;
}

int main() {
  cin >> n;
  cin >> s;
  n--;
  memset(memo, -1, sizeof(memo));
  LL ans = dp(n, 0, 1);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

int n;
string s;

const LL MOD = 1e9 + 7;

LL memo[4005][4005][5];

LL dp(int l, int r, int indi) { // 0 = menaik, 1 menurun
  if (l < 0 || r < 0)
    return 0;
  if ((l + r) == 0)
    return 1;
  LL &ret = memo[l][r][indi];
  if (ret != -1)
    return ret;
  int tot = l + r;
  if (indi == 0)
    ret = dp(l + 1, r - 1, indi);
  else
    ret = dp(l - 1, r + 1, indi);
  int id = n - tot;
  //	s[id] = '>';
  if (s[id] == '>')
    ret += dp(l - 1, r, 1);
  else
    ret += dp(l, r - 1, 0);
  ret %= MOD;
  //	cout<<l<<" "<<r<<" "<<indi<<endl;
  return ret;
}

int main() {
  cin >> n;
  cin >> s;
  n--;
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i <= 1; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 0; k <= j; k++)
        dp(k, j - k, i);
    }
  }
  LL ans = dp(n, 0, 1);
  printf("%lld\n", ans);
  return 0;
}
Problem p03180 (C++)

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

using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<double, double> PD;

#define FOR(x, b, e) for (int x = b; x <= (e); x++)
#define FORD(x, b, e) for (int x = b; x >= (e); x--)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())

#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007

#define MAXT 20
#define MAXS 65600

int n;
LL tab[MAXT][MAXT];
LL dp[MAXS];

LL przejdz(int mask) {

  if (dp[mask] == -1) {
    LL sum = 0;
    FOR(i, 1, n) {
      FOR(j, 1, n) {
        if (((1 << (i - 1) & mask) != 0) && ((1 << (j - 1) & mask) != 0))
          sum += tab[i][j];
      }
    }
    dp[mask] = max(0LL, sum);
  } else
    return dp[mask];

  REP(i, 1 << n) {
    int sub1 = i & mask;
    int sub2 = sub1 ^ mask;
    dp[mask] = max(dp[mask], przejdz(sub1) + przejdz(sub2));
  }

  return dp[mask];
}

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;

  FOR(i, 1, n) {
    FOR(j, 1, n)
    cin >> tab[i][j];
  }

  FOR(i, 0, 1 << n)
  dp[i] = -1;

  cout << przejdz((1 << n) - 1) / 2LL;

  return 0;
}


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

using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
typedef pair<double, double> PD;

#define FOR(x, b, e) for (int x = b; x <= (e); x++)
#define FORD(x, b, e) for (int x = b; x >= (e); x--)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())

#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007

#define MAXT 20
#define MAXS 65600

int n;
LL tab[MAXT][MAXT];
LL dp[MAXS];

LL przejdz(int mask) {

  if (dp[mask] == -1) {
    LL sum = 0;
    FOR(i, 1, n) {
      FOR(j, 1, n) {
        if (((1 << (i - 1) & mask) != 0) && ((1 << (j - 1) & mask) != 0))
          sum += tab[i][j];
      }
    }
    dp[mask] = max(0LL, sum);
  } else
    return dp[mask];

  for (int sub1 = (mask - 1) & mask; sub1; sub1 = (sub1 - 1) & mask) {

    int sub2 = sub1 ^ mask;
    dp[mask] = max(dp[mask], przejdz(sub1) + przejdz(sub2));
  }

  return dp[mask];
}

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;

  FOR(i, 1, n) {
    FOR(j, 1, n)
    cin >> tab[i][j];
  }

  FOR(i, 0, 1 << n)
  dp[i] = -1;

  cout << przejdz((1 << n) - 1) / 2LL;

  return 0;
}
Problem p03180 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N;
ll a[20][20];

ll score[(1 << 16)];
ll dp[(1 << 16)];
const ll INF = 1e18;

ll solve(int state) {
  if (dp[state] != -INF)
    return dp[state];

  ll res = score[state];

  for (int i = state - 1; i > 0; i--) {
    i &= state;
    ll tmp = solve(i);
    tmp += solve(state ^ i);
    res = max(res, tmp);
  }

  return dp[state] = res;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 0; i < (1 << N); i++) {
    vector<int> v;
    for (int j = 0; j < N; j++) {
      if ((1 << j) & i)
        v.push_back(j);
    }

    ll res = 0;
    for (int j = 0; j < v.size(); j++) {
      for (int k = j + 1; k < v.size(); k++) {
        res += a[v[j]][v[k]];
      }
    }
    score[i] = max(res, 0ll);
    dp[i] = -INF;
  }

  dp[0] = 0;
  cout << solve((1 << N) - 1) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int N;
ll a[20][20];

ll score[(1 << 16)];
ll dp[(1 << 16)];
const ll INF = 1e18;

ll solve(int state) {
  if (dp[state] != -INF)
    return dp[state];

  ll res = score[state];

  for (int i = state - 1; i > 0; i--) {
    i &= state;
    if (i == 0)
      break;
    ll tmp = solve(i);
    tmp += solve(state ^ i);
    res = max(res, tmp);
  }

  return dp[state] = res;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
    }
  }

  for (int i = 0; i < (1 << N); i++) {
    vector<int> v;
    for (int j = 0; j < N; j++) {
      if ((1 << j) & i)
        v.push_back(j);
    }

    ll res = 0;
    for (int j = 0; j < v.size(); j++) {
      for (int k = j + 1; k < v.size(); k++) {
        res += a[v[j]][v[k]];
      }
    }
    score[i] = max(res, 0ll);
    dp[i] = -INF;
  }

  dp[0] = 0;
  cout << solve((1 << N) - 1) << endl;

  return 0;
}
Problem p03180 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 16;
const int N2 = (1 << N);
int n, a[N][N];
long long cost[N2], dp[N2];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  int p2n = (1 << n);

  for (int bm = 0; bm < p2n; bm++) {
    for (int i = 1; i < n; i++) {
      if ((bm & (1 << i)) == 0)
        continue;
      for (int j = 0; j < i; j++) {
        if ((bm & (1 << j)) == 0)
          continue;
        cost[bm] += (long long)a[i][j];
      }
    }
  }

  for (int bm = 1; bm < p2n; bm++) {
    long long ans = 0;
    for (int sbm = 0; sbm < p2n; sbm++) {
      if ((sbm & bm) != sbm)
        continue;
      int cbm = bm ^ sbm;
      ans = max(ans, cost[sbm] + dp[cbm]);
    }
    dp[bm] = ans;
  }

  cout << dp[p2n - 1] << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 16;
const int N2 = (1 << N);
int n, a[N][N];
long long cost[N2], dp[N2];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }

  int p2n = (1 << n);

  for (int bm = 0; bm < p2n; bm++) {
    for (int i = 1; i < n; i++) {
      if ((bm & (1 << i)) == 0)
        continue;
      for (int j = 0; j < i; j++) {
        if ((bm & (1 << j)) == 0)
          continue;
        cost[bm] += (long long)a[i][j];
      }
    }
  }

  for (int bm = 1; bm < p2n; bm++) {
    long long ans = 0;
    for (int sbm = bm; sbm > 0; sbm = (sbm - 1) & bm) {
      if ((sbm & bm) != sbm)
        continue;
      int cbm = bm ^ sbm;
      ans = max(ans, cost[sbm] + dp[cbm]);
    }
    dp[bm] = ans;
  }

  cout << dp[p2n - 1] << '\n';

  return 0;
}
Problem p03180 (C++)

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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

int n;
vl pre;
vl dp;

void f(int i, vi p, ll s, int mask, int g) {
  int len = p.size();
  if (i == len) {
    dp[mask] = max(dp[mask], s + pre[g]);
    return;
  }

  f(i + 1, p, s, mask, g);
  f(i + 1, p, s, (mask ^ (1 << p[i])), g ^ (1 << p[i]));
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);

  cin >> n;

  int z = (1 << n);
  vector<vi> a(n, vi(n, 0));
  for (auto &i : a)
    for (auto &j : i)
      cin >> j;

  pre.resize(z, 0);
  ll nn = INT64_MIN + 1000000;
  dp.resize(z, nn);
  for (int i = 0; i < (1 << n); ++i) {
    for (int j = 0; j < n; ++j) {
      if (!(i & (1 << j)))
        continue;

      for (int k = j + 1; k < n; ++k) {
        if (!(i & (1 << k)))
          continue;

        pre[i] += a[k][j];
      }
    }
  }

  dp[0] = 0;
  for (int i = 0; i < z; ++i) {
    vi p;

    for (int j = 0; j < n; ++j) {
      if (!(i & (1 << j)))
        p.push_back(j);
    }

    f(0, p, dp[i], i, 0);
  }

  cout << dp[z - 1] << '\n';
}

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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;

int n;
vl pre;
vl dp;

void f(int i, const vi &p, ll s, int mask, int g) {
  if (i == (int)p.size()) {
    dp[mask] = max(dp[mask], s + pre[g]);
    return;
  }

  f(i + 1, p, s, mask, g);
  f(i + 1, p, s, (mask ^ (1 << p[i])), g ^ (1 << p[i]));
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);

  cin >> n;

  int z = (1 << n);
  vector<vi> a(n, vi(n, 0));
  for (auto &i : a)
    for (auto &j : i)
      cin >> j;

  pre.resize(z, 0);
  ll nn = INT64_MIN + 1000000;
  dp.resize(z, nn);
  for (int i = 0; i < (1 << n); ++i) {
    for (int j = 0; j < n; ++j) {
      if (!(i & (1 << j)))
        continue;

      for (int k = j + 1; k < n; ++k) {
        if (!(i & (1 << k)))
          continue;

        pre[i] += a[k][j];
      }
    }
  }

  dp[0] = 0;
  for (int i = 0; i < z; ++i) {
    vi p;

    for (int j = 0; j < n; ++j) {
      if (!(i & (1 << j)))
        p.push_back(j);
    }

    f(0, p, dp[i], i, 0);
  }

  cout << dp[z - 1] << '\n';
}
Problem p03180 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
#define MAX_N 17
#define MAX_M 1000005

ll dp[MAX_M];
ll a[MAX_N][MAX_N];
ll sum[MAX_M];
int n, m;

void dfs(int x) {
  if (dp[x] != -1)
    return;
  dp[x] = sum[x];
  if (__builtin_popcount(x) <= 1)
    return;

  vector<int> f;
  int xx = x;
  rep(i, n) {
    if (xx % 2 == 1)
      f.push_back(i);
    xx /= 2;
  }

  int w = f.size();

  rep(i, 1 << w) {
    int ii = i;
    int y = 0, z = 0;
    rep(j, w) {
      if (ii % 2 == 0)
        y += 1 << f[j];
      else
        z += 1 << f[j];
      ii /= 2;
    }
    if (y == 0 || z == 0)
      continue;
    dfs(y);
    dfs(z);
    dp[x] = max(dp[x], dp[y] + dp[z]);
  }

  return;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];

  m = 1 << n;

  rep(i, m) {
    vector<int> f;
    int ii = i;
    rep(j, n) {
      if (ii % 2 == 1)
        f.push_back(j);
      ii /= 2;
    }
    rep(j, f.size()) {
      srep(k, j + 1, f.size()) { sum[i] += a[f[j]][f[k]]; }
    }
  }

  rep(i, m) dp[i] = -1;

  dfs(m - 1);

  ll ans = dp[m - 1];

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
#define yn                                                                     \
  { puts("Yes"); }                                                             \
  else {                                                                       \
    puts("No");                                                                \
  }
#define MAX_N 17
#define MAX_M 1000005

ll dp[MAX_M];
ll a[MAX_N][MAX_N];
ll sum[MAX_M];
int n, m;

void dfs(int x) {
  if (dp[x] != -1)
    return;
  dp[x] = sum[x];
  if (__builtin_popcount(x) <= 1)
    return;

  for (int i = x; i > 0; --i &= x) {
    int y = i;
    int z = i ^ x;
    if (y == 0 || z == 0)
      continue;
    dfs(y);
    dfs(z);
    dp[x] = max(dp[x], dp[y] + dp[z]);
  }

  return;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> a[i][j];

  m = 1 << n;

  rep(i, m) {
    vector<int> f;
    int ii = i;
    rep(j, n) {
      if (ii % 2 == 1)
        f.push_back(j);
      ii /= 2;
    }
    rep(j, f.size()) {
      srep(k, j + 1, f.size()) { sum[i] += a[f[j]][f[k]]; }
    }
  }

  rep(i, m) dp[i] = -1;

  dfs(m - 1);

  ll ans = dp[m - 1];

  cout << ans << endl;
  return 0;
}
Problem p03180 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 1 << 60
int dp[(1 << 16)];
int sum[(1 << 16)];

int calculate(vector<vector<int>> &score, int mask) {
  int ans = 0;
  for (int i = 0; i < 17; i++) {
    for (int j = i + 1; j < 17; j++) {
      if (((mask & (1 << i)) != 0) && ((mask & (1 << j)) != 0))
        ans += score[i][j];
    }
  }
  return ans;
}
void prepareSum(vector<vector<int>> &score, int n) {
  for (int i = 1; i < (1 << n); i++)
    dp[i] = inf;
  // summing up the values for each and every subset
  for (int submask = 1; submask < (1 << n); submask++)
    sum[submask] = calculate(score, submask);
}

int solve(vector<vector<int>> &score, int mask) {
  if (mask == 0)
    return 0;
  if (dp[mask] != inf)
    return dp[mask];
  int ans = 0;
  for (int submask = mask; submask != 0;
       submask = (submask - 1) & mask) // generating all the subsets of the mask
    ans = max(ans,
              sum[submask] +
                  solve(score, mask ^ submask)); // xoring the submask and mask
  return dp[mask] = ans;
}
signed main() {
  int n;
  cin >> n;
  vector<vector<int>> score(n, vector<int>(n));
  // memset(dp, inf, sizeof dp);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> score[i][j];
  prepareSum(score, n);
  cout << solve(score, (1 << n) - 1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf (int)1 << 60
int dp[(1 << 16)];
int sum[(1 << 16)];

int calculate(vector<vector<int>> &score, int mask) {
  int ans = 0;
  for (int i = 0; i < 17; i++) {
    for (int j = i + 1; j < 17; j++) {
      if (((mask & (1 << i)) != 0) && ((mask & (1 << j)) != 0))
        ans += score[i][j];
    }
  }
  return ans;
}
void prepareSum(vector<vector<int>> &score, int n) {
  for (int i = 1; i < (1 << n); i++)
    dp[i] = inf;
  // summing up the values for each and every subset
  for (int submask = 1; submask < (1 << n); submask++)
    sum[submask] = calculate(score, submask);
}

int solve(vector<vector<int>> &score, int mask) {
  if (mask == 0)
    return 0;
  if (dp[mask] != inf)
    return dp[mask];
  int ans = 0;
  for (int submask = mask; submask != 0;
       submask = (submask - 1) & mask) // generating all the subsets of the mask
    ans = max(ans,
              sum[submask] +
                  solve(score, mask ^ submask)); // xoring the submask and mask
  return dp[mask] = ans;
}
signed main() {
  int n;
  cin >> n;
  vector<vector<int>> score(n, vector<int>(n));
  // memset(dp, inf, sizeof dp);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> score[i][j];
  prepareSum(score, n);
  cout << solve(score, (1 << n) - 1);
}
Problem p03180 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

#define endl '\n'

using namespace std;

const int MAXN = 16;
long long points[MAXN][MAXN], dp[(1 << MAXN) + 3], total[(1 << MAXN) + 3];

void calc_dp(int mask, int curr, int ind, vector<int> missing) {
  if (ind == (int)missing.size()) {
    int new_mask = (mask | curr);
    dp[new_mask] = max(dp[new_mask], dp[mask] + total[curr]);
    return;
  }
  calc_dp(mask, curr, ind + 1, missing);
  calc_dp(mask, (curr | (1 << missing[ind])), ind + 1, missing);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int i2 = 0; i2 < n; ++i2) {
      cin >> points[i][i2];
    }
  }

  for (int mask = 0; mask < (1 << n); ++mask) {
    for (int i = 0; i < n; ++i) {
      for (int i2 = i + 1; i2 < n; ++i2) {
        if ((mask & (1 << i)) and (mask & (1 << i2))) {
          total[mask] += points[i][i2];
        }
      }
    }
  }

  for (int mask = 0; mask < (1 << n); ++mask) {
    vector<int> missing;
    for (int i = 0; i < n; ++i) {
      if (!(mask & (1 << i))) {
        missing.push_back(i);
      }
    }
    calc_dp(mask, 0, 0, missing);
  }

  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

#define endl '\n'

using namespace std;

const int MAXN = 16;
long long points[MAXN][MAXN], dp[(1 << MAXN) + 3], total[(1 << MAXN) + 3];

void calc_dp(int mask, int curr, int ind, const vector<int> &missing) {
  if (ind == (int)missing.size()) {
    int new_mask = (mask | curr);
    dp[new_mask] = max(dp[new_mask], dp[mask] + total[curr]);
    return;
  }
  calc_dp(mask, curr, ind + 1, missing);
  calc_dp(mask, (curr | (1 << missing[ind])), ind + 1, missing);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int i2 = 0; i2 < n; ++i2) {
      cin >> points[i][i2];
    }
  }

  for (int mask = 0; mask < (1 << n); ++mask) {
    for (int i = 0; i < n; ++i) {
      for (int i2 = i + 1; i2 < n; ++i2) {
        if ((mask & (1 << i)) and (mask & (1 << i2))) {
          total[mask] += points[i][i2];
        }
      }
    }
  }

  for (int mask = 0; mask < (1 << n); ++mask) {
    vector<int> missing;
    for (int i = 0; i < n; ++i) {
      if (!(mask & (1 << i))) {
        missing.push_back(i);
      }
    }
    calc_dp(mask, 0, 0, missing);
  }

  cout << dp[(1 << n) - 1] << endl;
  return 0;
}
Problem p03180 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int maxn = 1e5;
LL a[20][20], d[maxn], vis[maxn][20];

inline int num(int c, int t[]) {
  int res = 0;
  for (int i = 0; i < c; ++i) {
    res += 1 << t[i];
  }
  return res;
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < (1 << n); ++i) {
    int b[20], c = 0;
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j)) {
        b[c++] = j;
      }
    }
    for (int j = 0; j < c; ++j) {
      for (int k = j + 1; k < c; ++k) {
        d[i] += a[b[j]][b[k]];
      }
    }
  }
  int num = (1 << n) - 1;
  int c = 0;
  for (int i = 1; i <= num; ++i) {
    // cout << i << endl;
    for (int j = i; j > 0; j = (j - 1) & i) {
      // cout << j << ' ';
      d[i] = max(d[i], d[j] + d[j ^ i]);
      ++c;
    }
    // cout << endl;
  }
  cout << d[num] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
typedef vector<int> vii;
const int maxn = 1e5;
LL a[20][20], d[maxn], vis[maxn][20];

inline int num(int c, int t[]) {
  int res = 0;
  for (int i = 0; i < c; ++i) {
    res += 1 << t[i];
  }
  return res;
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < (1 << n); ++i) {
    int b[20], c = 0;
    for (int j = 0; j < n; ++j) {
      if (i & (1 << j)) {
        b[c++] = j;
      }
    }
    for (int j = 0; j < c; ++j) {
      for (int k = j + 1; k < c; ++k) {
        d[i] += a[b[j]][b[k]];
      }
    }
  }
  int num = (1 << n) - 1;
  int c = 0;
  for (int i = 1; i <= num; ++i) {
    // cout << i << endl;
    for (int j = i; j > 0; j = (j - 1) & i) {
      // cout << j << ' ';
      d[i] = max(d[i], d[j] + d[j ^ i]);
      ++c;
    }
    // cout << endl;
  }
  cout << d[num] << endl;
  return 0;
}
Problem p03180 (C++)

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

#define lint long long

const int N = 16;
lint cost[N][N];
int n;
lint subsetCost[1 << N];
lint dp[1 << N];

lint solve(int mask) {
  if (__builtin_popcount(mask) == n)
    return 0;
  if (dp[mask] != -1)
    return dp[mask];
  lint sum = 0;

  vector<int> indices;
  indices.clear();
  for (int i = 0; i < n; i++)
    if (!(mask & (1 << i)))
      indices.push_back(i);
  int sz = indices.size();

  for (int bitmask = 1; bitmask < (1 << sz); bitmask++) {
    int num = 0;
    for (int i = 0; i < sz; i++)
      if ((1 << i) & bitmask)
        num += (1 << indices[i]);

    if (!(num & mask))
      sum = max(sum, subsetCost[num] + solve(mask | num));
  }
  return dp[mask] = sum;
}

int main() {
  cin >> n;
  vector<int> subset;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> cost[i][j];

  for (int mask = 0; mask < (1 << n); mask++) {
    subset.clear();
    for (int i = 0; i < n; i++)
      if (mask & (1 << i))
        subset.push_back(i);
    lint c = 0;
    for (int x : subset)
      for (int y : subset)
        c += cost[x][y];
    subsetCost[mask] = c / 2;
  }
  // cout << "DONE!" << endl ;
  memset(dp, -1, sizeof(dp));
  cout << solve(0);
}


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

#define lint long long

const int N = 16;
lint cost[N][N];
int n;
lint subsetCost[1 << N];
lint dp[1 << N];

lint solve(int mask) {
  if (__builtin_popcount(mask) == n)
    return 0;
  if (dp[mask] != -1)
    return dp[mask];
  lint sum = 0;

  vector<int> indices;
  indices.clear();
  for (int i = 0; i < n; i++)
    if (!(mask & (1 << i)))
      indices.push_back(i);
  int sz = indices.size();

  for (int bitmask = 1; bitmask < (1 << sz); bitmask++) {
    int num = 0;
    for (int i = 0; i < sz; i++)
      if ((1 << i) & bitmask)
        num += (1 << indices[i]);

    if (!(num & mask))
      sum = max(sum, subsetCost[num] + solve(mask | num));
  }
  return dp[mask] = sum;
}

int main() {
  cin >> n;
  vector<int> subset;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> cost[i][j];

  for (int mask = 0; mask < (1 << n); mask++) {
    subset.clear();
    for (int i = 0; i < n; i++)
      if (mask & (1 << i))
        subset.push_back(i);
    lint c = 0;
    for (int x : subset)
      for (int y : subset)
        c += cost[x][y];
    subsetCost[mask] = c / 2;
  }
  // cout << "DONE!" << endl ;
  dp[0] = 0;
  for (int mask = 1; mask < (1 << n); mask++) {
    lint sum = 0;
    vector<int> indices;
    indices.clear();
    for (int i = 0; i < n; i++)
      if ((mask & (1 << i)))
        indices.push_back(i);
    int sz = indices.size();

    for (int bitmask = 0; bitmask < (1 << sz); bitmask++) {
      int num = 0;
      for (int i = 0; i < sz; i++)
        if ((1 << i) & bitmask)
          num += (1 << indices[i]);

      sum = max(sum, subsetCost[num] + dp[num ^ mask]);
    }
    dp[mask] = sum;
  }
  // memset(dp, -1, sizeof(dp));
  cout << dp[(1 << n) - 1]; // cout << solve(0);
}
Problem p03180 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 16;

int matrix[MAXN + 7][MAXN + 7];
long long cost[MAXN + 7];
long long dp[(1 << MAXN) + 7];

vector<int> masks_by_size[MAXN + 7];
int N;

bool checkbit(int mask, int i) { return (mask >> i) & 1; }

void precalc() {
  for (int mask = 1; mask < (1 << N); mask++) {
    // cerr << mask << " " << __builtin_popcount(mask) << "\n";
    masks_by_size[__builtin_popcount(mask)].push_back(mask);

    for (int i = 1; i <= N; i++) {
      if (checkbit(mask, i - 1) == true) {
        for (int j = i + 1; j <= N; j++) {
          if (checkbit(mask, j - 1) == true) {
            cost[mask] += matrix[i][j];
          }
        }
      }
    }
  }
}

long long dpf(int mask) {
  // cerr << mask << "\n";
  long long &d = dp[mask];

  if (d != -1) {
    return d;
  }

  if (mask == (1 << N) - 1) {
    return d = 0;
  }

  d = 0;

  for (int bit_cnt = 1; bit_cnt <= N / 2; bit_cnt++) {
    for (int mask2 : masks_by_size[bit_cnt]) {
      if ((mask & mask2) == 0) {
        // cerr << mask << " + " << mask2 << " -> " << cost[mask2] << " " <<
        // (mask | mask2) << "\n";
        d = max(d, cost[mask2] + dpf(mask | mask2));
      }
    }
  }

  d = max(d, cost[((1 << N) - 1) ^ mask]);

  return d;
}

int main() {
  memset(dp, -1, sizeof dp);

  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> matrix[i][j];
    }
  }

  precalc();

  cout << dpf(0) << "\n";

  return 0;
}

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

using namespace std;

const int MAXN = 16;

int matrix[MAXN + 7][MAXN + 7];
long long cost[(1 << MAXN) + 7];
long long dp[(1 << MAXN) + 7];

vector<int> masks_by_size[MAXN + 7];
int N;

bool checkbit(int mask, int i) { return (mask >> i) & 1; }

void precalc() {
  for (int mask = 1; mask < (1 << N); mask++) {
    // cerr << mask << " " << __builtin_popcount(mask) << "\n";
    masks_by_size[__builtin_popcount(mask)].push_back(mask);

    for (int i = 1; i <= N; i++) {
      if (checkbit(mask, i - 1) == true) {
        for (int j = i + 1; j <= N; j++) {
          if (checkbit(mask, j - 1) == true) {
            cost[mask] += matrix[i][j];
          }
        }
      }
    }
  }
}

long long dpf(int mask) {
  // cerr << mask << "\n";
  long long &d = dp[mask];

  if (d != -1) {
    return d;
  }

  if (mask == (1 << N) - 1) {
    return d = 0;
  }

  d = 0;

  for (int bit_cnt = 1; bit_cnt <= N / 2; bit_cnt++) {
    for (int mask2 : masks_by_size[bit_cnt]) {
      if ((mask & mask2) == 0) {
        // cerr << mask << " + " << mask2 << " -> " << cost[mask2] << " " <<
        // (mask | mask2) << "\n";
        d = max(d, cost[mask2] + dpf(mask | mask2));
      }
    }
  }

  d = max(d, cost[((1 << N) - 1) ^ mask]);

  return d;
}

int main() {
  memset(dp, -1, sizeof dp);

  cin >> N;

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> matrix[i][j];
    }
  }

  precalc();

  cout << dpf(0) << "\n";

  return 0;
}
Problem p03180 (C++)

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

int main() {
  int n;
  cin >> n;
  long long pair[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> pair[i][j];
    }
  }
  vector<long long> dp((1 << n), 0);
  for (int bit = 0; bit < (1 << n); bit++) {
    bitset<16> b = bit;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (b[i] && b[j]) {
          dp[bit] += pair[i][j];
        }
      }
    }
  }
  for (int k = 2; k <= n; k++) {
    for (int bit = 0; bit < (1 << n); bit++) {
      bitset<16> b = bit;
      if (b.count() != k)
        continue;
      for (int bit2 = 0; bit2 < (1 << n); bit2++) {
        if ((bit | bit2) ^ bit)
          continue;
        dp[bit] = max(dp[bit], dp[bit2] + dp[bit ^ bit2]);
      }
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  long long pair[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> pair[i][j];
    }
  }
  vector<long long> dp((1 << n), 0);
  for (int bit = 0; bit < (1 << n); bit++) {
    bitset<16> b = bit;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (b[i] && b[j]) {
          dp[bit] += pair[i][j];
        }
      }
    }
  }
  for (int k = 2; k <= n; k++) {
    for (int bit = 0; bit < (1 << n); bit++) {
      bitset<16> b = bit;
      if (b.count() != k)
        continue;
      for (int bit2 = bit; bit2 > 0; bit2 = (bit2 - 1) & bit) {
        dp[bit] = max(dp[bit], dp[bit2] + dp[bit ^ bit2]);
      }
    }
  }
  cout << dp[(1 << n) - 1] << endl;
  return 0;
}