Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define mmin(a, b) a = min(a, (b))
#define mmax(a, b) a = max(a, (b))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ALL(a) a.begin(), a.end()
#define sqr(x) (x) * (x)
#define fori(i, n) for (int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;

const double PI = 2 * acos(0.0);

template <class T> void show(const vector<T> &a) {
  for (T x : a)
    cout << x << " ";
  cout << endl;
}

const ll oo = 1e18;

const int N = 20;

int ans[1 << N];

string bin(int m) {
  stringstream ss;
  while (m) {
    if (m % 2 == 1)
      ss << "1";
    else
      ss << "0";
    m /= 2;
  }
  string s = ss.str();
  reverse(ALL(s));
  return s;
}

set<pii> dp[1 << N];
int a[N];
int n;

void upd(set<pii> &a, set<pii> b) {
  a.insert(ALL(b));
  while (SZ(a) > 2)
    a.erase(a.begin());
}

void dfs(int v) {
  if (SZ(dp[v]))
    return;
  dp[v].insert(mp(a[v], v));
  for (int i = 0; i < n; ++i)
    if ((v >> i) & 1) {
      dfs(v ^ (1 << i));
      upd(dp[v], dp[v ^ (1 << i)]);
    }
}

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

  cin >> n;
  fori(i, (1 << n)) { cin >> a[i]; }
  dfs((1 << n) - 1);

  for (int i = 0; i < (1 << n); ++i) {
    if (SZ(dp[i]) >= 2)
      ans[i] = (*dp[i].begin()).first + (*next(dp[i].begin())).first;
    if (i)
      mmax(ans[i], ans[i - 1]);
  }
  for (int i = 1; i < (1 << n); ++i)
    cout << ans[i] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define mmin(a, b) a = min(a, (b))
#define mmax(a, b) a = max(a, (b))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ALL(a) a.begin(), a.end()
#define sqr(x) (x) * (x)
#define fori(i, n) for (int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;

const double PI = 2 * acos(0.0);

template <class T> void show(const vector<T> &a) {
  for (T x : a)
    cout << x << " ";
  cout << endl;
}

const ll oo = 1e18;

const int N = 20;

int ans[1 << N];

string bin(int m) {
  stringstream ss;
  while (m) {
    if (m % 2 == 1)
      ss << "1";
    else
      ss << "0";
    m /= 2;
  }
  string s = ss.str();
  reverse(ALL(s));
  return s;
}

set<pii> dp[1 << N];
int a[1 << N];
int n;

void upd(set<pii> &a, set<pii> b) {
  a.insert(ALL(b));
  while (SZ(a) > 2)
    a.erase(a.begin());
}

void dfs(int v) {
  if (SZ(dp[v]))
    return;
  dp[v].insert(mp(a[v], v));
  for (int i = 0; i < n; ++i)
    if ((v >> i) & 1) {
      dfs(v ^ (1 << i));
      upd(dp[v], dp[v ^ (1 << i)]);
    }
}

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

  cin >> n;
  fori(i, (1 << n)) { cin >> a[i]; }
  dfs((1 << n) - 1);

  for (int i = 0; i < (1 << n); ++i) {
    if (SZ(dp[i]) >= 2)
      ans[i] = (*dp[i].begin()).first + (*next(dp[i].begin())).first;
    if (i)
      mmax(ans[i], ans[i - 1]);
  }
  for (int i = 1; i < (1 << n); ++i)
    cout << ans[i] << endl;
}