alexjercan/bugnet huggingface dataset:
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back

typedef long long ll;

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 5;

int v[300005];
int w[300005];

ll dp[555][100005];

ll rek(int pos, int l) {
  if (pos < 512)
    return dp[pos][l];
  if (l < w[pos])
    return rek(pos / 2, l);
  return max(rek(pos / 2, l), rek(pos / 2, l - w[pos]) + v[pos]);
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", v + i, w + i);
  }
  for (int i = 1; i < 512; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
    }
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int pos, l;
    scanf("%d%d", &pos, &l);
    printf("%lld\\n", rek(pos, l));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back

typedef long long ll;

const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 5;

int v[300005];
int w[300005];

ll memo[1055][100005];

ll dp(int pos, int l) {
  if (pos == 0)
    return 0;
  if (memo[pos][l] != -1)
    return memo[pos][l];
  if (l < w[pos])
    return memo[pos][l] = dp(pos / 2, l);
  return memo[pos][l] = max(dp(pos / 2, l), dp(pos / 2, l - w[pos]) + v[pos]);
}

ll rek(int pos, int l) {
  if (pos < 1024)
    return dp(pos, l);
  if (l < w[pos])
    return rek(pos / 2, l);
  return max(rek(pos / 2, l), rek(pos / 2, l - w[pos]) + v[pos]);
}

