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

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, S = 1 << 9;

int n, q, v[(1 << 18) + 5], w[(1 << 18) + 5];

int dp[S][N];
pair<int, int> val[22];
int sw[S + 5], sv[S + 5];

void calc(int u) {
  if (dp[u][0] != -1)
    return;
  int len = 0;
  int ver = u;
  while (ver) {
    val[len++] = {v[ver], w[ver]};
    ver /= 2;
  }
  int nmask = 1 << len;
  dp[u][0] = 0;
  sw[0] = sv[0] = 0;
  for (int mask = 1; mask < nmask; ++mask) {
    int p = __builtin_ctz(mask), bef = mask ^ (1 << p);
    int ww = sw[bef] + val[p].second;
    int vv = sv[bef] + val[p].first;
    sw[mask] = ww;
    sv[mask] = vv;
    if (ww < N)
      dp[u][ww] = max(dp[u][ww], vv);
  }
  int now = 0;
  for (int i = 0; i < N; ++i) {
    now = max(dp[u][i], now);
    dp[u][i] = now;
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", v + i, w + i);
  }
  memset(dp, -1, sizeof dp);
  scanf("%d", &q);
  while (q--) {
    int u, l;
    scanf("%d %d", &u, &l);
    int ans = 0;
    if (u < S) {
      calc(u);
      ans = dp[u][l];
    } else {
      int x = u;
      while (x >= S)
        x /= 2;
      calc(x);
      int len = 0;
      while (u >= S) {
        val[len++] = {v[u], w[u]};
        u /= 2;
      }
      ans = dp[u][l];
      int nmask = 1 << len;
      for (int mask = 1; mask < nmask; ++mask) {
        int p = __builtin_ctz(mask), bef = mask ^ (1 << p);
        int ww = sw[bef] + val[p].second;
        int vv = sv[bef] + val[p].first;
        sw[mask] = ww;
        sv[mask] = vv;
        if (ww <= l)
          ans = max(ans, vv + dp[u][l - ww]);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = (1 << 18) + 5, W = 1e5 + 5, S = 1 << 8;

int n, q, v[N], w[N];

int dp[S][W];

inline int calc(int u, int l) {
  if (u == 0)
    return 0;
  int &ret = dp[u][l];
  if (ret != -1)
    return ret;
  ret = calc(u >> 1, l);
  if (l >= w[u])
    ret = max(ret, v[u] + calc(u >> 1, l - w[u]));
  return ret;
}

inline int calc2(int u, int l) {
  if (u < S)
    return calc(u, l);
  int ret = calc2(u >> 1, l);
  if (l >= w[u])
    ret = max(ret, v[u] + calc2(u >> 1, l - w[u]));
  return ret;
}

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

#include <bits/stdc++.h>

using namespace std;

const int N = 1 << 18, L = 1e5 + 5, S = 1 << 9;

int n, q, v[N], w[N];

int dp[S][L];
pair<int, int> val[22];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", v + i, w + i);
  }
  for (int i = 1; i < S; ++i) {
    int vv = v[i], ww = w[i];
    int par = i / 2;
    for (int j = 0; j < L; ++j)
      dp[i][j] = dp[par][j];
    for (int j = 0; j + ww < L; ++j)
      dp[i][j + ww] = max(dp[i][j + ww], dp[par][j] + vv);
  }
  scanf("%d", &q);
  while (q--) {
    int u, l;
    scanf("%d %d", &u, &l);
    int ans = 0;
    if (u < S) {
      ans = dp[u][l];
    } else {
      int len = 0;
      while (u >= S) {
        val[len++] = {v[u], w[u]};
        u /= 2;
      }
      int nmask = 1 << len;
      ans = 0;
      for (int mask = 0; mask < nmask; ++mask) {
        int ww = 0;
        int vv = 0;
        for (int i = 0; i < len; ++i)
          if ((mask >> i) & 1)
            vv += val[i].first, ww += val[i].second;
        if (ww <= l)
          ans = max(ans, vv + dp[u][l - ww]);
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 1 << 18, L = 1e5 + 5, S = 1 << 9;

int n, q, v[N], w[N];

int dp[S][L];
pair<int, int> val[22];

int calc(int u, int l) {
  if (u <= 0)
    return 0;
  int &ret = dp[u][l];
  if (ret != -1)
    return ret;
  ret = calc(u / 2, l);
  if (l >= w[u])
    ret = max(ret, v[u] + calc(u / 2, l - w[u]));
  return ret;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", v + i, w + i);
  }
  memset(dp, -1, sizeof dp);
  scanf("%d", &q);
  while (q--) {
    int u, l;
    scanf("%d %d", &u, &l);
    int ans = 0;
    if (u < S) {
      ans = calc(u, l);
    } else {
      int len = 0;
      while (u >= S) {
        val[len++] = {v[u], w[u]};
        u /= 2;
      }
      int nmask = 1 << len;
      ans = calc(u, l);
      for (int mask = 1; mask < nmask; ++mask) {
        int ww = 0;
        int vv = 0;
        for (int i = 0; i < len; ++i)
          if ((mask >> i) & 1)
            vv += val[i].first, ww += val[i].second;
        if (ww <= l)
          ans = max(ans, vv + calc(u, l - ww));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using ll = long long int;
using P = std::pair<int, ll>;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

P ve[262145];
ll dp[257][100001], ans;
int s[2], m;

void solve(int i, int w, ll v) {
  if (w < 0) {
    return;
  }
  if (i <= m) {
    ans = std::max(ans, v + dp[i][w]);
    // std::cout << "*" << ans << std::endl;
    return;
  }
  solve(i / 2, w, v);
  solve(i / 2, w - ve[i].first, v + ve[i].second);
}

int main(void) {
  int n, i, j, k, v, q, l;
  std::cin >> n;
  m = std::min(n, 255);
  for (i = 1; i <= n; ++i) {
    std::cin >> ve[i].second >> ve[i].first;
  }
  for (i = 1; i <= m; ++i) {
    for (j = 0; j <= 100000; ++j) {
      dp[i][j] = std::max(dp[i][j], dp[i / 2][j]);
      if (j + ve[i].first <= 100000) {
        dp[i][j + ve[i].first] =
            std::max(dp[i][j + ve[i].first], dp[i / 2][j] + ve[i].second);
      }
    }
  }
  std::cin >> q;
  while (q--) {
    std::cin >> v >> l;
    ans = 0ll;
    solve(v, l, 0ll);
    std::cout << ans << std::endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using ll = long long int;
using P = std::pair<ll, ll>;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

P ve[262145], x[2][1000];
int s[2];

void calc(int k, int i, ll w, ll v) {
  if (!i) {
    x[k][s[k]] = P(w, v);
    ++s[k];
    return;
  }
  calc(k, i / 4, w, v);
  calc(k, i / 4, w + ve[i].first, v + ve[i].second);
}

int main(void) {
  int n, i, j, k, v, q;
  ll l, ans;
  std::cin >> n;
  for (i = 1; i <= n; ++i) {
    std::cin >> ve[i].second >> ve[i].first;
  }
  std::cin >> q;
  while (q--) {
    std::cin >> v >> l;
    s[0] = 0;
    s[1] = 0;
    calc(0, v, 0ll, 0ll);
    calc(1, v / 2, 0ll, 0ll);
    for (k = 0; k < 2; ++k) {
      std::sort(x[k], x[k] + s[k]);
      for (i = 1; i < s[k]; ++i) {
        x[k][i].second = std::max(x[k][i].second, x[k][i - 1].second);
      }
    }
    j = s[1] - 1;
    ans = 0ll;
    for (i = 0; i < s[0]; ++i) {
      while (j && x[0][i].first + x[1][j].first > l) {
        --j;
      }
      if (x[0][i].first + x[1][j].first > l) {
        break;
      }
      ans = std::max(ans, x[0][i].second + x[1][j].second);
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 5;
const int N2 = (1 << 9) + 5;
const int M = 1e5 + 5;
const int inf = INT_MAX;

int read() {
  int s = 0;
  char c = getchar(), lc = '+';
  while (c < '0' || '9' < c)
    lc = c, c = getchar();
  while ('0' <= c && c <= '9')
    s = s * 10 + c - '0', c = getchar();
  return lc == '-' ? -s : s;
}
void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x < 10)
    putchar(x + '0');
  else {
    write(x / 10);
    putchar(x % 10 + '0');
  }
}
void print(int x, char c = '\\n') {
  write(x);
  putchar(c);
}
int n, v[N], w[N], d[N], dp[N2][M];
int query(int x, int l) {
  if (l < 0)
    return -inf;
  if (d[x] <= 9)
    return dp[x][l];
  return max(query(x / 2, l), query(x / 2, l - w[x]) + v[x]);
}

signed main() {
  memset(dp, 0, sizeof(dp));
  n = read();
  d[0] = 0;
  for (int i = 1; i <= n; i++) {
    v[i] = read();
    w[i] = read();
    d[i] = d[i / 2] + 1;
  }
  for (int now = 1; now <= n && d[now] <= 9; now++) {
    for (int i = 0; i <= (int)1e5; i++)
      dp[now][i] = dp[now / 2][i];
    for (int i = w[now]; i <= (int)1e5; i++)
      dp[now][i] = max(dp[now][i], dp[now / 2][i - w[now]] + v[now]);
  }
  int m = read();
  while (m--) {
    int x = read(), l = read();
    print(query(x, l));
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 5;
const int N2 = (1 << 9) + 5;
const int M = 1e5 + 5;

int read() {
  int s = 0;
  char c = getchar(), lc = '+';
  while (c < '0' || '9' < c)
    lc = c, c = getchar();
  while ('0' <= c && c <= '9')
    s = s * 10 + c - '0', c = getchar();
  return lc == '-' ? -s : s;
}
void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x < 10)
    putchar(x + '0');
  else {
    write(x / 10);
    putchar(x % 10 + '0');
  }
}
void print(int x, char c = '\\n') {
  write(x);
  putchar(c);
}
int n, v[N], w[N], d[N], sum[N], dp[N2][M];
int query(int x, int l) {
  if (d[x] <= 9)
    return dp[x][l];
  return max(query(x / 2, l), query(x / 2, l - w[x]) + v[x]);
}

signed main() {
  memset(dp, 0, sizeof(dp));
  n = read();
  d[0] = sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    v[i] = read();
    w[i] = read();
    d[i] = d[i / 2] + 1;
    sum[i] = sum[i / 2] + w[i];
  }
  for (int now = 1; now <= n; now++)
    for (int i = w[now]; i <= sum[now]; i++)
      dp[now][i] = max(dp[now / 2][i], dp[now / 2][i - w[now]] + v[now]);
  int m = read();
  while (m--) {
    int x = read(), l = read();
    print(query(x, l));
  }

  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 515
#define MAXL 100005

using namespace std;
int dp[MAXN][MAXL];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &v[i], &w[i]);
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i < min(n + 1, 1 << 9); ++i) {
    int p = i / 2;
    for (int j = 1; j < MAXL; ++j)
      dp[i][j] = max(dp[p][j], j >= w[i] ? dp[p][j - w[i]] + v[i] : 0);
  }
  int q;
  scanf("%d", &q);
  string ans;
  for (int i = 0; i < q; ++i) {
    int vi, l;
    scanf("%d%d", &vi, &l);
    if (vi < (1 << 9)) {
      ans += to_string(dp[vi][l]) + "\\n";
      continue;
    }
    vector<int> path;
    while (vi >= (1 << 9)) {
      path.emplace_back(vi);
      vi >>= 1;
    }
    int best = 0;
    int m = path.size();
    for (int j = 0; j < (1 << m); ++j) {
      int vs = 0, ws = 0;
      for (int k = 0; k < m; ++k)
        if (j & (1 << k)) {
          vs += v[path[k]];
          ws += w[path[k]];
        }
      if (ws > l)
        continue;
      best = max(best, vs + dp[vi][l - ws]);
    }
    ans += to_string(best) + "\\n";
  }
  printf("%s", ans.c_str());
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 515
#define MAXL 100005

using namespace std;
int dp[MAXN][MAXL];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &v[i], &w[i]);
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i < min(n + 1, 1 << 9); ++i) {
    int p = i / 2;
    for (int j = 1; j < MAXL; ++j)
      dp[i][j] = max(dp[p][j], j >= w[i] ? dp[p][j - w[i]] + v[i] : 0);
  }
  int q;
  scanf("%d", &q);
  string ans;
  for (int i = 0; i < q; ++i) {
    int vi, l;
    scanf("%d%d", &vi, &l);
    if (vi < (1 << 9)) {
      ans += to_string(dp[vi][l]) + "\\n";
      continue;
    }
    vector<int> path;
    while (vi >= (1 << 9)) {
      path.emplace_back(vi);
      vi >>= 1;
    }
    int best = 0;
    int m = path.size();
    for (int j = 0; j < (1 << m); ++j) {
      int vs = 0, ws = 0;
      for (int k = 0; k < m; ++k)
        if (j & (1 << k)) {
          vs += v[path[i]];
          ws += w[path[i]];
        }
      if (ws > l)
        continue;
      best = max(best, vs + dp[vi][l - ws]);
    }
    ans += to_string(best) + "\\n";
  }
  printf("%s", ans.c_str());
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int dp[1040][100005];

int main() {
  int n;
  cin >> n;
  int v[300000], w[300000];
  for (int i = 1; i <= n; i++)
    scanf("%d %d", &v[i], &w[i]);
  for (int i = 1; i <= 10; i++) {
    for (int j = (1 << (i - 1)); j < min(n + 1, (1 << i)); j++) {
      for (int k = 0; k < (1 << i); k++) {
        int m = j;
        int s = 0, t = 0;
        for (int l = 0; l < i; l++) {
          if ((k >> l) & 1) {
            s += v[m];
            t += w[m];
          }
          m /= 2;
        }
        if (t <= 100000)
          dp[j][t] = max(dp[j][t], s);
      }
      for (int t = 1; t <= 100000; t++)
        dp[j][t] = max(dp[j][t], dp[j][t - 1]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    cin >> u >> l;
    if (u < 256) {
      printf("%d\\n", dp[u][l]);
      continue;
    }
    int ans = 0;
    for (int b = 0; b < 256; b++) {
      int m = u;
      int s = 0, t = 0;
      for (int i = 0; i < 8; i++) {
        if ((b >> i) & 1) {
          s += v[m];
          t += w[m];
        }
        m /= 2;
      }
      if (t <= l)
        ans = max(ans, dp[m][l - t] + s);
    }
    printf("%d\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int dp[520][100005];

int main() {
  int n;
  cin >> n;
  int v[300000], w[300000];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  for (int i = 1; i < 10; i++) {
    for (int j = (1 << (i - 1)); j < min(512, (1 << i)); j++) {
      for (int k = 0; k < (1 << i); k++) {
        int m = j;
        int s = 0, t = 0;
        for (int l = 0; l < i; l++) {
          if ((k >> l) & 1) {
            s += v[m];
            t += w[m];
          }
          m /= 2;
        }
        if (t <= 100000)
          dp[j][t] = max(dp[j][t], s);
      }
      for (int t = 1; t <= 100000; t++)
        dp[j][t] = max(dp[j][t], dp[j][t - 1]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int u, l;
    cin >> u >> l;
    if (u < 512) {
      cout << dp[u][l] << endl;
      continue;
    }
    int ans = 0;
    for (int b = 0; b < 512; b++) {
      int m = u;
      int s = 0, t = 0;
      for (int i = 0; i < 9; i++) {
        if ((b >> i) & 1) {
          s += v[m];
          t += w[m];
        }
        m /= 2;
      }
      if (t <= l)
        ans = max(ans, dp[m][l - t] + s);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

vector<int> dp[1ll << 10];
int v[1ll << 18];
int w[1ll << 18];

int n, q;

const int INF = 1ll << 28ll;

void f(int pos, int depth) {
  if (pos > n)
    return;
  if (pos >= (1ll << 10))
    return;

  dp[pos].resize(100001, 0);
  for (int i = 100000; i > 0; i--) {
    dp[pos][i] = dp[pos / 2][i];
    if (i >= w[pos]) {
      dp[pos][i] = max(dp[pos][i], dp[pos / 2][i - w[pos]] + v[pos]);
    }
  }

  f(pos * 2, depth + 1);
  f(pos * 2 + 1, depth + 1);
}

int dfs(int pos, int lim) {
  if (lim < 0)
    return -INF;
  if (pos < (1ll << 10)) {
    return dp[pos][lim];
  }

  return max(dfs(pos / 2, lim), dfs(pos / 2, lim - w[pos]) + v[pos]);
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  dp[0].resize(100001, 0);
  f(1, 0);

  cin >> q;
  while (q--) {
    int u, lim;
    cin >> u >> lim;
    cout << dfs(u, lim) << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
using llong = long long;

map<llong, llong> dp[1ll << 18];
llong v[1ll << 18];
llong w[1ll << 18];

llong n, q;

const llong INF = 1ll << 60ll;

llong dfs(llong pos, llong lim) {
  if (lim < 0)
    return -INF;
  if (pos == 0)
    return 0;

  dp[pos][lim] = max(dfs(pos / 2, lim), dfs(pos / 2, lim - w[pos]) + v[pos]);
  return dp[pos][lim];
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  cin >> q;
  while (q--) {
    llong u, lim;
    cin >> u >> lim;
    cout << dfs(u, lim) << '\\n';
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
using namespace std;

const int MAX = 100001;
const int MAX_N = (1 << 18);
const int A = 1024;

ll V[MAX_N];
int W[MAX_N], S[MAX], L[MAX];
int N, Q;

ll dp[A + 1][MAX + 1] = {};

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> S[i] >> L[i];
  }

  int nw, w;
  ll nv, v;
  for (int k = 1; k <= min(N, A); k++) {
    nv = V[k - 1];
    nw = W[k - 1];
    for (int w = 0; w < MAX; w++) {
      if (w >= nw && (dp[k][w] < dp[k / 2][w - nw] + nv)) {
        dp[k][w] = dp[k / 2][w - nw] + nv;
      }
      if (dp[k][w] < dp[k / 2][w]) {
        dp[k][w] = dp[k / 2][w];
      }
    }
  }

  int s, l;
  for (int ind = 0; ind < Q; ind++) {
    s = S[ind];
    l = L[ind];
    vector<pair<ll, int>> dp2 = {};
    while (s >= A) {
      dp2.push_back({V[s - 1], W[s - 1]});
      s /= 2;
    }

    ll ans = dp[s][l];
    vector<pair<ll, int>> dp3 = {}, ndp = {};
    dp3.push_back({0ll, 0ll});
    for (auto NVW : dp2) {
      nv = NVW.first;
      nw = NVW.second;
      ndp = {};
      for (auto VW : dp3) {
        v = VW.first;
        w = VW.second;
        if (w + nw <= l) {
          ndp.push_back({v + nv, w + nw});
          ans = max(ans, dp[s][l - (w + nw)] + (v + nv));
        }
      }
      for (auto VW : ndp) {
        dp3.push_back(VW);
      }
    }
    cout << ans << "\\n";
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
using namespace std;

const int MAX_N = 100010;
const int A = 1024;

ll V[MAX_N], W[MAX_N], S[MAX_N], L[MAX_N];
int N, Q;

ll dp[A + 1][MAX_N + 1] = {};

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> S[i] >> L[i];
  }

  int nw, w;
  ll nv, v;
  for (int k = 1; k <= min(N, A); k++) {
    nv = V[k - 1];
    nw = W[k - 1];
    for (int w = 0; w < MAX_N; w++) {
      if (w >= nw && (dp[k][w] < dp[k / 2][w - nw] + nv)) {
        dp[k][w] = dp[k / 2][w - nw] + nv;
      }
      if (dp[k][w] < dp[k / 2][w]) {
        dp[k][w] = dp[k / 2][w];
      }
    }
  }

  int s, l;
  for (int ind = 0; ind < Q; ind++) {
    s = S[ind];
    l = L[ind];
    vector<pair<ll, int>> dp2 = {};
    while (s >= A) {
      dp2.push_back({V[s - 1], W[s - 1]});
      s /= 2;
    }

    ll ans = dp[s][l];
    vector<pair<ll, int>> dp3 = {}, ndp = {};
    dp3.push_back({0ll, 0ll});
    for (auto NVW : dp2) {
      nv = NVW.first;
      nw = NVW.second;
      ndp = {};
      for (auto VW : dp3) {
        v = VW.first;
        w = VW.second;
        if (w + nw <= l) {
          ndp.push_back({v + nv, w + nw});
          ans = max(ans, dp[s][l - (w + nw)] + (v + nv));
        }
      }
      for (auto VW : ndp) {
        dp3.push_back(VW);
      }
    }
    cout << ans << "\\n";
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = (1 << 9) + 5, M = 100005;
int n, m, Q;
int v[N * N], w[N * N];
long long f[N][M];
bool book[N * N];
long long ans;
void dfs(int u, long long ret, long long sum) {
  if (ret < 0)
    return;
  if (u <= (1 << 9)) {
    ans = max(ans, sum + f[u][ret]);
    return;
  }
  book[u] = true;
  dfs(u / 2, ret - w[u], sum + v[u]);
  book[u] = false;
  dfs(u / 2, ret, sum);
  return;
}
void solve() {
  int u, L;
  scanf("%d%d", &u, &L);
  ans = 0;
  dfs(u, L, 0);
  printf("%lld\\n", ans);
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &v[i], &w[i]);
  m = 100000;
  for (int i = 1; i <= min(1 << 9, n); i++) {
    int p = i / 2;
    for (int j = 0; j <= m; j++)
      if (j >= w[i])
        f[i][j] = max(f[p][j], f[p][j - w[i]] + v[i]);
      else
        f[i][j] = f[p][j];
  }
  scanf("%d", &Q);
  while (Q--)
    solve();
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = (1 << 10) + 5, M = 100005;
int n, m, Q;
int v[N << 1], w[N << 1];
long long f[N][M];
bool book[N << 1];
long long ans;
void dfs(int u, long long ret, long long sum) {
  if (ret < 0)
    return;
  if (u <= (1 << 9)) {
    ans = max(ans, sum + f[u][ret]);
    return;
  }
  book[u] = true;
  dfs(u / 2, ret - w[u], sum + v[u]);
  book[u] = false;
  dfs(u / 2, ret, sum);
  return;
}
void solve() {
  int u, L;
  scanf("%d%d", &u, &L);
  ans = 0;
  dfs(u, L, 0);
  printf("%lld\\n", ans);
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &v[i], &w[i]);
  m = 100000;
  for (int i = 1; i <= min(1 << 9, n); i++) {
    int p = i / 2;
    for (int j = 0; j <= m; j++)
      if (j >= w[i])
        f[i][j] = max(f[p][j], f[p][j - w[i]] + v[i]);
      else
        f[i][j] = f[p][j];
  }
  scanf("%d", &Q);
  while (Q--)
    solve();
  return 0;
}