Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[300010], dp[300010], b[300010];
ll mp[512][100001];
int main() {
  int n;
  while (cin >> n) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", b + i, a + i);
    }
    mp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      dp[i] = dp[i / 2] + 1;
      int tmp = a[i], tmp1 = b[i];
      if (dp[i] <= 9) {
        for (int j = 0; j <= 100000; ++j)
          mp[i][j] = mp[i / 2][j];
        for (int it = 0; it <= 100000 - tmp; ++it) {
          mp[i][it + tmp] = max(mp[i][it + tmp], mp[i / 2][it] + tmp1);
        }
        ll tmp2 = 0, tmp3;
        for (int it = 0; it <= 100000; ++it) {
          mp[i][it] = max(tmp2, mp[i][it]);
          tmp2 = mp[i][it];
        }
      }
    }
    int q;
    scanf("%d", &q);
    int x, l;
    vector<pair<int, int>> v;
    for (int i = 0; i < q; ++i) {
      scanf("%d%d", &x, &l);
      v.clear();
      while (dp[x] > 9) {
        v.push_back(mk(a[x], b[x]));
        x /= 2;
      }
      ans = 0;
      for (int i = 0; i < (1 << v.size()); ++i) {
        int tmp = 0;
        ll tmp1 = 0, tmp2;
        for (int j = 0; j < v.size(); ++j) {
          if (i & (1 << j)) {
            tmp += v[j].fs;
            tmp1 += v[j].sc;
            if (tmp > l) {
              break;
            }
          }
        }
        if (tmp > l) {
          continue;
        }
        tmp2 = mp[x][l - tmp];
        ans = max(ans, tmp2 + tmp1);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[300010], dp[300010], b[300010];
ll mp[512][100001];
int main() {
  int n;
  while (cin >> n) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", b + i, a + i);
    }
    mp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      dp[i] = dp[i / 2] + 1;
      int tmp = a[i], tmp1 = b[i];
      for (int j = 0; j <= 100000; ++j)
        mp[i][j] = mp[i / 2][j];
      if (dp[i] <= 9) {
        for (int it = 0; it <= 100000 - tmp; ++it) {
          mp[i][it + tmp] = max(mp[i][it + tmp], mp[i / 2][it] + tmp1);
        }
        ll tmp2 = 0, tmp3;
        for (int it = 0; it <= 100000; ++it) {
          mp[i][it] = max(tmp2, mp[i][it]);
          tmp2 = mp[i][it];
        }
      }
    }
    int q;
    scanf("%d", &q);
    int x, l;
    vector<pair<int, int>> v;
    for (int i = 0; i < q; ++i) {
      scanf("%d%d", &x, &l);
      v.clear();
      while (dp[x] > 9) {
        v.push_back(mk(a[x], b[x]));
        x /= 2;
      }
      ans = 0;
      for (int i = 0; i < (1 << v.size()); ++i) {
        int tmp = 0;
        ll tmp1 = 0, tmp2;
        for (int j = 0; j < v.size(); ++j) {
          if (i & (1 << j)) {
            tmp += v[j].fs;
            tmp1 += v[j].sc;
            if (tmp > l) {
              break;
            }
          }
        }
        if (tmp > l) {
          continue;
        }
        tmp2 = mp[x][l - tmp];
        ans = max(ans, tmp2 + tmp1);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}