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

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

const int k = 9;
vector<vector<int>> d;
vector<int> w;
vector<int> v;

int dp(int x, int y) {
  if (y < 0)
    return -1000000;
  if (x < pow(2, k - 1)) {
    return d[x][y];
  } else {
    int a = dp(x / 2, y);
    int b = dp(x / 2, y - w[x - 1]) + v[x - 1];
    return max(a, b);
  }
}

int main() {
  int n;
  cin >> n;
  v = w = vector<int>(n);
  rep(i, n) cin >> v[i] >> w[i];

  int q;
  cin >> q;
  vector<int> u(q);
  vector<int> l(q);
  rep(i, q) { cin >> u[i] >> l[i]; }

  int N = min(int(pow(2, k - 1)) - 1, n);
  int M = *max_element(l.begin(), l.end());
  d.push_back(vector<int>(M, 0));
  for (int i = 1; i < N + 1; i++) {
    vector<int> row;
    rep(j, M + 1) {
      if (i == 1) {
        if (w[0] <= j) {
          row.push_back(v[0]);
        } else {
          row.push_back(0);
        }
      } else {
        int a1 = d[i / 2][j];
        int a2 = 0;
        if (j >= w[i - 1])
          a2 = d[i / 2][j - w[i - 1]] + v[i - 1];
        row.push_back(max(a1, a2));
      }
    }
    d.push_back(row);
  }
  vector<int> res(q);
  rep(i, q) {
    int x1 = u[i];
    int x2 = l[i];
    res[i] = dp(x1, x2);
  }

  rep(i, q) { cout << res[i] << endl; }
  return 0;
}


Error: TLE

Fixed submission:

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

const int k = 9;

