Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200010;
int ma[maxn], ma2[maxn];
ll a[maxn];
int main() {
  // freopen("text.in","r",stdin);
  int n, i, j, tmp;
  scanf("%d", &n);
  n = (1 << n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  memset(ma, -1, sizeof(ma));
  ma[0] = 0;
  memset(ma2, -1, sizeof(ma2));
  for (i = 1; i < n; i++) {
    for (j = 0; j <= 20; j++) {
      if ((1 << j) & i) {
        tmp = i ^ (1 << j);
        /*if (i==7) {
            printf("now==%d %d %d\n",tmp,ma[tmp],ma2[tmp]);
            printf("tmp==%d ma=%d ma2=%d\n",tmp,ma[i],ma2[i]);
        }*/
        if (a[ma[tmp]] > a[ma[i]]) {
          ma2[i] = ma[i];
          ma[i] = ma[tmp];
          if (ma2[tmp] != -1) {
            if (a[ma2[tmp]] > a[ma2[i]])
              ma2[i] = ma2[tmp];
          }
        } else {
          // printf("t==%d %d\n",ma[tmp],ma2[i]);
          if (ma[tmp] != ma[i] && a[ma[tmp]] > a[ma2[i]]) {
            ma2[i] = ma[tmp];
          } else {
            if (ma2[tmp] != ma[i] && a[ma2[tmp]] > a[ma2[i]]) {
              ma2[i] = ma2[tmp];
            }
          }
        }
        /*if (i==7) {
            printf("2==tmp==%d ma=%d ma2=%d\n",tmp,ma[i],ma2[i]);
        }*/
      }
    }
    if (a[i] > a[ma[i]]) {
      ma2[i] = ma[i];
      ma[i] = i;
    } else {
      if (a[i] > a[ma2[i]])
        ma2[i] = i;
    }
  }
  /*for (i=1;i<n;i++) {
      printf("i=%d %d %d\n",i,ma[i],ma2[i]);
  }*/
  ll ans = 0;
  for (i = 1; i < n; i++) {
    ans = max(a[ma[i]] + a[ma2[i]], ans);
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 500010;
int ma[maxn], ma2[maxn];
ll a[maxn];
int main() {
  // freopen("text.in","r",stdin);
  int n, i, j, tmp;
  scanf("%d", &n);
  n = (1 << n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  memset(ma, -1, sizeof(ma));
  ma[0] = 0;
  memset(ma2, -1, sizeof(ma2));
  for (i = 1; i < n; i++) {
    for (j = 0; j <= 20; j++) {
      if ((1 << j) & i) {
        tmp = i ^ (1 << j);
        /*if (i==7) {
            printf("now==%d %d %d\n",tmp,ma[tmp],ma2[tmp]);
            printf("tmp==%d ma=%d ma2=%d\n",tmp,ma[i],ma2[i]);
        }*/
        if (a[ma[tmp]] > a[ma[i]]) {
          ma2[i] = ma[i];
          ma[i] = ma[tmp];
          if (ma2[tmp] != -1) {
            if (a[ma2[tmp]] > a[ma2[i]])
              ma2[i] = ma2[tmp];
          }
        } else {
          // printf("t==%d %d\n",ma[tmp],ma2[i]);
          if (ma[tmp] != ma[i] && a[ma[tmp]] > a[ma2[i]]) {
            ma2[i] = ma[tmp];
          } else {
            if (ma2[tmp] != ma[i] && a[ma2[tmp]] > a[ma2[i]]) {
              ma2[i] = ma2[tmp];
            }
          }
        }
        /*if (i==7) {
            printf("2==tmp==%d ma=%d ma2=%d\n",tmp,ma[i],ma2[i]);
        }*/
      }
    }
    if (a[i] > a[ma[i]]) {
      ma2[i] = ma[i];
      ma[i] = i;
    } else {
      if (a[i] > a[ma2[i]])
        ma2[i] = i;
    }
  }
  /*for (i=1;i<n;i++) {
      printf("i=%d %d %d\n",i,ma[i],ma2[i]);
  }*/
  ll ans = 0;
  for (i = 1; i < n; i++) {
    ans = max(a[ma[i]] + a[ma2[i]], ans);
    printf("%lld\n", ans);
  }
  return 0;
}