Problem p03313 (C++)

Buggy submission (status: Runtime Error):
// #pragma comment(linker,"/STACK:102400000,102400000")
// #include <initializer_list>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map>
// #include <unordered_set>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define eps 1e-8
#define LL_INF 0x3fffffffffffffff
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define pper(i, n, m) for (int i = n; i >= m; i--)
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define slla(n) scanf("%lld", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define DE(val) cout << #val << ": " << val << endl;
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  repp(i, 1, ___T)

const int dx44[5] = {0, -1, -1, 1, 1};
const int dy44[5] = {0, -1, 1, -1, 1};

const int dx4[5] = {0, -1, 0, 1, 0};
const int dy4[5] = {0, 0, 1, 0, -1};
const int dx8[9] = {0, -1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
const int dx82[9] = {0, -1, -1, 1, 1, 2, 2, -2, -2};
const int dy82[9] = {0, 2, -2, 2, -2, 1, -1, 1, -1};
const int maxn = 2e5 + 50;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;

ll po(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  } else {
    return gcd(b % a, a);
  }
}
void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    extgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
ll inverse(ll a, ll n) {
  ll d, x, y;
  extgcd(a, n, d, x, y);
  return d == 1 ? (x + n) % n : -1;
}

void YES() {
  puts("YES");
  exit(0);
}
void Yes() { puts("Yes"); } // exit(0); }
void NO() {
  puts("NO");
  exit(0);
}
void No() { puts("No"); } // exit(0); }
void one() {
  puts("-1");
  exit(0);
}

int a[maxn];

struct node {
  int a, b;
} Max[maxn];

void f(int x, node &b) {
  if (b.a == x || b.b == x)
    return;
  if (a[b.a] < a[x])
    swap(b.a, b.b), b.a = x;
  else if (a[b.b] < a[x])
    b.b = x;
}

void solve() {
  int n;
  scanf("%d", &n);
  int N = 1 << n;
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    Max[i].a = a[0] > a[i] ? 0 : i;
    Max[i].b = a[0] > a[i] ? i : 0;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        int k = i ^ (1 << j);
        f(Max[k].a, Max[i]);
        f(Max[k].b, Max[i]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i < N; i++) {
    ans = max(ans, a[Max[i].a] + a[Max[i].b]);
    printf("%d\n", ans);
  }
}

int main() {

  solve();
  // system("pause");
  return 0;
}

Fixed submission:
// #pragma comment(linker,"/STACK:102400000,102400000")
// #include <initializer_list>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <unordered_map>
// #include <unordered_set>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define eps 1e-8
#define LL_INF 0x3fffffffffffffff
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a, b, sizeof(a))
#define pper(i, n, m) for (int i = n; i >= m; i--)
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define slla(n) scanf("%lld", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define DE(val) cout << #val << ": " << val << endl;
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  repp(i, 1, ___T)

const int dx44[5] = {0, -1, -1, 1, 1};
const int dy44[5] = {0, -1, 1, -1, 1};

const int dx4[5] = {0, -1, 0, 1, 0};
const int dy4[5] = {0, 0, 1, 0, -1};
const int dx8[9] = {0, -1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[9] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
const int dx82[9] = {0, -1, -1, 1, 1, 2, 2, -2, -2};
const int dy82[9] = {0, 2, -2, 2, -2, 1, -1, 1, -1};
const int maxn = 5e6 + 50;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;

ll po(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (a == 0) {
    return b;
  } else {
    return gcd(b % a, a);
  }
}
void extgcd(ll a, ll b, ll &d, ll &x, ll &y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    extgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
ll inverse(ll a, ll n) {
  ll d, x, y;
  extgcd(a, n, d, x, y);
  return d == 1 ? (x + n) % n : -1;
}

void YES() {
  puts("YES");
  exit(0);
}
void Yes() { puts("Yes"); } // exit(0); }
void NO() {
  puts("NO");
  exit(0);
}
void No() { puts("No"); } // exit(0); }
void one() {
  puts("-1");
  exit(0);
}

int a[maxn];

struct node {
  int a, b;
} Max[maxn];

void f(int x, node &b) {
  if (b.a == x || b.b == x)
    return;
  if (a[b.a] < a[x])
    swap(b.a, b.b), b.a = x;
  else if (a[b.b] < a[x])
    b.b = x;
}

void solve() {
  int n;
  scanf("%d", &n);
  int N = 1 << n;
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    Max[i].a = a[0] > a[i] ? 0 : i;
    Max[i].b = a[0] > a[i] ? i : 0;
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        int k = i ^ (1 << j);
        f(Max[k].a, Max[i]);
        f(Max[k].b, Max[i]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i < N; i++) {
    ans = max(ans, a[Max[i].a] + a[Max[i].b]);
    printf("%d\n", ans);
  }
}

int main() {

  solve();
  // system("pause");
  return 0;
}