Problem p02798 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

void cmin(int &lhs, int rhs) { lhs = min(lhs, rhs); }
const int INF = 1e9;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);

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

  vector<vector<int>> dp(1 << n, vector<int>(n, INF));
  dp[0][0] = 0;

  auto build = [&](int cnt, int id) {
    if (cnt < 0)
      return 0;
    else if ((cnt - id) % 2)
      return b[id];
    else
      return a[id];
  };

  for (int bit = 0; bit < ((1 << n) - 1); bit++) {
    int pc = __builtin_popcount(bit);

    for (int i = 0; i < n; i++) {
      int x = build(pc - 1, i);
      int piyo = pc;
      for (int j = 0; j < n; j++) {
        if ((bit >> j) & 1) {
          piyo--;
          continue;
        }
        int y = build(pc, j);
        if (x <= y) {
          int nexBit = bit | (1 << j);
          cmin(dp[nexBit][j], dp[bit][i] + piyo);
        }
      }
    }
  }
  int ans = *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end());
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    assert(ans % 2 == 0);
    cout << ans / 2 << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

void cmin(int &lhs, int rhs) { lhs = min(lhs, rhs); }
const int INF = 1e9;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n);

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

  vector<vector<int>> dp(1 << n, vector<int>(n, INF));
  dp[0][0] = 0;

  auto build = [&](int cnt, int id) {
    if (cnt < 0)
      return 0;
    else if ((cnt - id) % 2)
      return b[id];
    else
      return a[id];
  };

  for (int bit = 0; bit < ((1 << n) - 1); bit++) {
    int pc = __builtin_popcount(bit);

    for (int i = 0; i < n; i++) {
      int x = build(pc - 1, i);
      int piyo = pc;
      for (int j = 0; j < n; j++) {
        if ((bit >> j) & 1) {
          piyo--;
          continue;
        }
        int y = build(pc, j);
        if (x <= y) {
          int nexBit = bit | (1 << j);
          cmin(dp[nexBit][j], dp[bit][i] + piyo);
        }
      }
    }
  }
  int ans = *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end());
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}