Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < (int)(n); i++)
#define N 200010
vector<pair<int, int>> a[N];
vector<pair<int, int>> aa;
vector<pair<int, int>> b;
int val[N];
int wgt[N];
int n, k, nn, kk, mx, sz, q, lim, vec, xx, yy, zz;
int x, y, z;
int ans;
int main() {
  scanf("%d", &n);
  x = n + 1;
  y = 0;
  while (x > 1) {
    x /= 2;
    y++;
  }
  y = min(11, y);
  k = 1;
  f(i, y) k *= 2;
  f(i, n) { scanf("%d %d", &val[i + 1], &wgt[i + 1]); }
  a[1].push_back(make_pair(0, 0));
  a[1].push_back(make_pair(wgt[1], val[1]));
  for (int i = 2; i < k; i++) {
    x = i / 2;
    sz = a[x].size();
    aa = a[x];
    f(j, sz) {
      y = a[x][j].first + wgt[i];
      z = a[x][j].second + val[i];
      aa.push_back(make_pair(y, z));
    }
    sort(aa.begin(), aa.end());
    sz *= 2;
    mx = -1;
    f(j, sz) {
      if (mx < aa[j].second) {
        a[i].push_back(aa[j]);
        mx = aa[j].second;
      }
    }
  }
  scanf("%d", &q);
  f(qq, q) {
    scanf("%d %d", &vec, &lim);
    b.clear();
    b.push_back(make_pair(0, 0));
    while (vec >= k) {
      aa = b;
      sz = b.size();
      f(j, sz) {
        y = b[j].first + wgt[vec];
        z = b[j].second + val[vec];
        aa.push_back(make_pair(y, z));
      }
      sort(aa.begin(), aa.end());
      b.clear();
      sz *= 2;
      mx = -1;
      f(j, sz) {
        if (mx < aa[j].second) {
          b.push_back(aa[j]);
          mx = aa[j].second;
        }
      }
      vec /= 2;
    }
    sz = a[vec].size();
    x = 0;
    y = b.size();
    y--;
    ans = 0;
    while ((x < sz) && (y >= 0)) {
      xx = (a[vec][x].first) + (b[y].first);
      yy = (a[vec][x].second) + (b[y].second);
      if (xx <= lim) {
        ans = max(ans, yy);
        x++;
      } else {
        y--;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < (int)(n); i++)
#define N 800010
vector<pair<int, int>> a[N];
vector<pair<int, int>> aa;
vector<pair<int, int>> b;
int val[N];
int wgt[N];
int n, k, nn, kk, mx, sz, q, lim, vec, xx, yy, zz;
int x, y, z;
int ans;
int main() {
  scanf("%d", &n);
  x = n + 1;
  y = 0;
  while (x > 1) {
    x /= 2;
    y++;
  }
  y = min(11, y);
  k = 1;
  f(i, y) k *= 2;
  f(i, n) { scanf("%d %d", &val[i + 1], &wgt[i + 1]); }
  a[1].push_back(make_pair(0, 0));
  a[1].push_back(make_pair(wgt[1], val[1]));
  for (int i = 2; i < k; i++) {
    x = i / 2;
    sz = a[x].size();
    aa = a[x];
    f(j, sz) {
      y = a[x][j].first + wgt[i];
      z = a[x][j].second + val[i];
      aa.push_back(make_pair(y, z));
    }
    sort(aa.begin(), aa.end());
    sz *= 2;
    mx = -1;
    f(j, sz) {
      if (mx < aa[j].second) {
        a[i].push_back(aa[j]);
        mx = aa[j].second;
      }
    }
  }
  scanf("%d", &q);
  f(qq, q) {
    scanf("%d %d", &vec, &lim);
    b.clear();
    b.push_back(make_pair(0, 0));
    while (vec >= k) {
      aa = b;
      sz = b.size();
      f(j, sz) {
        y = b[j].first + wgt[vec];
        z = b[j].second + val[vec];
        aa.push_back(make_pair(y, z));
      }
      sort(aa.begin(), aa.end());
      b.clear();
      sz *= 2;
      mx = -1;
      f(j, sz) {
        if (mx < aa[j].second) {
          b.push_back(aa[j]);
          mx = aa[j].second;
        }
      }
      vec /= 2;
    }
    sz = a[vec].size();
    x = 0;
    y = b.size();
    y--;
    ans = 0;
    while ((x < sz) && (y >= 0)) {
      xx = (a[vec][x].first) + (b[y].first);
      yy = (a[vec][x].second) + (b[y].second);
      if (xx <= lim) {
        ans = max(ans, yy);
        x++;
      } else {
        y--;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}