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

#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 5;
int fa[N], f[1 << 9][N], v[N], w[N];
int main() {
  //	freopen("in05.txt","r",stdin);
  //	freopen("out05.txt","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i], fa[i] = i / 2;
  for (int i = 1; i < (1 << 9); ++i) {
    for (int j = 1; j <= 1e5; ++j)
      f[i][j] = f[fa[i]][j];
    for (int j = w[i]; j <= 1e5; ++j)
      f[i][j] = max(f[i][j], f[fa[i]][j - w[i]] + v[i]);
  }
  // cout<<"erhtyjrstyd\\n";
  int q, x, l;
  cin >> q;
  while (q--) {
    cin >> x >> l;
    // cout<<x<<" "<<l<<endl;
    if (x < (1 << 9)) {
      cout << f[x][l] << endl;
      continue;
    }
    vector<pair<int, int>> vec;
    while (x >= (1 << 9))
      vec.push_back(make_pair(v[x], w[x])), x = fa[x];
    // cout<<"dfgbtfhrteff\\n";
    int cnt = vec.size(), ans = f[x][l];
    // cout<<(1<<cnt)<<" "<<cnt<<endl;
    for (int i = 0; i < (1 << cnt); ++i) {
      int val = 0, wei = 0;
      for (int j = 0; j < cnt; ++j)
        if ((i >> j) & 1)
          val += vec[j].first, wei += vec[j].second;
      // cout<<i<<" "<<val<<" "<<wei<<endl;
      if (wei <= l)
        ans = max(ans, f[x][l - wei] + val);
    }
    // cout<<"rgerydgg\\n";
    cout << ans << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int fa[N], f[1 << 9][N], v[1 << 18], w[1 << 18];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i], fa[i] = i / 2;
  for (int i = 1; i < (1 << 9); ++i) {
    for (int j = 1; j <= 1e5; ++j)
      f[i][j] = f[fa[i]][j];
    for (int j = w[i]; j <= 1e5; ++j)
      f[i][j] = max(f[i][j], f[fa[i]][j - w[i]] + v[i]);
  }
  int q, x, l;
  cin >> q;
  while (q--) {
    cin >> x >> l;
    if (x < (1 << 9)) {
      cout << f[x][l] << endl;
      continue;
    }
    vector<pair<int, int>> vec;
    while (x >= (1 << 9))
      vec.push_back(make_pair(v[x], w[x])), x = fa[x];
    int cnt = vec.size(), ans = f[x][l];
    for (int i = 0; i < (1 << cnt); ++i) {
      int val = 0, wei = 0;
      for (int j = 0; j < cnt; ++j)
        if ((i >> j) & 1)
          val += vec[j].first, wei += vec[i].second;
      if (wei <= l)
        ans = max(ans, f[x][l - wei] + val);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 300005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline ll read() {
  ll x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll n, a[maxn], b[maxn], dp[520][maxn], L;

inline ll sol(int u, ll nw) {
  if (u <= 512)
    return dp[u][nw];
  ll tmp = 0;
  if (nw >= b[u])
    tmp = max(tmp, sol(u / 2, nw - b[u]) + a[u]);
  tmp = max(tmp, sol(u / 2, nw));
  return tmp;
}

int main() {
  n = read();
  rep(i, 1, n) a[i] = read(), b[i] = read();
  L = 1e5;
  rep(i, 1, 512) {
    rep(j, 1, L) dp[i][j] = dp[i / 2][j];
    per(j, L, b[i]) dp[i][j] = max(dp[i][j], dp[i][j - b[i]] + a[i]);
  }
  int Q = read();
  while (Q--) {
    int v = read(), l = read();
    printf("%lld\\n", sol(v, l));
  }
  return 0;
}

Error: -11

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 300005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

ll n, a[maxn], b[maxn], dp[520][maxn], L;

inline ll sol(int u, ll nw) {
  if (u <= 512)
    return dp[u][nw];
  ll tmp = 0;
  if (nw >= b[u])
    tmp = max(tmp, sol(u / 2, nw - b[u]) + a[u]);
  tmp = max(tmp, sol(u / 2, nw));
}

int main() {
  n = read();
  rep(i, 1, n) a[i] = read(), b[i] = read();
  L = 1e5;
  rep(i, 1, 512) {
    rep(j, 1, L) dp[i][j] = dp[i / 2][j];
    per(j, L, b[i]) dp[i][j] = max(dp[i][j], dp[i][j - b[i]] + a[i]);
  }
  int Q = read();
  while (Q--) {
    int v = read(), l = read();
    printf("%lld\\n", sol(v, l));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const ll inf = 1e18;

const int K = (1 << 9);

int n;
ll v[maxn], w[maxn];
vector<pair<ll, ll>> dp[K + 10];

vector<pair<ll, ll>> all;

void go(int at, ll val, ll wei) {
  if (at == 0) {
    all.push_back({wei, val});
    return;
  }
  go(at / 2, val, wei);
  go(at / 2, val + v[at], wei + w[at]);
}

void compute(int at) {
  all.clear();
  go(at, 0, 0);
  sort(all.begin(), all.end());

  auto &res = dp[at];
  for (auto p : all) {
    if (res.empty() || res.back().second < p.second) {
      res.push_back(p);
    }
  }
}

ll dfs(int at, ll val, ll sack) {
  if (sack < 0) {
    return -inf;
  }
  if (at == 0) {
    return val;
  }
  if (at <= K) {
    auto iter = std::prev(
        upper_bound(dp[at].begin(), dp[at].end(), make_pair(sack, inf)));
    return val + iter->second;
  }
  return max(dfs(at / 2, val, sack), dfs(at / 2, val + v[at], sack - w[at]));
}

ll solve(int at, ll L) { return dfs(at, 0, L); }

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

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

  for (int i = 1; i <= min(n, K); i++) {
    compute(i);
  }

  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    cout << solve(v, L) << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

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

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const ll inf = 1e18;

const int K = (1 << 18);

int n;
ll v[maxn], w[maxn];

vector<pair<ll, ll>> dp[K + 10];

vector<pair<ll, ll>> all;

void go(int at, ll val, ll wei) {
  all.push_back({wei, val});
  if (at == 0)
    return;
  go(at / 2, val, wei);
  go(at / 2, val + v[at], wei + w[at]);
}

void compute(int at) {
  all.clear();
  go(at, 0, 0);
  sort(all.begin(), all.end());

  auto &res = dp[at];
  for (auto p : all) {
    if (res.empty() || res.back().second < p.second) {
      res.push_back(p);
    }
  }
}

ll dfs(int at, ll val, ll sack) {
  if (sack < 0) {
    return -inf;
  }
  if (at == 0) {
    return val;
  }
  if (at <= K) {
    auto iter = std::prev(
        upper_bound(dp[at].begin(), dp[at].end(), make_pair(sack, inf)));
    return iter->second;
  }
  return max(dfs(at / 2, val, sack), dfs(at / 2, val + v[at], sack - w[at]));
}

ll solve(int at, ll L) { return dfs(at, 0, L); }

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

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

  for (int i = 1; i <= min(n, K); i++) {
    compute(i);
  }

  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    cout << solve(v, L) << "\\n";
  }

  return 0;
}
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 vi = vector<int>;
using vv = vector<vi>;

int main() {
  int n;
  cin >> n;
  vi v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];

  int q;
  cin >> q;
  vi s(q), tot(q);
  int mtot = 0;
  rep(i, q) {
    cin >> s[i] >> tot[i];
    mtot = max(mtot, tot[i]);
  }

  int size = min(n + 1, 512);
  vv dp(size, vi(mtot + 2, 0));

  for (int i = 1; i < size; i++) {
    for (int j = 1; j <= mtot; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i / 2][j]);
      if (j - w[i] >= 0)
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
    }
  } // dp[1~511][0~100000]のvが求まった

  rep(i, q) {
    if (s[i] < size) {
      cout << dp[s[i]][tot[i]] << endl;
      continue;
    }

    int len = 0, b = s[i];
    while (b > 511) {
      b /= 2;
      len++;
    }

    // int len = (int)log2(s[i])-8+1;

    int ans = 0;

    rep(k, 1 << len) {
      int vsum = 0, wsum = 0;
      b = s[i];
      rep(j, len) {
        // if(wsum>tot[i])continue;
        if (k >> j & 1) {
          vsum += v[b];
          wsum += w[b];
        }
        b /= 2;
      }

      if (wsum > tot[i])
        continue;
      ans = max(ans, vsum + dp[b][tot[i] - wsum]);
    }

    cout << ans << 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 vi = vector<int>;
using vv = vector<vi>;

int main() {
  int n;
  cin >> n;
  vi v(n + 1), w(n + 1);
  rep(i, n) cin >> v[i + 1] >> w[i + 1];

  int q;
  cin >> q;
  vi s(q), tot(q);
  int mtot = 0;
  rep(i, q) {
    cin >> s[i] >> tot[i];
    mtot = max(mtot, tot[i]);
  }

  int size = min(n + 1, 512);
  vv dp(size, vi(mtot + 2, 0));

  for (int i = 1; i < size; i++) {
    for (int j = 1; j <= mtot; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i / 2][j]);
      if (j - w[i] >= 0)
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
    }
  } // dp[1~511][0~100000]のvが求まった

  rep(i, q) {
    if (s[i] < size) {
      cout << dp[s[i]][tot[i]] << endl;
      continue;
    }

    /*
    int len=0, b=s[i];
    while(b>511){
        b /= 2;
        len ++;
    }
    */

    int len = (int)log2(s[i]) - 8 + 1;

    int ans = 0;

    rep(k, 1 << len) {
      int vsum = 0, wsum = 0;
      int b = s[i];
      rep(j, len) {
        // if(wsum>tot[i])continue;
        if (k >> j & 1) {
          vsum += v[b];
          wsum += w[b];
        }
        b /= 2;
      }

      if (wsum > tot[i])
        continue;
      ans = max(ans, vsum + dp[b][tot[i] - wsum]);
    }

    cout << ans << endl;
  }

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

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

int main() {
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v.at(i) >> w.at(i);
  }
  cin >> q;
  vector<int> u(q), l(q);
  for (int i = 0; i < q; i++) {
    cin >> u.at(i) >> l.at(i);
  }
  int d = 9;
  while ((1 << d) > n)
    d--;
  vector<vector<int>> dp(1 << d, vector<int>(100001));
  for (int i = 1; i < (1 << d); i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j < w[i])
        continue;
      dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
    }
    for (int j = 1; j <= 100000; j++) {
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  for (int i = 0; i < q; i++) {
    if (u.at(i) < (1 << d)) {
      cout << dp[u[i]][l[i]] << "\\n";
    } else {
      int dq = 0, ans = 0;
      while ((u[i] >> dq) >= (1 << d))
        dq++;
      for (int j = 0; j < (1 << dq); j++) {
        int sv = 0, sw = 0;
        for (int k = 0; k < dq; k++) {
          if ((j >> k) & 1)
            continue;
          sv += v.at(u[i] >> k);
          sw += w.at(u[i] >> k);
        }
        if (sw > l[i])
          continue;
        ans = max(ans, sv + dp[u[i] >> dq][l[i] - sw]);
      }
      cout << ans << "\\n";
    }
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int n, q;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v.at(i) >> w.at(i);
  }
  cin >> q;
  vector<int> u(q), l(q);
  for (int i = 0; i < q; i++) {
    cin >> u.at(i) >> l.at(i);
  }
  int d = 9;
  while ((1 << d) > n)
    d--;
  vector<vector<int>> dp(1 << d, vector<int>(100001));
  for (int i = 1; i < (1 << d); i++) {
    for (int j = 0; j <= 100000; j++) {
      dp.at(i).at(j) = dp.at(i / 2).at(j);
      if (j < w.at(i))
        continue;
      dp.at(i).at(j) =
          max(dp.at(i).at(j), dp.at(i / 2).at(j - w.at(i)) + v.at(i));
    }
    for (int j = 1; j <= 100000; j++) {
      dp.at(i).at(j) = max(dp.at(i).at(j), dp.at(i).at(j - 1));
    }
  }
  for (int i = 0; i < q; i++) {
    if (u.at(i) < (1 << d)) {
      cout << dp.at(u.at(i)).at(l.at(i)) << "\\n";
    } else {
      int dq = 0, ans = 0;
      while ((u[i] >> dq) >= (1 << d))
        dq++;
      for (int j = 0; j < (1 << dq); j++) {
        int sv = 0, sw = 0;
        for (int k = 0; k < dq; k++) {
          if ((j >> k) & 1)
            continue;
          sv += v.at(u[i] >> k);
          sw += w.at(u[i] >> k);
        }
        if (sw > l[i])
          continue;
        ans = max(ans, sv + dp.at(u[i] >> dq).at(l[i] - sw));
      }
      cout << ans << "\\n";
    }
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

int dp[1024][100001], v[323456], w[323456];
int n, q;

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 (~dp[a][l])
    return dp[a][l];
  return dp[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
}

int main() {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  cin >> q;

  while (q--) {
    int a, l;
    cin >> a >> l;
    cout << f(a, l) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

ll dp[1024][100001], v[323456], w[323456];
ll n;
ll q;

ll f(ll a, ll l) {
  if (l < 0)
    return -inf;
  if (!a)
    return 0;
  if (a > 1023)
    return max(f(a / 2, l), f(a / 2, l - w[a]) + v[a]);
  if (~dp[a][l])
    return dp[a][l];
  return dp[a][l] = max({f(a / 2, l), f(a, l - 1), f(a / 2, l - w[a]) + v[a]});
}

int main() {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (ll i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  cin >> q;

  while (q--) {
    ll a, l;
    cin >> a >> l;
    cout << f(a, l) << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
// #define int long long
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
constexpr long long inf = 1e17;
constexpr int mod = 1000000007;
int V[270000], W[270000];
int v[100006], L[100006];
ll dp[512][100006];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) cin >> V[i] >> W[i];
  int q;
  cin >> q;
  dp[0][W[0]] = V[0];
  rep(j, 100000) dp[0][j + 1] = max(dp[0][j], dp[0][j + 1]);
  for (int i = 1; i < min(n, 511); i++) {
    rep(j, 100001) {
      dp[i][j] = max(dp[i][j], dp[(i - 1) >> 1][j]);
      if (j + W[i] <= 100000)
        dp[i][j + W[i]] = max(dp[i][j + W[i]], dp[(i - 1) >> 1][j] + V[i]);
    }
    rep(j, 100000) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  }
  rep(i, q) {
    cin >> v[i] >> L[i];
    v[i]--;
    vector<int> t;
    while (v[i] >= 511) {
      t.emplace_back(v[i]);
      v[i] = (v[i] - 1) >> 1;
    }
    ll ans = 0;
    ll v2, w2;
    rep(j, 1 << t.size()) {
      v2 = 0, w2 = 0;
      rep(k, t.size()) {
        if (j & (1 << k)) {
          v2 += V[t[k]];
          w2 += W[t[k]];
        }
      }
      if (w2 <= L[i]) {
        ans = max(ans, v2 + dp[v[i]][L[i] - w2]);
      }
    }
    cout << ans << "\\n";
  }
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
// #define int long long
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
constexpr long long inf = 1e17;
constexpr int mod = 1000000007;
int V[270000], W[270000];
int v[100006], L[100006];
ll dp[257][100006];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  rep(i, n) cin >> V[i] >> W[i];
  int q;
  cin >> q;
  dp[0][W[0]] = V[0];
  rep(j, 100000) dp[0][j + 1] = max(dp[0][j], dp[0][j + 1]);
  for (int i = 1; i < min(n, 255); i++) {
    rep(j, 100001) {
      dp[i][j] = max(dp[i][j], dp[(i - 1) >> 1][j]);
      if (j + W[i] <= 100000)
        dp[i][j + W[i]] = max(dp[i][j + W[i]], dp[(i - 1) >> 1][j] + V[i]);
    }
    rep(j, 100000) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  }
  rep(i, q) {
    cin >> v[i] >> L[i];
    v[i]--;
    vector<int> t;
    while (v[i] >= 255) {
      t.emplace_back(v[i]);
      v[i] = (v[i] - 1) >> 1;
    }
    ll ans = 0;
    ll v2, w2;
    rep(j, 1 << t.size()) {
      v2 = 0, w2 = 0;
      rep(k, t.size()) {
        if (j & (1 << k)) {
          v2 += V[t[k]];
          w2 += W[t[k]];
        }
      }
      if (w2 <= L[i]) {
        ans = max(ans, v2 + dp[v[i]][L[i] - w2]);
      }
    }
    cout << ans << "\\n";
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

constexpr int prep = 10;
int64_t dp[(1 << prep) + 2][100001];

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

  int n;
  cin >> n;
  static int64_t v[(1 << 18) + 2], w[(1 << 18) + 2];
  static int depth[(1 << 18) + 2];
  depth[0] = 0;
  for (int i = 1; i <= n; ++i)
    depth[i] = depth[i / 2] + 1;

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

  for (int i = 1; i <= min(n, (1 << prep) - 1); ++i) {
    for (int j = 0; j <= 100000; ++j)
      dp[i][j] = dp[i / 2][j];
    for (int j = 0; j <= 100000; ++j) {
      if (j + w[i] <= 100000)
        dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[i / 2][j] + v[i]);
    }
    for (int j = 1; j <= 100000; ++j)
      if (dp[i][j - 1] > dp[i][j])
        dp[i][j] = dp[i][j - 1];
  }

  int q;
  cin >> q;
  while (q--) {
    int vv, ll;
    cin >> vv >> ll;
    if (vv < (1 << prep)) {
      cout << dp[vv][ll] << endl;
      continue;
    }
    int64_t cand = 0;
    for (int ptn = 0; ptn < (1 << depth[vv] - prep); ++ptn) {
      int ii = vv, p = ptn;
      int64_t we = 0, vl = 0;
      while (depth[ii] > prep) {
        if (p & 1) {
          we += w[ii];
          vl += v[ii];
        }
        ii /= 2;
        p /= 2;
        if (we > ll)
          goto l_ng;
      }
      cand = max(cand, dp[ii][ll - we] + vl);
    l_ng:;
    }
    cout << cand << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int64_t dp[(1 << 9) + 2][100001];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  static int64_t v[(1 << 18) + 2], w[(1 << 18) + 2];
  static int depth[(1 << 18) + 2];
  depth[0] = 0;
  for (int i = 1; i <= n; ++i)
    depth[i] = depth[i / 2] + 1;

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

  for (int i = 1; i <= min(n, (1 << 9) - 1); ++i) {
    for (int j = 0; j <= 100000; ++j)
      dp[i][j] = dp[i / 2][j];
    for (int j = 0; j <= 100000; ++j) {
      if (j + w[i] <= 100000)
        dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[i / 2][j] + v[i]);
    }
    for (int j = 1; j <= 100000; ++j)
      if (dp[i][j - 1] > dp[i][j])
        dp[i][j] = dp[i][j - 1];
  }

  int q;
  cin >> q;
  while (q--) {
    int vv, ll;
    cin >> vv >> ll;
    if (vv < (1 << 9)) {
      cout << dp[vv][ll] << endl;
      continue;
    }
    int64_t cand = 0;
    for (int ptn = 0; ptn < (1 << depth[vv] - 9); ++ptn) {
      int ii = vv, p = ptn;
      int64_t we = 0, vl = 0;
      while (depth[ii] > 9) {
        if (p & 1) {
          we += w[ii];
          vl += v[ii];
        }
        ii /= 2;
        p /= 2;
        if (we > ll)
          goto l_ng;
      }
      cand = max(cand, dp[ii][ll - we] + vl);
    l_ng:;
    }
    cout << cand << endl;
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll N;
ll V[1 << 18], W[1 << 18];
ll dp[1 << 9][100001] = {};
ll Dfs(ll vert, ll w) {
  if (vert == 0)
    return 0;
  if (dp[vert][w] != -1)
    return dp[vert][w];

  ll ret = Dfs(vert / 2, w);
  if (w >= W[vert]) {
    ret = max(ret, V[vert] + Dfs(vert / 2, w - W[vert]));
  }
  return dp[vert][w] = ret;
}

ll Dfs2(ll vert, ll w) {
  if (vert < (1 << 9))
    return dp[vert][w];
  ll ret = 0;
  ret = max(ret, Dfs2(vert / 2, w));
  if (w < W[vert])
    return ret;
  return max(ret, V[vert] + Dfs2(vert / 2, w - W[vert]));
}
int main() {
  cin >> N;
  for (ll i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  for (ll i = 1; i < (1ll << 9); i++)
    for (ll j = 0; j <= 100000; j++)
      dp[i][j] = -1;
  for (ll i = 1; i < (1ll << 9); i++)
    for (ll j = 0; j <= 100000; j++)
      Dfs(i, j);
  ll Q;
  cin >> Q;
  for (ll i = 0; i < Q; i++) {
    ll v, L;
    cin >> v >> L;
    cout << Dfs2(v, L) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll N;
ll V[1 << 18], W[1 << 18];
ll dp[1 << 9][100001] = {};
ll Dfs(ll vert, ll w) {
  if (vert == 0)
    return 0;
  if (dp[vert][w] != -1)
    return dp[vert][w];

  ll ret = Dfs(vert / 2, w);
  if (w >= W[vert]) {
    ret = max(ret, V[vert] + Dfs(vert / 2, w - W[vert]));
  }
  return dp[vert][w] = ret;
}
int main() {
  cin >> N;
  for (ll i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  for (ll i = 1; i < (1ll << 9); i++)
    for (ll j = 0; j <= 100000; j++)
      dp[i][j] = -1;
  for (ll i = 1; i < (1ll << 9); i++)
    for (ll j = 0; j <= 100000; j++)
      Dfs(i, j);
  ll Q;
  cin >> Q;
  for (ll i = 0; i < Q; i++) {
    ll v, L;
    cin >> v >> L;
    vector<ll> vs;
    while (v >= (1 << 9)) {
      vs.push_back(v);
      v /= 2;
    }
    ll S = vs.size();
    ll ans = 0;
    for (ll state = 0; state < (1 << S); state++) {
      ll vSum = 0;
      ll wSum = 0;
      for (ll i = 0; i < S; i++) {
        if (state & (1 << i)) {
          vSum += V[vs[i]];
          wSum += W[vs[i]];
        }
      }
      if (wSum > L)
        continue;
      vSum += dp[v][L - wSum];
      ans = max(ans, vSum);
    }
    cout << ans << endl;
  }

  return 0;
}