int dp(int x, int y, vector<vector<int>> d, vector<int> w, vector<int> v) {
  if (x <= pow(2, k)) {
    return d[x][y];
  } else {
    int a = dp(x / 2, y, d, w, v);
    int b = dp(x / 2, y - w[x - 1], d, w, v) + v[x - 1];
    return max(a, b);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> w(n);
  rep(i, n) cin >> v[i] >> w[i];

  int q;
  cin >> q;
  vector<int> u(q);
  vector<int> l(q);
  rep(i, q) { cin >> u[i] >> l[i]; }

  int N = min(int(pow(2, k)), n);
  int M = *max_element(l.begin(), l.end());
  vector<vector<int>> d;
  d.push_back(vector<int>(M, 0));
  for (int i = 1; i < N + 1; i++) {
    vector<int> row;
    rep(j, M + 1) {
      if (i == 1) {
        if (w[0] <= j) {
          row.push_back(v[0]);
        } else {
          row.push_back(0);
        }
      } else {
        int a1 = d[i / 2][j];
        int a2 = 0;
        if (j >= w[i - 1])
          a2 = d[i / 2][j - w[i - 1]] + v[i - 1];
        row.push_back(max(a1, a2));
      }
    }
    d.push_back(row);
  }
  vector<int> res(q);
  rep(i, q) {
    int x1 = u[i];
    int x2 = l[i];
    res[i] = dp(x1, x2, d, w, v);
  }

  rep(i, q) { cout << res[i] << endl; }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

std::vector<int> v;
std::vector<int> w;
std::vector<std::vector<int>> dp;
int lgN;

int knapsack(int ver, int capa) {
  if (capa < 0) {
    return INT_MIN;
  }
  if (capa == 0) {
    return 0;
  }
  if (ver <= lgN) {
    return dp[ver][capa];
  } else {
    int v0 = knapsack(ver / 2, capa);
    int v1 = knapsack(ver / 2, capa - w[ver - 1]) + v[ver - 1];

    return std::max(v0, v1);
  }
}

int main() {
  int N;
  std::cin >> N;
  v.resize(N);
  w.resize(N);
  for (int i = 0; i < N; i++) {
    std::cin >> v[i] >> w[i];
  }
  int Q;
  std::cin >> Q;

  lgN = std::min(512, N);
  // lgN=N;
  dp.resize(lgN + 1);
  for (int i = 0; i < lgN + 1; i++) {
    dp[i].resize(100001);
  }
  for (int i = 0; i < lgN + 1; i++) {
    dp[0][i] = 0;
    dp[i][0] = 0;
  }

  for (int i = 1; i < lgN + 1; i++) {
    for (int j = 1; j < 100001; j++) {
      if (j >= w[i - 1] && dp[i / 2][j] < dp[i / 2][j - w[i - 1]] + v[i - 1]) {
        dp[i][j] = dp[i / 2][j - w[i - 1]] + v[i - 1];
      } else {
        dp[i][j] = dp[i / 2][j];
      }
    }
  }

  // std::cout<<std::endl;

  int ver, L;
  for (int i = 0; i < Q; i++) {
    std::cin >> ver >> L;
    std::cout << knapsack(ver, L) << std::endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

std::vector<int> v;
std::vector<int> w;
std::vector<std::vector<int>> dp;
int lgN;

int knapsack(int ver, int capa) {
  if (capa < 0) {
    return INT_MIN;
  }
  if (capa == 0) {
    return 0;
  }
  if (ver <= lgN) {
    return dp[ver][capa];
  } else {
    int v0 = knapsack(ver / 2, capa);
    int v1 = knapsack(ver / 2, capa - w[ver - 1]) + v[ver - 1];

    return std::max(v0, v1);
  }
}

int main() {
  int N;
  std::cin >> N;
  v.resize(N);
  w.resize(N);
  for (int i = 0; i < N; i++) {
    std::cin >> v[i] >> w[i];
  }
  int Q;
  std::cin >> Q;
  std::vector<int> vertex(Q);
  std::vector<int> L(Q);
  for (int i = 0; i < Q; i++) {
    std::cin >> vertex[i] >> L[i];
  }

  lgN = std::log2(N);
  // lgN=N;
  dp.resize(lgN + 1);
  for (int i = 0; i < lgN + 1; i++) {
    dp[i].resize(100001);
  }
  for (int i = 0; i < lgN + 1; i++) {
    dp[0][i] = 0;
    dp[i][0] = 0;
  }

  for (int i = 1; i < lgN + 1; i++) {
    for (int j = 1; j < 100001; j++) {
      if (j >= w[i - 1] && dp[i / 2][j] < dp[i / 2][j - w[i - 1]] + v[i - 1]) {
        dp[i][j] = dp[i / 2][j - w[i - 1]] + v[i - 1];
      } else {
        dp[i][j] = dp[i / 2][j];
      }
    }
  }

  // std::cout<<std::endl;

  for (int i = 0; i < Q; i++) {
    std::cout << knapsack(vertex[i], L[i]) << std::endl;
  }

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  const int MAX = 100001;
  int limit = min(512, n);
  vector<vector<int>> dp(limit, vector<int>(MAX));
  for (int j = 0; j < MAX; j++) {
    dp[0][j] = (j >= w[0] ? v[0] : 0);
  }
  for (int i = 1; i < limit; i++) {
    int p = (i - 1) / 2;
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = dp[p][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[p][j - w[i]] + v[i]);
      }
    }
  }
  int tt;
  cin >> tt;
  while (tt--) {
    int ver, L;
    cin >> ver >> L;
    --ver;
    vector<int> ids;
    while (ver >= limit) {
      ids.push_back(ver);
      ver = (ver - 1) / 2;
    }
    int sz = (int)ids.size();
    int ans = 0;
    for (int t = 0; t < (1 << sz); t++) {
      int sv = 0, sw = 0;
      for (int i = 0; i < sz; i++) {
        if (t & (1 << i)) {
          sv += v[ids[i]];
          sw += w[ids[i]];
        }
      }
      if (sw <= L) {
        ans = max(ans, dp[ver][L - sw] + sv);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int V[300000], w[300000];
int **c;

void knapsack(ll n, ll W) {
  for (ll i = 0; i <= W; i++) {
    c[0][i] = 0;
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= W; j++) {
      if (w[i] <= j) {
        c[i][j] = max(V[i] + c[i / 2][j - w[i]], c[i / 2][j]);
      } else
        c[i][j] = c[i / 2][j];
    }
  }
}

void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> V[i] >> w[i];
  }
  c = (int **)malloc((n + 1) * sizeof(int *));
  for (ll i = 0; i <= n; i++) {
    c[i] = (int *)malloc((100001) * sizeof(int));
  }
  knapsack(n, 100000);
  ll q;
  cin >> q;
  vector<ll> v(q), L(q);
  for (ll i = 0; i < q; i++) {
    cin >> v[i] >> L[i];
  }
  for (ll i = 0; i < q; i++) {
    cout << c[v[i]][L[i]] << endl;
  }

  return;
}

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<ll> v(n), w(n);
  v.push_back(0);
  w.push_back(0);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];
  vector<vector<ll>> dp(1024, vector<ll>(100001));
  // ignore 0
  for (int i = 1; i < min(1024, n + 1); i++) {
    rep(j, w[i]) dp[i][j] = dp[i / 2][j];
    for (int j = w[i]; j <= 100000; j++) {
      dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int s;
    ll l;
    cin >> s >> l;
    vector<pair<ll, ll>> cand;
    cand.emplace_back(0, l);
    for (; s >= 1024; s /= 2) {
      int prev_sz = cand.size();
      rep(i, prev_sz) {
        ll val, rest;
        tie(val, rest) = cand[i];
        if (rest >= w[s])
          cand.emplace_back(val + v[s], rest - w[s]);
      }
    }
    ll ans = 0;
    for (auto p : cand) {
      ll val, rest;
      tie(val, rest) = p;
      chmax(ans, dp[s][rest] + val);
    }
    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>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b);
#define chmin(a, b) a = min(a, b);
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  vector<ll> v(n), w(n);
  v.push_back(0);
  w.push_back(0);
  rep(i, n) cin >> v[i] >> w[i];
  vector<vector<ll>> dp(1024, vector<ll>(100001));
  // ignore 0
  for (int i = 1; i < min(1024, n + 1); i++) {
    for (int j = v[i]; j <= 100000; j++) {
      dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int s;
    ll l;
    cin >> s >> l;
    set<pair<ll, ll>> dp2;
    dp2.emplace(0, l);
    if (l >= w[s]) {
      dp2.emplace(v[s], l - w[s]);
    }
    while (s >= 1024) {
      s /= 2;
      vector<pair<ll, ll>> ndp;
      int now = 0;
      for (auto p : dp2) {
        ll val, rest;
        tie(val, rest) = p;
        if (rest >= w[s])
          ndp.emplace_back(val + v[s], rest - w[s]);
      }
      dp2.insert(ndp.begin(), ndp.end());
    }
    ll ans = 0;
    for (auto p : dp2) {
      ll val, rest;
      tie(val, rest) = p;
      chmax(ans, dp[s][rest] + val);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int lg = 9, N = 1 << 18, MX = 1e5 + 1, inf = 1e9;
int v[N], w[N], h[N], prp[1 << lg][MX], par[N][lg + 1];
int sum[1 << lg], val[1 << lg];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i], h[i] = h[i / 2] + 1, par[i][0] = i;
    for (int j = 1; j <= lg; j++)
      par[i][j] = par[i / 2][j - 1];
  }
  for (int i = 1; i < min(n + 1, 1 << lg); i++) {
    memset(sum, 0, sizeof sum), memset(val, 0, sizeof val);
    for (int mask = 1; mask < (1 << h[i]); mask++) {
      int bit = __builtin_ctz(mask);
      sum[mask] = sum[mask ^ (1 << bit)] + w[par[i][bit]],
      val[mask] = val[mask ^ (1 << bit)] + v[par[i][bit]];
      if (sum[mask] < MX)
        prp[i][sum[mask]] = max(prp[i][sum[mask]], val[mask]);
    }
    for (int j = 1; j < MX; j++)
      prp[i][j] = max(prp[i][j], prp[i][j - 1]);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, L;
    cin >> u >> L;
    if (h[u] <= lg) {
      cout << prp[u][L] << "\\n";
      continue;
    }
    int ans = 0, p = par[u][lg];
    memset(sum, 0, sizeof sum), memset(val, 0, sizeof val);
    for (int mask = 1; mask < (1 << lg); mask++) {
      int bit = __builtin_ctz(mask);
      sum[mask] = sum[mask ^ (1 << bit)] + w[par[u][bit]],
      val[mask] = val[mask ^ (1 << bit)] + v[par[u][bit]];
      if (sum[mask] <= L)
        ans = max(ans, val[mask] + prp[p][L - sum[mask]]);
    }
    cout << max(ans, prp[p][L]) << "\\n";
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pii pair<int, int>
#define F first
#define S second
const int lg = 9, N = 1 << 18, MX = 1e5 + 1, inf = 1e9;
int V[N], W[N], par[N], prp[1 << 9][MX];
vector<pii> vec[N];
deque<int> dq[N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
    dq[i] = dq[i / 2], dq[i].push_back(i);
    if (dq[i].size() > lg)
      par[i] = dq[i].front(), dq[i].pop_front();
    vec[i].push_back({0, 0});
    for (int x : dq[i]) {
      int S = vec[i].size();
      for (int j = 0; j < S; j++)
        if (vec[i][j].F + W[x] < MX)
          vec[i].push_back({vec[i][j].F + W[x], vec[i][j].S + V[x]});
    }
  }
  for (int i = 1; i <= n; i++)
    if (!par[i]) {
      for (pii p : vec[i])
        prp[i][p.F] = max(prp[i][p.F], p.S);
      for (int j = 1; j < MX; j++)
        prp[i][j] = max(prp[i][j], prp[i][j - 1]);
    }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, L;
    cin >> u >> L;
    if (!par[u]) {
      cout << prp[u][L] << "\\n";
      continue;
    }
    int ans = 0;
    for (auto p : vec[u]) {
      int w = p.F, v = p.S;
      if (w > L)
        continue;
      ans = max(ans, v + prp[par[u]][L - w]);
    }
    cout << ans << "\\n";
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vl = vector<long>;
using vvl = vector<vector<long>>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

long K = 9;
long D = 512;

vvl dp(D + 1, vl(100010, 0));
vector<long> V(262144, 0), W(262144, 1e6);

void dfs(int now, int d) {
  long v = V.at(now - 1);
  long w = W.at(now - 1);
  rep(j, 100001) {
    if (j - w >= 0)
      dp.at(now).at(j) =
          max(dp.at(now / 2).at(j), dp.at(now / 2).at(j - w) + v);
    else
      dp.at(now).at(j) = dp.at(now / 2).at(j);
  }
  if (d == K - 1)
    return;
  dfs(now * 2, d + 1);
  dfs(now * 2 + 1, d + 1);
}

long search(int now, long v, long w, long L) {
  if (now < D) {
    if (w > L)
      return 0;
    else
      return v + dp.at(now).at(L - w);
  }
  return max(search(now / 2, v + V.at(now - 1), w + W.at(now - 1), L),
             search(now / 2, v, w, L));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long n;
  cin >> n;
  rep(i, n) cin >> V.at(i) >> W.at(i);
  dfs(1, 0);
  int q;
  cin >> q;
  rep(i, q) {
    long v, l;
    cin >> v >> l;
    if (v < D) {
      cout << dp[v][l] << "\\n";
    } else {
      cout << search(v, 0, 0, l) << "\\n";
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vl = vector<long>;
using vvl = vector<vector<long>>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

long K = 9;
long D = 512;

vvl dp(D + 1, vl(100010, 0));
vector<long> V(262144, 0), W(262144, 1e6);

void dfs(int now, int d) {
  long v = V.at(now - 1);
  long w = W.at(now - 1);
  rep(j, 100001) {
    if (j - w >= 0)
      dp.at(now).at(j) =
          max(dp.at(now / 2).at(j), dp.at(now / 2).at(j - w) + v);
    else
      dp.at(now).at(j) = dp.at(now / 2).at(j);
  }
  if (d == K - 1)
    return;
  dfs(now * 2, d + 1);
  dfs(now * 2 + 1, d + 1);
}

long search(int now, long v, long w, long L) {
  if (now < D) {
    if (w > L)
      return 0;
    else
      return v + dp.at(now).at(L - w);
  }
  return max(search(now / 2, v + V.at(now - 1), w + W.at(now - 1), L),
             search(now / 2, v, w, L));
}

int main() {
  long n;
  cin >> n;
  rep(i, n) cin >> V.at(i) >> W.at(i);
  dfs(1, 0);
  int q;
  cin >> q;
  rep(i, q) {
    long v, l;
    cin >> v >> l;
    if (v < D) {
      cout << dp[v][l] << "\\n";
    } else {
      cout << search(v, 0, 0, l) << "\\n";
    }
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 1 << 19;
const int MOD = 1e9 + 7;
int V[maxn], W[maxn], n, q, dp[555][100009];
int main() {
#ifdef DEBUG
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> W[i] >> V[i];
  for (int i = 1; i <= 512; ++i) {
    for (int j = 0; j <= 100000; ++j)
      dp[i][j] = dp[i >> 1][j];
    for (int j = 0; j <= 100000; ++j)
      if (j >= V[i])
        dp[i][j] = max(dp[i >> 1][j - V[i]] + W[i], dp[i][j]);
  }
  cin >> q;
  for (int ts = 1; ts <= q; ++ts) {
    int v, l, ans = 0;
    cin >> v >> l;
    if (v <= 512) {
      ans = dp[v][l];
    } else {
      vector<int> idx;
      for (; v > 512; v >>= 1)
        idx.push_back(v);
      int totv, totw;
      for (int i = 0; i < (1 << Sz(idx)); ++i) {
        totv = totw = 0;
        for (int j = 0; j < Sz(idx); ++j)
          if (i >> j & 1)
            totv += V[idx[j]], totw += W[idx[j]];
        if (totv <= l) {
          ans = max(dp[v][l - totv] + totw, ans);
        }
      }
    }
    cout << ans << '\\n';
  }
  return 0;
}
// 2020.06.14 10:06:45


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 200009;
const int MOD = 1e9 + 7;
int V[maxn], W[maxn], n, q, dp[555][100009];
int main() {
#ifdef DEBUG
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> W[i] >> V[i];
  for (int i = 1; i <= 512; ++i) {
    for (int j = 0; j <= 100000; ++j)
      dp[i][j] = dp[i >> 1][j];
    for (int j = 0; j <= 100000; ++j)
      if (j >= V[i])
        dp[i][j] = max(dp[i >> 1][j - V[i]] + W[i], dp[i][j]);
  }
  cin >> q;
  for (int ts = 1; ts <= q; ++ts) {
    int v, l, ans = 0;
    cin >> v >> l;
    if (v <= 512) {
      ans = dp[v][l];
    } else {
      vector<int> idx;
      for (; v > 512; v >>= 1)
        idx.push_back(v);
      int totv, totw;
      for (int i = 0; i < (1 << Sz(idx)); ++i) {
        totv = totw = 0;
        for (int j = 0; j < Sz(idx); ++j)
          if (i >> j & 1)
            totv += V[idx[j]], totw += W[idx[j]];
        if (totv <= l) {
          ans = max(dp[v][l - totv] + totw, ans);
        }
      }
    }
    cout << ans << '\\n';
  }
  return 0;
}
// 2020.06.14 10:06:45
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  const int MAX = 100001;
  int limit = min(512, n);
  vector<vector<int>> dp(limit, vector<int>(MAX));
  for (int j = 0; j < MAX; j++) {
    dp[0][j] = (j >= w[0] ? v[0] : 0);
  }
  for (int i = 1; i < limit; i++) {
    int p = (i - 1) / 2;
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = dp[p][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[p][j - w[i]] + v[i]);
      }
    }
  }
  int tt;
  cin >> tt;
  while (tt--) {
    int ver, L;
    cin >> ver >> L;
    --ver;
    vector<int> ids;
    while (ver >= limit) {
      ids.push_back(ver);
      ver = (ver - 1) / 2;
    }
    int sz = (int)ids.size();
    int ans = 0;
    for (int t = 0; t < (1 << sz); t++) {
      int sv = 0;
      int sw = 0;
      for (int i = 0; i < sz; i++) {
        if (t & (1 << i)) {
          sv += v[ids[i]];
          sw += w[ids[i]];
        }
      }
      if (sw <= L) {
        ans = max(ans, dp[ver][L - sw] + sv);
      }
    }
    cout << ans << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

ll N;
vector<ll> V, W;
ll Q;
const ll K = 9, L = 100000;
vector<vector<ll>> Dp;
ll I;

ll solve(ll i, ll l) {
  if (i <= I)
    return Dp[i][l];
  ll res = solve(i / 2, l);
  if (l >= W[i])
    res = max(res, V[i] + solve(i / 2, l - W[i]));
  return res;
}
int main() {
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (ll i = 1; i <= N; + +i)
    cin >> V[i] >> W[i];
  I = min(N, (1ll << K) - 1);
  Dp.resize(I + 1);
  for (auto &dp : Dp)
    dp.resize(L + 1);
  for (ll i = 1; i <= I; ++i) {
    for (ll l = 0; l <= L; ++l) {
      Dp[i][l] = Dp[i / 2][l];
      if (W[i] <= l)
        Dp[i][l] = max(Dp[i][l], Dp[i / 2][l - W[i]] + V[i]);
    }
  }
  cin >> Q;
  for (ll q = 0; q < Q; ++q) {
    ll i, l;
    cin >> i >> l;
    cout << solve(i, l) << endl;
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
const long long M = 998244353;
const long long iv2 = 499122177;
int n, t, i, v[N], w[N], l, q, j;
int dp[512][100005];
int dfs(int t, int l) {
  if (t < 512)
    return dp[t][l];
  int s = dfs(t / 2, l);
  if (l >= w[t])
    s = max(s, dfs(t / 2, l - w[t]) + v[t]);
  return s;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d %d", &v[i], &w[i]);
  for (i = 1; i < 512 && i <= n; ++i) {
    for (j = 100000; j >= w[i]; --j)
      dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
    for (j = w[i] - 1; j >= 0; --j)
      dp[i][j] = dp[i / 2][j];
  }
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &t, &l);
    printf("%d\\n", dfs(t, l));
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
const long long M = 998244353;
const long long iv2 = 499122177;
int n, t, i, v[N], w[N], l, q;
map<int, long long> dp[262144];
long long dfs(int t, int l) {
  if (t == 0)
    return 0;
  if (dp[t][l])
    return dp[t][l];
  long long s = dfs(t / 2, l);
  if (l >= w[t])
    s = max(s, dfs(t / 2, l - w[t]) + v[t]);
  return s;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i)
    scanf("%d %d", &v[i], &w[i]);
  scanf("%d", &q);
  for (i = 1; i <= q; ++i) {
    scanf("%d %d", &t, &l);
    printf("%d\\n", dfs(t, l));
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

const int mxn = 262150;
const int mxl = 1e5 + 1;
int n, q, v[mxn], w[mxn], dp[513][mxn];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i <= min(511, n); i++) {
    int p = i / 2;
    for (int j = 0; j < mxl; j++) {
      dp[i][j] = dp[p][j];
      if (j - w[i] >= 0) {
        dp[i][j] = max(dp[i][j], dp[p][j - w[i]] + v[i]);
      }
    }
  }
  cin >> q;
  while (q--) {
    int u, l;
    cin >> u >> l;
    vector<int> path;
    while (u > min(511, n)) {
      path.push_back(u);
      u /= 2;
    }
    int sz = path.size();
    int ans = 0;
    for (int i = 0; i < (1 << sz); i++) {
      int tw = 0, tv = 0;
      for (int j = 0; j < sz; j++) {
        if (i & (1 << j)) {
          tw += w[path[j]];
          tv += v[path[j]];
        }
      }
      if (l - tw >= 0) {
        ans = max(ans, dp[u][l - tw] + tv);
      }
    }
    cout << ans << '\\n';
  }
}

Error: -11

Fixed submission:

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

const int mxn = 2e5 + 5;
const int mxl = 1e5 + 1;
int n, q, v[mxn], w[mxn], par[mxn], dp[513][mxl];

// ifstream cin("in.txt");
// ofstream cout("out.txt");

int solve(int u, int l) {
  if (u <= 511)
    return dp[u][l];
  else {
    vector<int> path;
    while (u >= 512) {
      path.push_back(u);
      u = par[u];
    }
    int t = path.size();
    int ret = 0;
    for (int i = 0; i < (1 << t); i++) {
      int tw = 0, tv = 0;
      for (int j = 0; j < t; j++) {
        if (i & (1 << j)) {
          tw += w[path[j]];
          tv += v[path[j]];
        }
      }
      if (l - tw < 0)
        continue;
      ret = max(ret, dp[u][l - tw] + tv);
    }
    return ret;
  }
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 2; i <= n; i++) {
    par[i] = i / 2;
  }
  for (int i = 1; i <= min(511, n); i++) {
    for (int j = 1; j < mxl; j++) {
      dp[i][j] = dp[par[i]][j];
      if (j - w[i] < 0)
        continue;
      dp[i][j] = max(dp[i][j], dp[par[i]][j - w[i]] + v[i]);
    }
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, l;
    cin >> u >> l;
    cout << solve(u, l) << '\\n';
  }
}