int main() {
  memset(memo, -1, sizeof memo);
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", v + i, w + i);
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int pos, l;
    scanf("%d%d", &pos, &l);
    printf("%lld\\n", rek(pos, l));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;

const int maxn = 1 << 19;
int N, Q, V[maxn], W[maxn];
int depth[maxn];
ll dp[550][100001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
    depth[i] = depth[i / 2] + 1;
  }
  for (int i = 1; i <= N; i++) {
    if (depth[i] > 9)
      break;
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (W[i] <= j)
        dp[i][j] = max(dp[i][j], dp[i / 2][j - W[i]] + V[i]);
      if (j)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    if (depth[v] <= 9) {
      cout << dp[v][L] << '\\n';
    } else {
      vector<pair<int, int>> vv;
      do {
        vv.push_back({V[v], W[v]});
        v /= 2;
      } while (depth[v] > 9);
      int n = vv.size();
      ll ans = 0;
      for (int i = 0; i < (1 << n); i++) {
        ll val = 0, wt = 0;
        for (int j = 0; j < n; j++) {
          if (i & (1 << j)) {
            val += vv[j].first;
            wt += vv[j].second;
          }
        }
        if (wt > L)
          continue;
        ans = max(ans, dp[v][L - wt] + val);
      }
      cout << ans << '\\n';
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;

const int maxn = 1 << 19;
int N, Q, V[maxn], W[maxn];

gp_hash_table<int, ll> dp[maxn];

ll f(int v, int L) {
  if (v == 0)
    return 0;
  if (dp[v].find(L) != dp[v].end())
    return dp[v][L];
  dp[v][L] = f(v / 2, L);
  if (W[v] <= L)
    dp[v][L] = max(dp[v][L], f(v / 2, L - W[v]) + V[v]);
  return dp[v][L];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    ll ans = f(v, L);
    cout << ans << '\\n';
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef double db;
typedef vector<int> vi;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;
int f[(1 << 9) + 10][100010];
int v[(1 << 18) + 10], w[(1 << 18) + 10];
int dfs(int x, int ws) {
  if (x < (1 << 9))
    return f[x][ws];
  if (w[x] <= ws)
    return max(dfs(x / 2, ws), dfs(x / 2, ws - w[x]) + v[x]);
  return dfs(x / 2, ws);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n;
  rep(i, 1, n + 1) cin >> v[i] >> w[i];
  rep(i, 1, min(n + 1, 1 << 9)) {
    rep(j, 0, 1e5 + 1) {
      f[i][j] = f[i / 2][j];
      if (j >= w[i])
        f[i][j] = max(f[i][j], f[i / 2][j - w[i]] + v[i]);
    }
  }
  int query, weight;
  cin >> q;
  rep(i, 0, q) {
    cin >> query >> weight;
    if (query < (1 << 9)) {
      cout << f[query][weight] << '\\n';
    } else {
      cout << dfs(query, weight) << '\\n';
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef double db;
typedef vector<int> vi;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;
map<int, int> t[1 << 18];
int w[maxn], v[maxn];
int cw[1 << 19], cv[1 << 19];
int dfs(int vt, int l) {
  if (!vt)
    return 0;
  if (l > cv[vt])
    return cw[vt];
  if (t[vt][l] != 0)
    return t[vt][l] == -1 ? 0 : t[vt][l];
  int s = dfs(vt / 2, l);
  if (v[vt] <= l)
    s = max(s, dfs(vt / 2, l - v[vt]) + w[vt]);
  t[vt][l] = (s == 0 ? -1 : s);
  return s;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, 1, n + 1) cin >> w[i] >> v[i];
  rep(i, 1, n + 1) {
    cw[i] = cw[i / 2] + w[i];
    cv[i] = cv[i / 2] + v[i];
  }
  int q;
  cin >> q;
  rep(i, 0, q) {
    int vt, l;
    cin >> vt >> l;
    cout << dfs(vt, l) << '\\n';
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, Q;
#define Maxn 100010
int V[Maxn * 10], W[Maxn * 10];
int f[512][100010];
int seq1[Maxn], seq2[Maxn], cnt;
int to[Maxn];
int val1[Maxn], val2[Maxn];
void dfs(int u) {
  if (u >= 512)
    return;
  if (u > n)
    return;
  int fa = u / 2;
  for (int i = 0; i <= 100000; ++i) {
    f[u][i] = f[fa][i];
    if (i >= W[u])
      f[u][i] = max(f[u][i], f[fa][i - W[u]] + V[u]);
  }
  dfs(u << 1);
  dfs(u << 1 | 1);
}

int lowbit(int x) { return x & (-x); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &V[i], &W[i]);
  }
  dfs(1);
  scanf("%d", &Q);
  int u, L;
  for (int i = 0; i < 9; ++i)
    to[(1 << i)] = i;
  while (Q--) {
    scanf("%d%d", &u, &L);
    if (u < 512)
      printf("%d\\n", f[u][L]);
    else {
      cnt = 0;
      while (u >= 512) {
        seq1[cnt] = W[u];
        seq2[cnt] = V[u];
        u >>= 1;
        cnt++;
      }
      int Ans = f[u][L];
      for (int i = 1; i < (1 << cnt); ++i) {
        int d = lowbit(i);
        val1[i] = val1[i ^ d] + seq1[to[d]];
        val2[i] = val2[i ^ d] + seq2[to[d]];
        if (val1[i] <= L)
          Ans = max(Ans, f[u][L - val1[i]] + val2[i]);
      }
      printf("%d\\n", Ans);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, Q;
#define Maxn 100010
int V[Maxn * 10], W[Maxn * 10];
int f[512][100010];
int seq1[Maxn], seq2[Maxn], cnt;
int to[Maxn];
int val1[Maxn], val2[Maxn];
void dfs(int u) {
  if (u >= 512)
    return;
  if (u > n)
    return;
  int fa = u / 2;
  for (int i = 0; i <= 100000; ++i) {
    f[u][i] = f[fa][i];
    if (i >= W[u])
      f[u][i] = max(f[u][i], f[fa][i - W[u]] + V[u]);
  }
  dfs(u << 1);
  dfs(u << 1 | 1);
}

int lowbit(int x) { return x & (-x); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &V[i], &W[i]);
  }
  dfs(1);
  scanf("%d", &Q);
  int u, L;
  for (int i = 0; i < 9; ++i)
    to[(1 << i)] = i;
  while (Q--) {
    scanf("%d%d", &u, &L);
    if (u < 512)
      printf("%d\\n", f[u][L]);
    else {
      cnt = 0;
      while (u >= 4) {
        seq1[cnt] = W[u];
        seq2[cnt] = V[u];
        u >>= 1;
        cnt++;
      }
      int Ans = f[u][L];
      for (int i = 1; i < (1 << cnt); ++i) {
        int d = lowbit(i);
        val1[i] = val1[i ^ d] + seq1[to[d]];
        val2[i] = val2[i ^ d] + seq2[to[d]];
        if (val1[i] <= L)
          Ans = max(Ans, f[u][L - val1[i]] + val2[i]);
      }
      printf("%d\\n", Ans);
    }
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 300001
#define maxl 100001

int N, Q, V[maxn], W[maxn];
int twok[maxn][10];
int dp[550][maxl];
int depth[maxn];

int curidx = 0;

void dfs(int x, int d) {
  if (d == 9)
    return;

  FOR(i, 0, maxl - 1) {
    dp[x][i] = dp[x / 2][i];
    if (W[x] <= i)
      dp[x][i] = max(dp[x / 2][i - W[x]] + V[x], dp[x][i]);
  }

  dfs(x * 2, d + 1);
  dfs(x * 2 + 1, d + 1);
}

int main() {
  fast;

  cin >> N;

  FOR(i, 1, N) cin >> V[i] >> W[i];
  cin >> Q;

  dfs(1, 0);

  depth[1] = 0;
  FOR(i, 2, N) depth[i] = depth[i / 2] + 1;

  FOR(i, 1, Q) {
    int x, L;
    cin >> x >> L;

    if (depth[x] < 9)
      cout << dp[x][L] << '\\n';
    else {
      vpi stuff;
      int ans = 0;
      int cur = x;
      while (depth[cur] >= 9) {
        stuff.pb(pi(V[cur], W[cur]));
        cur /= 2;
      }

      FOR(bm, 0, (1LL << stuff.size()) - 1) {
        pi res = pi(0, 0);
        FOR(j, 0, stuff.size() - 1) if (bm & (1LL << j)) {
          res.f += stuff[j].f;
          res.s += stuff[j].s;
        }
        if (res.s > L)
          continue;
        ans = max((ll)ans, res.f + dp[cur][L - res.s]);
      }
      cout << ans << '\\n';
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define f first
#define s second
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i, x) for (auto i : x)
#define mem(x, i) memset(x, i, sizeof x)
#define fast ios_base::sync_with_stdio(false), cin.tie(0)
#define maxn 300001

int N, Q, V[maxn], W[maxn];

int curidx = 0;

int solve(int x, int L) {
  if (x == 0)
    return 0;
  int cur = x;
  int mx = 0;
  FOR(i, 0, 18) {
    if (W[cur] <= L)
      mx = max(mx, solve(cur / 2, L - W[cur]) + V[cur]);
    cur /= 2;
  }
  return mx;
}

int main() {
  fast;

  cin >> N;

  FOR(i, 1, N) cin >> V[i] >> W[i];
  cin >> Q;
  FOR(i, 1, Q) {
    int x, L;
    cin >> x >> L;

    cout << solve(x, L) << '\\n';
  }
}
Problem p02648 (C++) — buggy submission, verdict: Memory Limit Exceeded

//
// Created by Danny Mittal on 6/13/20.
//

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int values[1 << 18];
int weights[1 << 18];
int depth[1 << 18];
int knapsack[512][100001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int a = 1; a <= n; a++) {
    cin >> values[a] >> weights[a];
  }
  depth[0] = -1;
  for (int j = 0; j <= 100000; j++) {
    knapsack[0][j] = 0;
  }
  for (int a = 1; a <= n; a++) {
    depth[a] = depth[a >> 1] + 1;
    if (depth[a] < 9) {
      for (int j = 0; j <= 100000; j++) {
        knapsack[a][j] = knapsack[a >> 1][j];
        if (j >= weights[a]) {
          knapsack[a][j] =
              max(knapsack[a][j], knapsack[a >> 1][j - weights[a]] + values[a]);
        }
      }
    }
  }
  int q;
  cin >> q;
  for (; q; q--) {
    int a, limit;
    cin >> a >> limit;
    int b = a;
    while (depth[b] >= 9) {
      b >>= 1;
    }
    int res = knapsack[b][limit];
    if (a != b) {
      int d = depth[a] - depth[b];
      vector<int> dpW;
      vector<int> dpV;
      dpW.push_back(0);
      dpV.push_back(0);
      for (int mask = 1; mask < (1 << d); mask++) {
        int bit = mask & -mask;
        int w = dpW[mask ^ bit] + weights[a / bit];
        int v = dpV[mask ^ bit] + values[a / bit];
        dpW.push_back(w);
        dpV.push_back(v);
        if (w <= limit) {
          res = max(res, knapsack[b][limit - w] + v);
        }
      }
    }
    cout << res << '\\n';
  }
  cout << flush;
}

Error: MLE

Fixed submission:

//
// Created by Danny Mittal on 6/13/20.
//

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int values[1 << 18];
int weights[1 << 18];
int depth[1 << 18];
int knapsack[512][100001];
vector<pair<int, int>> combs[1 << 18];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int a = 1; a <= n; a++) {
    cin >> values[a] >> weights[a];
  }
  depth[0] = -1;
  for (int j = 0; j <= 100000; j++) {
    knapsack[0][j] = 0;
  }
  for (int a = 1; a <= n; a++) {
    combs[a].emplace_back(0, 0);
    depth[a] = depth[a >> 1] + 1;
    if (depth[a] < 9) {
      for (int j = 0; j <= 100000; j++) {
        knapsack[a][j] = knapsack[a >> 1][j];
        if (j >= weights[a]) {
          knapsack[a][j] =
              max(knapsack[a][j], knapsack[a >> 1][j - weights[a]] + values[a]);
        }
      }
    } else if (depth[a] == 9) {
      combs[a].emplace_back(weights[a], values[a]);
    } else {
      for (auto p : combs[a >> 1]) {
        combs[a].push_back(p);
        combs[a].emplace_back(p.first + weights[a], p.second + values[a]);
      }
    }
  }
  int q;
  cin >> q;
  for (; q; q--) {
    int a, limit;
    cin >> a >> limit;
    int b = a;
    while (depth[b] >= 9) {
      b >>= 1;
    }
    int res = 0;
    for (auto p : combs[a]) {
      res = max(res, p.second + knapsack[b][limit - p.first]);
    }
    cout << res << '\\n';
  }
  cout << flush;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
// meet in the middle
int f[2048][100005];
int value[300005], weight[300005];
int vis[300005];
int stack[1024];

int max(int a, int b) { return a > b ? a : b; }

void preWork(int u, int d, int n) {
  vis[u] = 1;
  int p = u / 2;
  for (int i = 0; i <= 100000; i++) {
    f[u][i] = f[p][i];
    if (i - weight[u] >= 0)
      f[u][i] = max(f[u][i], f[p][i - weight[u]] + value[u]);
    if (i != 0)
      f[u][i] = max(f[u][i], f[u][i - 1]);
  }
  if (d + 1 <= 10) {
    if (2 * u <= n)
      preWork(2 * u, d + 1, n);
    if (2 * u + 1 <= n)
      preWork(2 * u + 1, d + 1, n);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &value[i], &weight[i]);
  }
  preWork(1, 1, n);
  int q;
  scanf("%d", &q);
  while (q--) {
    int p, l;
    scanf("%d%d", &p, &l);
    if (vis[p])
      printf("%d\\n", f[p][l]);
    else {
      int last = p, cnt = 0;
      while (vis[last] == 0) {
        stack[++cnt] = last;
        last /= 2;
      }
      int ans = f[last][l];
      for (int i = 0; i < (1 << cnt); i++) {
        int totalW = 0, totalV = 0;
        int mask = i;
        for (int j = 1; j <= cnt; j++) {
          if (mask % 2)
            totalW += weight[stack[j]], totalV += value[stack[j]];
          mask /= 2;
        }
        if (totalW <= l)
          ans = max(ans, f[last][l - totalW] + totalV);
      }
      printf("%d\\n", ans);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
// meet in the middle
int f[1024][100005];
int value[300005], weight[300005];
int vis[300005];
int dp[2][100005];

int max(int a, int b) { return a > b ? a : b; }

void preWork(int u, int d, int n) {
  vis[u] = 1;
  int p = u / 2;
  for (int i = 0; i <= 100000; i++) {
    f[u][i] = f[p][i];
    if (i - weight[u] >= 0)
      f[u][i] = max(f[u][i], f[p][i - weight[u]] + value[u]);
    if (i != 0)
      f[u][i] = max(f[u][i], f[u][i - 1]);
  }
  if (d + 1 <= 9) {
    if (2 * u <= n)
      preWork(2 * u, d + 1, n);
    if (2 * u + 1 <= n)
      preWork(2 * u + 1, d + 1, n);
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &value[i], &weight[i]);
  }
  preWork(1, 1, n);
  int q;
  scanf("%d", &q);
  while (q--) {
    int p, l;
    scanf("%d%d", &p, &l);
    if (vis[p])
      printf("%d\\n", f[p][l]);
    else {
      int last = p, cnt = 0;
      while (vis[last] == 0)
        last /= 2, cnt++;
      for (int i = 0; i <= 100000; i++)
        dp[0][i] = f[last][i];
      for (int i = 1; i <= cnt; i++) {
        int u = i % 2, v = (i + 1) % 2;
        for (int j = 0; j <= 100000; j++) {
          dp[u][j] = dp[v][j];
          if (j >= weight[p])
            dp[u][j] = max(dp[u][j], dp[v][j - weight[p]] + value[p]);
          if (j != 0)
            dp[u][j] = max(dp[u][j], dp[u][j - 1]);
        }
        p /= 2;
      }
      printf("%d\\n", dp[cnt % 2][l]);
    }
  }

  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX = 100010;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  int sz = (int)sqrt(n);
  vector<vector<int>> dp(sz + 1, vector<int>(MAX));
  for (int i = 1; i <= sz; i++) {
    rep(j, MAX) {
      chmax(dp[i][j], dp[i / 2][j]);
      if (j - w[i - 1] >= 0)
        chmax(dp[i][j], dp[i / 2][j - w[i - 1]] + v[i - 1]);
    }
  }
  int q;
  cin >> q;
  rep(_, q) {
    int x, l;
    cin >> x >> l;
    if (x <= sz) {
      cout << dp[x][l] << '\\n';
      continue;
    }
    vector<int> v2, w2;
    while (x > sz) {
      v2.push_back(v[x - 1]);
      w2.push_back(w[x - 1]);
      x /= 2;
    }
    int ans = 0, sz2 = v2.size();
    rep(i, 1 << sz2) {
      int weight = 0, value = 0;
      rep(j, sz2) {
        if (i >> j & 1) {
          weight += w2[j];
          value += v2[j];
        }
      }
      if (l - weight >= 0)
        chmax(ans, value + dp[x][l - weight]);
    }
    cout << ans << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  int q;
  cin >> q;
  rep(_, q) {
    int now, l;
    cin >> now >> l;
    vector<int> v2, w2;
    while (now) {
      v2.push_back(v[now - 1]);
      w2.push_back(w[now - 1]);
      now /= 2;
    }
    int ans = 0, sz = v2.size();
    stack<tuple<int, int, int>> st;
    st.emplace(0, 0, 0);
    while (!st.empty()) {
      int cur, val, wei;
      tie(cur, val, wei) = st.top();
      st.pop();
      if (cur == sz) {
        chmax(ans, val);
        continue;
      }
      st.emplace(cur + 1, val, wei);
      if (wei + w2[cur] <= l)
        st.emplace(cur + 1, val + v2[cur], wei + w2[cur]);
    }
    cout << ans << '\\n';
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll res[1025][100010], dp[12][100010], v[270000] = {}, w[270000] = {};
void dfs(int s, int h, int l) {
  if (s >= 1024)
    return;
  int i;
  for (i = 0; i <= l; i++) {
    dp[h][i] = -1;
  }
  for (i = 0; i <= l; i++) {
    dp[h][i] = max(dp[h][i], dp[h - 1][i]);
    if (i + w[s] <= l)
      dp[h][i + w[s]] = max(dp[h][i + w[s]], dp[h - 1][i] + v[s]);
  }
  res[s][0] = dp[h][0];
  for (i = 1; i <= l; i++) {
    res[s][i] = max(res[s][i - 1], dp[h][i]);
  }
  dfs(2 * s, h + 1, l);
  dfs(2 * s + 1, h + 1, l);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, k, n, q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  dp[0][0] = 0;
  dfs(1, 1, 100002);
  cin >> q;
  for (i = 0; i < q; i++) {
    int ver, l;
    cin >> ver >> l;
    if (ver < 1024) {
      cout << res[ver][l] << endl;
    } else {
      vector<int> vect;
      vect.clear();
      while (ver >= 1024) {
        if (ver >= 1024)
          vect.push_back(ver);
        ver /= 2;
      }
      int sz = vect.size();
      ll ans = 0;
      for (j = 0; j < (1 << sz); j++) {
        ll val = 0, hev = 0;
        for (k = 0; k < sz; k++) {
          if (j & (1 << k)) {
            if (vect[k] < 1024)
              exit(1);
            val += v[vect[k]];
            hev += w[vect[k]];
          }
        }
        if (hev > l)
          continue;
        ans = max(ans, res[ver][l - hev] + val);
      }
      cout << ans << endl;
    }
  }
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll res[1025][100010], dp[12][100010], v[270000] = {}, w[270000] = {};
void dfs(int s, int h, int l) {
  if (s >= 1024)
    return;
  int i;
  for (i = 0; i <= l; i++) {
    dp[h][i] = -1;
  }
  for (i = 0; i <= l; i++) {
    dp[h][i] = max(dp[h][i], dp[h - 1][i]);
    if (i + w[s] <= l)
      dp[h][i + w[s]] = max(dp[h][i + w[s]], dp[h - 1][i] + v[s]);
  }
  res[s][0] = dp[h][0];
  for (i = 1; i <= l; i++) {
    res[s][i] = max(res[s][i - 1], dp[h][i]);
  }
  dfs(2 * s, h + 1, l);
  dfs(2 * s + 1, h + 1, l);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, k, n, q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  dp[0][0] = 0;
  dfs(1, 1, 100002);
  cin >> q;
  for (i = 0; i < q; i++) {
    int ver, l;
    cin >> ver >> l;
    if (ver < 1024) {
      cout << res[ver][l] << endl;
    } else {
      vector<int> vect;
      vect.clear();
      while (ver >= 1024) {
        if (ver >= 1024)
          vect.push_back(ver);
        ver /= 2;
      }
      int sz = vect.size();
      ll ans = 0;
      for (j = 0; j < (1 << sz); j++) {
        ll val = 0, hev = 0;
        for (k = 0; k < sz; k++) {
          if (j & (1 << k)) {
            if (vect[j] < 1024)
              exit(1);
            val += v[vect[j]];
            hev += w[vect[j]];
          }
        }
        if (hev > l)
          continue;
        ans = max(ans, res[ver][l - hev] + val);
      }
      cout << ans << endl;
    }
  }
}