alexjercan/bugnet huggingface dataset:
Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  rep(i, n) cin >> a[i];
  vector<int> b(n + 100, 0);
  rep(i, min(k, (int)(sqrt(n) + 100))) {
    rep(j, n) {
      b[max(j - a[j], 0)]++;
      if (j + a[j] + 1 >= n)
        continue;
      b[j + a[j] + 1]--;
    }
    rep(j, n - 1) { b[j + 1] += b[j]; }
    swap(a, b);
    vector<int> empty(n + 100, 0);
    swap(b, empty);
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}

Error: WA

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;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  rep(i, n) cin >> a[i];
  vector<int> b(n, 0);
  rep(i, min(k, (int)(sqrt(n) + 100))) {
    rep(j, n) {
      b[max(j - a[j], 0)]++;
      if (j + a[j] >= n)
        continue;
      b[j + a[j] + 1]--;
    }
    rep(j, n - 1) { b[j + 1] += b[j]; }
    swap(a, b);
    vector<int> empty(n, 0);
    swap(b, empty);
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1 << 18;
const int MAXL = 1e5 + 7;
int v[MAXN], w[MAXN], dep[MAXN];
int dp[(1 << 9) + 7][MAXL];
int main() {
  int bound = 9;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    dep[i] = dep[i / 2] + 1;
  for (int i = 1; i <= n; i++)
    scanf("%d %d", &w[i], &v[i]);
  for (int i = 1; i <= n && dep[i] <= bound; i++) {
    for (int j = 0; j < MAXL; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (j >= v[i])
        dp[i][j] = max(dp[i][j], dp[i / 2][j - v[i]] + w[i]);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, L;
    scanf("%d %d", &u, &L);
    vector<int> vec;
    if (dep[u] <= bound) {
      printf("%d\\n", dp[u][L]);
      continue;
    }
    while (dep[u] > bound) {
      vec.push_back(u);
      u /= 2;
    }
    int sz = vec.size();
    int ans = 0;
    for (int s = 0; s < (1 << sz); s++) {
      int sumw = 0, sumv = 0;
      for (int i = 0; i < sz; i++)
        if (s >> i & 1)
          sumv += v[vec[i]], sumw += w[vec[i]];
      if (L >= sumv)
        ans = max(ans, sumw + dp[u][L - sumv]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 18;
const int MAXL = 1e5 + 7;
int v[MAXN], w[MAXN], dep[MAXN];
int dp[(1 << 9) + 7][MAXL];
int main() {
  int bound = 9;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    dep[i] = dep[i / 2] + 1;
  for (int i = 1; i <= n; i++)
    scanf("%d %d", &w[i], &v[i]);
  for (int i = 1; i <= min(MAXN / 2 - 1, n); i++) {
    for (int j = v[i]; j < MAXL; j++) {
      dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - v[i]] + w[i]);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, L;
    scanf("%d %d", &u, &L);
    vector<int> vec;
    if (dep[u] <= bound) {
      printf("%d\\n", dp[u][L]);
      continue;
    }
    while (dep[u] > bound) {
      vec.push_back(u);
      u /= 2;
    }
    int sz = vec.size();
    int ans = 0;
    for (int s = 0; s < (1 << sz); s++) {
      int sumw = 0, sumv = 0;
      for (int i = 0; i < sz; i++)
        if (s >> i & 1)
          sumv += v[vec[i]], sumw += w[vec[i]];
      ans = max(ans, sumw + dp[u][L - sumv]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int maxN = (1 << 18) + 10;
const int MAX = 1e5;
typedef long long ll;

int n, q;
#define pii pair<int, int>
pii a[maxN];
#define V first
#define W second

ll pre[maxN];
ll dp[512][MAX + 1];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].V >> a[i].W;
  }

  const int limit = 512;
  for (int j = 0; j <= MAX; ++j)
    dp[1][j] = (a[1].W > j ? 0 : a[1].V);

  for (int i = 2; i < min(limit, n + 1); ++i) {
    int par = i / 2;
    for (int j = 0; j <= MAX; ++j) {
      dp[i][j] = dp[par][j];
      if (j >= a[i].W)
        dp[i][j] = max(dp[i][j], dp[par][j - a[i].W] + a[i].V);
    }
  }

  cin >> q;
  while (q--) {
    int x, L;
    cin >> x >> L;
    vector<int> node;

    while (x >= limit) {
      node.push_back(x);
      x /= 2;
    }
    ll ans = 0;
    int sz = node.size();
    for (int i = 0; i < (1 << sz); ++i) {
      ll Le = 0;
      int Ri = 0;
      for (int j = 0; j < sz; j++)
        if ((i >> j) & 1) {
          Le = Le + a[node[j]].V;
          Ri = Ri + a[node[j]].W;
        }
      if (Ri <= L)
        ans = max(ans, dp[x][L - Ri] + Le);
    }
    cout << ans << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxN = (1 << 18) + 10;
const int MAX = 1e5;
typedef long long ll;

int n, q;
#define pii pair<int, int>
pii a[maxN];
#define V first
#define W second

ll pre[maxN];
ll dp[512][MAX + 1];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].V >> a[i].W;
  }

  const int limit = 512;
  for (int j = 0; j <= MAX; ++j)
    dp[1][j] = (a[1].W > j ? 0 : a[1].V);

  for (int i = 2; i < min(limit, n + 1); ++i) {
    int par = i / 2;
    for (int j = 0; j <= MAX; ++j) {
      dp[i][j] = dp[par][j];
      if (j >= a[i].W)
        dp[i][j] = max(dp[i][j], dp[par][j - a[i].W] + a[i].V);
    }
  }

  cin >> q;
  while (q--) {
    int x, L;
    cin >> x >> L;
    vector<int> node;

    while (x >= limit) {
      node.push_back(x);
      x /= 2;
    }
    ll ans = 0;
    int sz = node.size();
    for (int i = 0; i < (1 << sz); ++i) {
      ll Le = 0;
      int Ri = 0;
      for (int j = 0; j < sz; j)
        if ((i >> j) & 1) {
          Le = Le + a[node[j]].V;
          Ri = Ri + a[node[j]].W;
        }
      if (Ri <= L)
        ans = max(ans, dp[x][L - Ri] + Le);
    }
    cout << ans << '\\n';
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;

const int SAKAIME = 2048;
const int MAX_W = 100000;
const int MAX_N = 300000;

vector<vector<int>> memo(SAKAIME + 1, vector<int>(MAX_W + 1, 0));
vector<Pii> VW(MAX_N + 1);

int dfs(int v, int l) {
  if (v <= SAKAIME)
    return memo[v][l];

  int t = 0;
  if (l - VW[v].second >= 0)
    t = VW[v].first + dfs(v / 2, l - VW[v].second);
  return max(dfs(v / 2, l), t);
}

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> VW[i].first >> VW[i].second;

  for (int i = 1; i <= min(SAKAIME, N); i++) {
    for (int j = 1; j <= MAX_W; j++) {
      if (j - VW[i].second >= 0)
        memo[i][j] = VW[i].first + memo[i / 2][j - VW[i].second];
      memo[i][j] = max(memo[i / 2][j], memo[i][j]);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, l;
    cin >> v >> l;
    cout << dfs(v, l) << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;

const int SAKAIME = 2048;
const int MAX_W = 300000;
const int MAX_N = 100000;

vector<vector<int>> memo(SAKAIME + 1, vector<int>(MAX_W + 1, 0));
vector<Pii> VW(MAX_N + 1);

int dfs(int v, int l) {
  if (v <= SAKAIME)
    return memo[v][l];

  int t = 0;
  if (l - VW[v].second >= 0)
    t = VW[v].first + dfs(v / 2, l - VW[v].second);
  return max(dfs(v / 2, l), t);
}

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> VW[i].first >> VW[i].second;

  for (int i = 1; i <= min(SAKAIME, N); i++) {
    for (int j = 1; j <= MAX_W; j++) {
      if (j - VW[i].second >= 0)
        memo[i][j] = VW[i].first + memo[i / 2][j - VW[i].second];
      memo[i][j] = max(memo[i / 2][j], memo[i][j]);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, l;
    cin >> v >> l;
    cout << dfs(v, l) << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

vector<int> v(pow(2, 18) + 1, 0), w(pow(2, 18) + 1, 0);
vector<vector<int>> dp(2048, vector<int>(1e5 + 1, 0));

int dfs(int V, int W, int C) {
  if (W < 0) {
    return 0;
  }
  if (V < 2048) {
    return dp[V][W] + C;
  }
  int s = dfs(V / 2, W - w[V], C + v[V]), t = dfs(V / 2, W, C);
  return max(s, t);
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> v[i + 1] >> w[i + 1];
  }
  for (int i = 1; i < 2048; i++) {
    if (i > N) {
      break;
    }
    for (int j = 1; j < 1e5 + 1; j++) {
      if (j >= w[i]) {
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
      } else {
        dp[i][j] = dp[i / 2][j];
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int V, W;
    cin >> V >> W;
    cout << dfs(V, W, 0) << endl;
  }
}


Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

vector<int> v(pow(2, 18) + 1, 0), w(pow(2, 18) + 1, 0);
vector<vector<int>> dp(4096, vector<int>(1e5 + 1, 0));

int dfs(int V, int W, int C) {
  if (W < 0) {
    return 0;
  }
  if (V < 4096) {
    return dp[V][W] + C;
  }
  int s = dfs(V / 2, W - w[V], C + v[V]), t = dfs(V / 2, W, C);
  return max(s, t);
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> v[i + 1] >> w[i + 1];
  }
  for (int i = 1; i < 4096; i++) {
    if (i > N) {
      break;
    }
    for (int j = 1; j < 1e5 + 1; j++) {
      if (j >= w[i]) {
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
      } else {
        dp[i][j] = dp[i / 2][j];
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int V, W;
    cin >> V >> W;
    cout << dfs(V, W, 0) << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

ll n;
vector<vector<ll>> vw;
vector<vector<ll>> dp;
ll c1, c2;

void dfs(ll i) {
  ll j = i / 2;
  if (i > 1) {
    for (ll k = c2; k + 1; k--) {
      if (dp[j - 1][k] != 0 or k == 0) {
        ll ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  vw.resize(n);
  REP(i, n) {
    ll x, y;
    cin >> x >> y;
    vw[i] = {x, y};
  }
  c1 = 1 << 10;
  c2 = 100000;
  dp.resize(c1);
  REP(i, c1) dp[i].resize(c2 + 1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  ll q;
  cin >> q;
  REP(i, q) {
    ll v, l;
    cin >> v >> l;
    vector<ll> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    ll r = cand.size();
    ll ans = 0;
    REP(j, 1 << r) {
      ll v_sub = 0;
      ll w_sub = 0;
      REP(k, r) {
        if ((j >> k) & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

ll n;
vector<vector<ll>> vw;
vector<vector<ll>> dp;
ll c1, c2;

void dfs(ll i) {
  ll j = i / 2;
  for (ll k = c2; k + 1; k--) {
    if (dp[j - 1][k] != 0 or k == 0) {
      ll ne = k + vw[i - 1][1];
      if (ne <= c2)
        dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
    }
    dp[i - 1][k] = dp[j - 1][k];
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  vw.resize(n);
  REP(i, n) {
    ll x, y;
    cin >> x >> y;
    vw[i] = {x, y};
  }
  c1 = 1 << 10;
  c2 = 100000;
  dp.resize(c1);
  REP(i, c1) dp[i].resize(c2 + 1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  ll q;
  cin >> q;
  REP(i, q) {
    ll v, l;
    cin >> v >> l;
    vector<ll> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    ll r = cand.size();
    ll ans = 0;
    REP(j, 1 << r) {
      ll v_sub = 0;
      ll w_sub = 0;
      REP(k, r) {
        if ((j >> k) & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

I n;
vector<vector<I>> vw;
vector<vector<I>> dp;
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] != 0 or k == 0) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  vw.resize(n);
  REP(i, n) {
    I x, y;
    cin >> x >> y;
    vw[i] = {x, y};
  }
  c1 = 1 << 10;
  c2 = 100000;
  dp.resize(c1);
  REP(i, c1) dp[i].resize(c2 + 1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

ll n;
vector<vector<ll>> vw;
vector<vector<ll>> dp;
ll c1, c2;

void dfs(ll i) {
  ll j = i / 2;
  if (i > 1) {
    for (ll k = c2; k + 1; k--) {
      if (dp[j - 1][k] != 0 or k == 0) {
        ll ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n)) {
    dfs(i * 2);
    dfs(i * 2 + 1);
  }
}

signed main() {
  cin >> n;
  vw.resize(n);
  REP(i, n) {
    ll x, y;
    cin >> x >> y;
    vw[i] = {x, y};
  }
  c1 = (1 << 10) - 1;
  c2 = 100000;
  dp.resize(c1);
  REP(i, c1) dp[i].resize(c2 + 1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  ll q;
  cin >> q;
  REP(i, q) {
    ll v, l;
    cin >> v >> l;
    vector<ll> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    ll r = cand.size();
    ll ans = 0;
    REP(j, 1 << r) {
      ll v_sub = 0;
      ll w_sub = 0;
      REP(k, r) {
        if ((j >> k) & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

I n;
vector<vector<I>> vw, dp;
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] or !k) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  vw.resize(n);
  REP(i, n) {
    I x, y;
    cin >> x >> y;
    vw[i] = {x, y};
  }
  c1 = 1024;
  c2 = 100000;
  dp.resize(c1);
  REP(i, c1) dp[i].resize(c2 + 1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

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

I n;
I vw[26214][2], dp[1024][100001];
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] or !k) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  REP(i, n) { cin >> vw[i][0] >> vw[i][1]; }
  c1 = 1024;
  c2 = 100000;
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

I n;
I vw[323456][2], dp[1024][100001];
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] or !k) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  // vw.resize(n);
  REP(i, n) {
    I x, y;
    cin >> vw[i][0] >> vw[i][1];
  }
  c1 = 1024;
  c2 = 100000;
  // dp.resize(c1);REP(i,c1)dp[i].resize(c2+1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

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

I n;
I vw[26214][2], dp[1024][100001];
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] or !k) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  // vw.resize(n);
  REP(i, n) {
    I x, y;
    cin >> vw[i][0] >> vw[i][1];
  }
  c1 = 1024;
  c2 = 100000;
  // dp.resize(c1);REP(i,c1)dp[i].resize(c2+1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int p[1024][1 << 17], v[1 << 19], w[1 << 19], q, a, l, i;
int f(int a, int l) {
  if (l < 0)
    return -1e9;
  if (!a)
    return 0;
  if (a > 1023)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~p[a][l])
    return p[a][l];
  return p[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
};
int main() {
  memset(p, -1, 1 << 29);
  cin >> l;
  for (; i++ < l;)
    cin >> v[i] >> w[i];
  cin >> q;
  while (q--) {
    cin >> a >> l;
    cout << f(a, l) << " ";
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int p[1024][1 << 17], v[1 << 19], w[1 << 19], q, a, l, i;
int f(int a, int l) {
  if (l < 0)
    return -1e9;
  if (!a)
    return 0;
  if (a > 1023)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~p[a][l])
    return p[a][l];
  return p[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
};
int main() {
  memset(p, -1, 1 << 31);
  cin >> l;
  for (; i++ < l;)
    cin >> v[i] >> w[i];
  cin >> q;
  while (q--) {
    cin >> a >> l;
    cout << f(a, l) << " ";
  }
}