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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;
vector<pair<int, int>> p;
int dp[(1ll << 9)][100001];
signed main() {
  cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  int v[n], w[n];
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];

  for (int i = 0; i < w[0]; i++)
    dp[0][i] = 0;
  for (int i = w[0]; i < 100001; i++)
    dp[0][i] = v[0];
  for (int i = 1; i < n && i < (1ll << 9); i++) {
    for (int j = 0; j < w[i]; j++)
      dp[i][j] = dp[(i + 1) / 2 - 1][j];
    for (int j = w[i]; j < 100001; j++)
      dp[i][j] =
          max(dp[(i + 1) / 2 - 1][j], dp[(i + 1) / 2 - 1][j - w[i]] + v[i]);
  }
  int q, x, l;
  cin >> q;
  for (int Q = 0; Q < q; Q++) {
    cin >> x >> l;
    x--;
    p.clear();
    p.push_back({0, 0});
    while (x >= (1ll << 9)) {
      int t = p.size();
      for (int i = 0; i < t; i++)
        if (p[i].first + w[x] <= l)
          p.push_back({p[i].first + w[x], p[i].second + v[x]});
      x = (x + 1) / 2 - 1;
    }
    int ans = 0;
    for (auto &&i : p)
      ans = max(ans, dp[x][l - i.first] + i.second);
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 510000;
const int MOD = 1000000007;

int dp[(1ll << 9)][100001];
signed main() {
  cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  int v[n], w[n];
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];

  for (int i = 0; i < w[0]; i++)
    dp[0][i] = 0;
  for (int i = w[0]; i < 100001; i++)
    dp[0][i] = v[0];
  for (int i = 1; i < n && i < (1ll << 9); i++) {
    for (int j = 0; j < w[i]; j++)
      dp[i][j] = dp[(i + 1) / 2 - 1][j];
    for (int j = w[i]; j < 100001; j++)
      dp[i][j] =
          max(dp[(i + 1) / 2 - 1][j], dp[(i + 1) / 2 - 1][j - w[i]] + v[i]);
  }
  int q, x, l;
  cin >> q;
  for (int Q = 0; Q < q; Q++) {
    cin >> x >> l;
    x--;
    vector<pair<int, int>> p;
    p.push_back({0, 0});
    while (x > (1ll << 9)) {
      int t = p.size();
      for (int i = 0; i < t; i++)
        if (p[i].first + w[x] <= l)
          p.push_back({p[i].first + w[x], p[i].second + v[x]});
      x = (x + 1) / 2 - 1;
    }
    int ans = 0;
    for (auto &&i : p)
      ans = max(ans, dp[x][l - i.first] + i.second);
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;

typedef pair<int, int> pii;

int v[300000], w[300000];
int dp[600][100010];
int maxj[300000];

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 <= min(n, 512); i++) {
    vector<pii> a;
    int x = i;
    while (x) {
      a.push_back(mp(w[x], v[x]));
      x >>= 1;
    }
    //		cout << " i:" << i << endl;
    for (int mask = 0; mask < (1 << a.size()); mask++) {
      int sum = 0, val = 0;
      for (int j = 0; j < a.size(); j++) {
        if (mask >> j & 1)
          sum += a[j].fi, val += a[j].se;
      }
      //			cout << "      sum:" << sum << " " << val <<
      //endl;
      if (sum > 100000)
        continue;
      dp[i][sum] = max(dp[i][sum], val);
    }
    for (int j = 1; j <= 100000; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i][j]);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, L;
    scanf("%d%d", &x, &L);
    vector<pii> a;
    while (x > min(n, 512)) {
      a.push_back(mp(w[x], v[x]));
      x >>= 1;
    }
    int ans = 0;
    for (int mask = 0; mask < (1 << a.size()); mask++) {
      int sum = 0, val = 0;
      for (int j = 0; j < a.size(); j++) {
        if (mask >> j & 1)
          sum += a[j].fi, val += a[j].se;
      }
      if (sum > L)
        continue;
      ans = max(ans, dp[x][L - sum] + val);
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;

typedef pair<int, int> pii;

int v[300000], w[300000];
int maxj[300000];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, L;
    scanf("%d%d", &x, &L);
    vector<pii> a;
    while (x) {
      a.push_back(mp(w[x], v[x]));
      x >>= 1;
    }
    int N = a.size(), M = a.size() + 1 >> 1;
    sort(a.rbegin(), a.rend());
    vector<pii> all;
    for (int i = 0; i < (1 << M); i++) {
      int sum = 0, val = 0;
      for (int j = 0; j < M; j++) {
        if (i >> j & 1)
          sum += a[j].fi, val += a[j].se;
      }
      if (sum <= L)
        all.push_back(mp(sum, val));
    }
    sort(all.begin(), all.end());
    for (int i = 0; i < all.size(); i++) {
      maxj[i] = all[i].se;
      if (i)
        maxj[i] = max(maxj[i - 1], maxj[i]);
    }
    int ans = 0;
    for (int i = 0; i < (1 << N - M); i++) {
      int sum = 0, val = 0;
      for (int j = 0; j < N - M; j++) {
        if (i >> j & 1)
          sum += a[j + M].fi, val += a[j + M].se;
      }
      if (sum > L)
        continue;
      int pos = lower_bound(all.begin(), all.end(), mp(L - sum, 0x3f3f3f3f)) -
                all.begin() - 1;
      if (pos != -1)
        ans = max(ans, val + maxj[pos]);
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;

int main(int, char **) {
  const int cut = 9;
  int n;
  cin >> n;
  vector<int> V(n + 1), W(n + 1);
  rep(i, n) cin >> V[i + 1] >> W[i + 1];

  int N = min(n, 1 << (cut + 1) - 1);
  int M = 1e5;
  vector<vector<ll>> dp(N + 1, vector<ll>(M + 1));
  for (int i = 1; i <= N; i++) {
    rep(j, M + 1) {
      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;

  vector<ll> ansv;
  rep(i, q) {
    int v, L;
    cin >> v >> L;
    vector<int> vs;
    while (v > N) {
      vs.push_back(v);
      v >>= 1;
    }
    ll ans = dp[v][L];
    int k = vs.size();
    vector<ll> sumw(1 << k), sumv(1 << k);
    rep(j, k) {
      int l = 1 << j;
      for (int l2 = l; l2 < l << 1; l2++) {
        sumw[l2] = sumw[l2 ^ l] + W[vs[j]];
        sumv[l2] = sumv[l2 ^ l] + V[vs[j]];
        if (sumw[l2] <= L)
          ans = max(ans, sumv[l2] + dp[v][L - sumw[l2]]);
      }
    }
    ansv.push_back(ans);
  }
  for (auto ans : ansv)
    cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;

int main(int, char **) {
  int n;
  cin >> n;
  vector<int> V(n + 1), W(n + 1);
  rep(i, n) cin >> V[i + 1] >> W[i + 1];

  int q;
  cin >> q;
  vector<int> v(q), L(q);
  int M = 0;
  rep(i, q) {
    cin >> v[i] >> L[i];
    M = max(M, L[i]);
  }

  int k = (int)log2(n) / 2 + 1;
  vector<vector<ll>> dp(1 << k, vector<ll>(M + 1));
  for (int i = 1; i < (1 << k); i++) {
    rep(j, M + 1) {
      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];
    }
  }

  rep(i, q) {
    ll ans = 0;
    int k2 = (int)log2(v[i]) + 1 - k;
    if (k2 <= 0) {
      ans = dp[v[i]][L[i]];
    } else {
      rep(j, 1 << k2) {
        ll sumw = 0, sumv = 0;
        int p = v[i];
        rep(l, k2) {
          if (j & 1 << l) {
            sumw += W[p];
            sumv += V[p];
          }
          p /= 2;
        }
        if (sumw <= L[i])
          ans = max(ans, sumv + dp[p][L[i] - sumw]);
      }
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <cstdio>
using namespace std;
int N, V[1 << 18], W[1 << 18];
int dptable[1024][100001];
int dp(int i, int j) {
  if (i < 1024)
    return dptable[i][j];
  if (j < W[i])
    return dp(i / 2, j);
  return max(dp(i / 2, j - W[i]) + V[i], dp(i / 2, j));
}
int main() {
  int Q, v, L;
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d %d", &V[i], &W[i]);
  fill_n(dptable[0], 100001, 0);
  for (int i = 1; i < 1024; ++i) {
    for (int j = 0; j <= 100000; ++j) {
      if (j < W[i])
        dptable[i][j] = dptable[i >> 1][j];
      else
        dptable[i][j] =
            max(dptable[i >> 1][j], dptable[i >> 1][j - W[i]] + V[i]);
    }
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d %d", &v, &L);
    printf("%d\\n", dp(v, L));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
int N, V[1 << 18], W[1 << 18];
map<int, int> dptable[1 << 18];
int dp(int i, int j) {
  if (i == 0)
    return 0;
  if (dptable[i].find(j) != dptable[i].end())
    return dptable[i][j];
  if (j < W[i])
    return dptable[i][j] = dp(i / 2, j);
  return dptable[i][j] = max(dp(i / 2, j), dp(i / 2, j - W[i]) + V[i]);
}
int main() {
  int Q, v, L;
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d %d", &V[i], &W[i]);
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d %d", &v, &L);
    printf("%d\\n", dp(v, L));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 263000;
const int MAXX = 100000;

int n, v[N], w[N], q, a, b, i, best, act, j, m, res[512][MAXX + 4];
vector<ii> v1, v2;

int get(int i, int lim, int maxx) {
  v1.clear();
  v1.pb(ii(0, 0));
  int j;
  for (j = i; j >= lim; j /= 2) {
    v2.clear();
    for (auto &ww : v1)
      if (w[j] + ww.second <= maxx)
        v2.pb(ii(ww.first + v[j], ww.second + w[j]));
    for (auto &ww : v2)
      v1.pb(ww);
  }
  return j;
}

int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d %d", &v[i], &w[i]);
  m = min((1 << 9) - 1, n);
  for (i = 1; i <= m; i++) {
    get(i, 1, MAXX);
    // printf("%d\\n", v1.size());
    for (auto &ww : v1)
      if (res[i][ww.second] < ww.first)
        res[i][ww.second] = ww.first;
    best = 0;
    for (j = 0; j <= MAXX; j++)
      if (res[i][j] < best)
        res[i][j] = best;
      else
        best = res[i][j];
  }
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &a, &b);
    if (a <= m) {
      printf("%d\\n", res[a][b]);
      continue;
    }
    act = get(a, m + 1, b);
    // printf("%d\\n", act);
    best = 0;
    for (auto &ww : v1)
      best = max(best, ww.first + res[act][b - ww.second]);
    printf("%d\\n", best);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 263000;

int n, v[N], w[N], q, a, b, i;
map<int, int> mapa[N];

int get(int a, int b) {
  if (a == 1)
    return (b >= w[1]) ? v[1] : 0;
  if (mapa[a].find(b) != mapa[a].end())
    return mapa[a][b];
  int res = get(a / 2, b);
  if (b >= w[a])
    res = max(res, get(a / 2, b - w[a]) + v[a]);
  mapa[a][b] = res;
  return res;
}

int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d %d", &v[i], &w[i]);
  scanf("%d", &q);
  while (q--) {
    scanf("%d %d", &a, &b);
    printf("%d\\n", get(a, b));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#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;
ll I;
vector<vector<ll>> Dp;
vector<pair<ll, ll>> C;

ll solve(ll i, ll l) {
  C.clear();
  C.push_back({l, 0});
  while (i > I) {
    ll n = C.size();
    for (ll k = 0; k < n; ++k) {
      ll l, v;
      tie(l, v) = C[k];
      if (l >= W[i])
        C.push_back({l - W[i], v + V[i]});
    }
    i /= 2;
  }
  ll res = 0;
  for (auto lv : C) {
    ll l, v;
    tie(l, v) = lv;
    res = max(res, v + Dp[i][l]);
  }
  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;
}


Error: 0

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;
ll I;
vector<vector<ll>> Dp;
vector<pair<ll, ll>> C;

ll solve(ll i, ll l) {
  C.clear();
  C.push_back({l, 0});
  while (i > I) {
    ll n = C.size();
    for (ll k = 0; k < n; ++k) {
      ll l, v;
      tie(l, v) = C[k];
      if (l >= W[i])
        C.push_back({l - W[k], v + V[i]});
    }
    i /= 2;
  }
  ll res = 0;
  for (auto lv : C) {
    ll l, v;
    tie(l, v) = lv;
    res = max(res, v + Dp[i][l]);
  }
  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;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 998244353;
const ld PI = acosl(-1);

void merge(vector<pii> &v, pii &p, int &lim) {
  vector<pii> res;
  int nxt = 1, N = v.size();
  res.push_back({0, 0});
  for (int i = 0; i < N; ++i) {
    pii tmp = {v[i].first + p.first, v[i].second + p.second};
    while (nxt < N && v[nxt].first <= tmp.first)
      res.push_back(v[nxt++]);
    if (tmp.first <= lim)
      res.push_back(tmp);
  }
  swap(res, v);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<pii> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i].second >> A[i].first;

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int v, L;
    cin >> v >> L;

    vector<pii> sum1, sum2;
    sum1.push_back({0, 0});
    sum2.push_back({0, 0});
    for (int j = 0; v > 0; v >>= 1, ++j) {
      merge((j < 9 ? sum1 : sum2), A[v - 1], L);
    }

    int siz1 = sum1.size(), siz2 = sum2.size(), maxv = 0;
    for (int j = 0; j < siz1; ++j) {
      if (maxv < sum1[j].second)
        maxv = sum1[j].second;
      else
        sum1[j].second = maxv;
    }

    int idx = siz1 - 1, res = 0;
    for (int j = 0; j < siz2; ++j) {
      while (idx >= 0 && sum2[j].first + sum1[idx].first > L)
        idx--;
      if (idx >= 0)
        res = max(res, sum2[j].second + sum1[idx].second);
    }
    cout << res << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T> using Graph = vector<vector<T>>;
const int MOD = 998244353;
const ld PI = acosl(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<pii> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i].first >> A[i].second;

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; ++i) {
    int v, L, res = 0;
    cin >> v >> L;

    vector<pii> atmp;
    while (v) {
      atmp.push_back(A[v - 1]);
      v >>= 1;
    }

    vector<map<int, int>> dp(atmp.size() + 1);
    dp[0][0] = 0;
    for (int j = 0; j < atmp.size(); ++j) {
      for (auto k : dp[j]) {
        dp[j + 1][k.first] = dp[j][k.first];
        if (k.first + atmp[j].second > L)
          continue;
        dp[j + 1][k.first + atmp[j].second] =
            max(dp[j + 1][k.first + atmp[j].second],
                dp[j][k.first] + atmp[j].first);
      }
    }
    for (auto j : dp[atmp.size()])
      res = max(res, j.second);

    cout << res << '\\n';
  }

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

#include <bits/stdc++.h>
#define IO_OP                                                                  \\
  std::ios::sync_with_stdio(0);                                                \\
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;

int w[1 << 18], v[1 << 18], dp[520][N];

signed main() {
  IO_OP;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  int m = min(n, 512);
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < N; j++) {
      if (j - w[i] >= 0)
        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;
  while (q--) {
    int x, l;
    cin >> x >> l;
    vi tt;
    while (x > m) {
      tt.PB(x);
      x /= 2;
    }
    int mx = 0;
    for (int mask = 0; mask < (1 << (tt.size())); mask++) {
      int w_tot = 0, v_tot = 0;
      for (int i = 0; i < tt.size(); i++) {
        if (mask >> i & 1) {
          w_tot += w[tt[i]];
          v_tot += v[tt[i]];
        }
      }
      if (w_tot <= l) {
        mx = max(mx, v_tot + dp[x][l - w_tot]);
      }
    }
    cout << mx << '\\n';
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define IO_OP                                                                  \\
  std::ios::sync_with_stdio(0);                                                \\
  std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << " is " << x << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;

int w[N], v[N], dp[520][N];

signed main() {
  IO_OP;

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  int m = 1 << (__lg(n) / 2);
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < N; j++) {
      if (j - w[i] >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int x, l;
    cin >> x >> l;
    vi tt;
    while (x > m) {
      tt.PB(x);
      x /= 2;
    }
    int mx = 0;
    for (int mask = 0; mask < (1 << (tt.size())); mask++) {
      int w_tot = 0, v_tot = 0;
      for (int i = 0; i < tt.size(); i++) {
        if (mask >> i & 1) {
          w_tot += w[tt[i]];
          v_tot += v[tt[i]];
        }
      }
      if (w_tot <= l) {
        mx = max(mx, v_tot + dp[x][l - w_tot]);
      }
    }
    cout << mx << '\\n';
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int n, q;
int v[(1 << 18) + 1], w[(1 << 18) + 1];
int dp[550][100001];
int inf = -1;
int vq, wq;

int dfs(int i, int weight) {
  // cout << i << " " << weight << " " << dp[i][weight] << endl;
  int res = 0;
  if (i < 550 && weight <= 100000) {
    if (dp[i][weight] != inf)
      return dp[i][weight];
  }
  if (i == 0) {
    res = 0;
  } else if (weight < w[i]) {
    res = dfs(i / 2, weight);
  } else {
    res = max(dfs(i / 2, weight), dfs(i / 2, weight - w[i]) + v[i]);
  }
  if (i < 550 && weight <= 100000)
    dp[i][weight] = res;
  return res;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < 550; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = inf;
    }
  }

  for (int j = 0; j < 512; j++) {
    for (int k = 0; k <= 100000; k++) {
      dfs(j, k);
    }
  }

  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> vq >> wq;
    cout << dfs(vq, wq) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int n, q;
int v[(1 << 18) + 1], w[(1 << 18) + 1];
int dp[550][100001];
int inf = -1;
int vq, wq;

int dfs(int i, int weight) {
  // cout << i << " " << weight << " " << dp[i][weight] << endl;
  int res = 0;
  if (i < 550 && weight <= 100000) {
    if (dp[i][weight] != inf)
      return dp[i][weight];
  }
  if (i == 0) {
    res = 0;
  } else if (weight < w[i]) {
    res = dfs(i / 2, weight);
  } else {
    res = max(dfs(i / 2, weight), dfs(i / 2, weight - w[i]) + v[i]);
  }
  if (i < 550 && weight <= 100000)
    dp[i][weight] = res;
  return res;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < 550; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = inf;
    }
  }

  cin >> q;
  for (int j = 0; j < q; j++) {
    cin >> vq >> wq;
    cout << dfs(vq, wq) << endl;
  }
  return 0;
}