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

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

// @author: pashka

int BUBEN = (1 << 9) - 1;
int MAX = 100001;

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int nn = min(n, BUBEN);
  vector<vector<int>> d(nn, vector<int>(MAX));
  for (int j = w[0]; j < MAX; j++) {
    d[0][j] = v[0];
  }
  for (int i = 1; i < nn; i++) {
    int p = (i - 1) / 2;
    for (int j = 0; j < MAX; j++) {
      d[i][j] = d[p][j];
      if (j >= w[i]) {
        d[i][j] = max(d[i][j], d[p][j - w[i]] + v[i]);
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int t, l;
    cin >> t >> l;
    t--;
    vector<int> c;
    while (t >= BUBEN) {
      c.push_back(t);
      t = (t - 1) / 2;
    }
    int m = c.size();
    int res = 0;
    for (int x = 0; x < (1 << m); x++) {
      int sv = 0;
      int sw = 0;
      for (int i = 0; i < m; i++) {
        if (x & (1 << i)) {
          sv += v[c[i]];
          sw += w[c[i]];
        }
      }
      if (sw <= l) {
        res = max(res, sv + d[t][l - sw]);
      }
    }
    cout << res << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

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

// @author: pashka

int BUBEN = (1 << 9) - 1;
int MAX = 100001;

int main() {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int nn = min(n, BUBEN);
  vector<vector<int>> d(nn, vector<int>(MAX));
  for (int j = w[0]; j < MAX; j++) {
    d[0][j] = v[0];
  }
  for (int i = 1; i < nn; i++) {
    int p = (i - 1) / 2;
    for (int j = 0; j < MAX; j++) {
      d[i][j] = d[p][j];
      if (j >= w[i]) {
        d[i][j] = max(d[i][j], d[p][j - w[i]] + v[i]);
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int t, l;
    cin >> t >> l;
    t--;
    vector<int> c;
    while (t > BUBEN) {
      c.push_back(t);
      t = (t - 1) / 2;
    }
    int m = c.size();
    int res = 0;
    for (int x = 0; x < (1 << m); x++) {
      int sv = 0;
      int sw = 0;
      for (int i = 0; i < m; i++) {
        if (x & (1 << i)) {
          sv += v[c[i]];
          sw += w[c[i]];
        }
      }
      if (sw <= l) {
        res = max(res, sv + d[t][l - sw]);
      }
    }
    cout << res << "\\n";
  }

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

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

typedef long long i64;

const int N = 1 << 18;

int n, L, v[N], w[N], lim;
vector<int> vec;
vector<pair<int, i64>> _opt[N];
vector<pair<int, i64>> all;
vector<pair<int, i64>> opt;
i64 ans;

void dfs(int i, int noww, i64 nowv) {
  if (i == lim) {
    all.emplace_back(noww, nowv);
    return;
  }
  dfs(i + 1, noww, nowv);
  dfs(i + 1, noww + w[vec[i]], nowv + v[vec[i]]);
}
void dfs2(int i, int noww, i64 nowv) {
  if (noww > L)
    return;
  if (i == vec.size()) {
    auto it =
        upper_bound(opt.begin(), opt.end(), make_pair(L - noww, (i64)1e18));
    if (it != opt.begin()) {
      --it;
      ans = max(ans, it->second + nowv);
    }
    return;
  }
  dfs2(i + 1, noww, nowv);
  dfs2(i + 1, noww + w[vec[i]], nowv + v[vec[i]]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i];
  int q;
  cin >> q;
  for (int v; q--;) {
    cin >> v >> L;
    vec.clear();
    for (int i = v; i; i >>= 1)
      vec.push_back(i);
    reverse(vec.begin(), vec.end());
    lim = min<int>(11, vec.size());
    if (!_opt[vec[lim - 1]].empty())
      opt = _opt[vec[lim - 1]];
    else {
      all.clear();
      dfs(0, 0, 0);
      sort(all.begin(), all.end());
      opt.clear();
      for (int i = all.size() - 1; i >= 0; --i) {
        while (!opt.empty() && opt.back().second <= all[i].second)
          opt.pop_back();
        opt.push_back(all[i]);
      }
      reverse(opt.begin(), opt.end());
      _opt[vec[lim - 1]] = opt;
    }
    ans = 0;
    dfs2(lim, 0, 0);
    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

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

typedef long long i64;

const int N = 1 << 18;

int n, L, v[N], w[N];
vector<int> vec;
vector<pair<int, i64>> all;
vector<pair<int, i64>> opt;
i64 ans;

void dfs(int i, int noww, i64 nowv) {
  if (noww > L)
    return;
  if (i == vec.size() / 2) {
    all.emplace_back(noww, nowv);
    return;
  }
  dfs(i + 1, noww, nowv);
  dfs(i + 1, noww + w[vec[i]], nowv + v[vec[i]]);
}
void dfs2(int i, int noww, i64 nowv) {
  if (noww > L)
    return;
  if (i == vec.size()) {
    auto it =
        upper_bound(opt.begin(), opt.end(), make_pair(L - noww, (i64)1e18));
    --it;
    ans = max(ans, it->second + nowv);
    return;
  }
  dfs2(i + 1, noww, nowv);
  dfs2(i + 1, noww + w[vec[i]], nowv + v[vec[i]]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i];
  int q;
  cin >> q;
  for (int v; q--;) {
    cin >> v >> L;
    vec.clear();
    for (int i = v; i; i >>= 1)
      vec.push_back(i);
    all.clear();
    dfs(0, 0, 0);
    sort(all.begin(), all.end());
    opt.clear();
    for (int i = all.size() - 1; i >= 0; --i) {
      while (!opt.empty() && opt.back().second <= all[i].second)
        opt.pop_back();
      opt.push_back(all[i]);
    }
    reverse(opt.begin(), opt.end());
    ans = 0;
    dfs2(vec.size() / 2, 0, 0);
    cout << ans << '\\n';
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll dp[1023][100001];
int w[1000000];
int v[1000000];

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

  rep(i, 100001) { dp[0][i] = (i < w[0] ? 0 : v[0]); }

  for (int i = 1; i < min(n, 1023); i++) {
    rep(j, 100001) {
      dp[i][j] = max(dp[(i - 1) / 2][j],
                     (j < w[i] ? 0 : dp[(i - 1) / 2][j - w[i]] + v[i]));
    }
  }

  cin >> q;
  vector<P> query(q);
  rep(i, q) { cin >> query[i].first >> query[i].second; }
  rep(i, q) {
    int vid, l;
    vid = query[i].first - 1;
    l = query[i].second;

    if (vid < 1023) {
      cout << dp[vid][l] << endl;
    }

    else {
      vector<int> wt;
      int tsize = 0, tpow = 1;
      int vidt = vid;
      while (vidt >= 1023) {
        vidt = (vidt - 1) / 2;
        tsize++;
        tpow *= 2;
      }

      ll ans = 0;
      rep(j, tpow) {
        vidt = vid;
        ll vt = 0;
        int wt = 0;
        rep(k, tsize) {
          if ((j >> k) & 1) {
            vt += v[vidt];
            wt += w[vidt];
          }
          vidt = (vidt - 1) / 2;
        }
        if (wt <= l) {
          ans = max(ans, vt + dp[vidt][l - wt]);
        }
      }
      cout << ans << endl;
    }
  }

  /*  rep(i, min(n, 511)) {
      rep(j, 7) cout << dp[i][j] << " ";
      cout << endl;
    }*/

  return 0;
}

/*
 */


Error: TLE

Fixed submission:

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

ll dp[511][100001];
int w[1000000];
int v[1000000];

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

  rep(i, 100001) { dp[0][i] = (i < w[0] ? 0 : v[0]); }

  for (int i = 1; i < min(n, 511); i++) {
    rep(j, 100001) {
      dp[i][j] = max(dp[(i - 1) / 2][j],
                     (j < w[i] ? 0 : dp[(i - 1) / 2][j - w[i]] + v[i]));
    }
  }

  cin >> q;
  vector<P> query(q);
  rep(i, q) { cin >> query[i].first >> query[i].second; }
  rep(i, q) {
    int vid, l;
    vid = query[i].first - 1;
    l = query[i].second;

    if (vid < 511) {
      cout << dp[vid][l] << endl;
    }

    else {
      vector<int> wt;
      int tsize = 0, tpow = 1;
      int vidt = vid;
      while (vidt >= 511) {
        vidt = (vidt - 1) / 2;
        tsize++;
        tpow *= 2;
      }

      ll ans = 0;
      rep(j, tpow) {
        vidt = vid;
        ll vt = 0;
        int wt = 0;
        rep(k, tsize) {
          if ((j >> k) & 1) {
            vt += v[vidt];
            wt += w[vidt];
          }
          vidt = (vidt - 1) / 2;
        }
        if (wt <= l) {
          ans = max(ans, vt + dp[vidt][l - wt]);
        }
      }
      cout << ans << endl;
    }
  }

  /*  rep(i, min(n, 511)) {
      rep(j, 7) cout << dp[i][j] << " ";
      cout << endl;
    }*/

  return 0;
}

/*
 */
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
struct S {
  int w, v;
};

void solve() {
  int n;
  cin >> n;
  dump(n);
  // vvl dp(n + 1, vl(100005));
  vvl dp(1025, vl(100005));
  vi vv(n + 1);
  vi vw(n + 1);
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    vv[i + 1] = v;
    vw[i + 1] = w;
    dump(i, v, w);
    int c = i + 1;
    int p = c / 2;
    if (1024 < c)
      continue;
    rep(j, 100001) {
      dp[c][j] = dp[p][j];
      if (0 <= j - w) {
        maxs(dp[c][j], dp[p][j - w] + v);
      }
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int v, l;
    cin >> v >> l;
    vector<S> s;
    s.push_back({0, 0});
    while (1024 < v) {
      int ns = s.size();
      rep(j, ns) {
        if (s[j].w + vw[v] <= l) {
          s.push_back({s[j].w + vw[v], s[j].v + vv[v]});
        }
      }
      v = v / 2;
    }
    ll ans = 0;
    for (auto c : s) {
      maxs(ans, dp[v][l - c.w] + c.v);
    }
    cout << ans << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

void solve() {
  int n;
  cin >> n;
  dump(n);
  // vvl dp(n + 1, vl(100005));
  vvl dp(1025, vl(100005));
  vi vv(n);
  vi vw(n);
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    vv[i] = v;
    vw[i] = w;
    dump(i, v, w);
    int c = i + 1;
    int p = c / 2;
    if (1024 < c)
      continue;
    rep(j, 100001) {
      dp[c][j] = dp[p][j];
      if (0 <= j - w) {
        maxs(dp[c][j], dp[p][j - w] + v);
      }
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int v, l;
    cin >> v >> l;
    vi u;
    while (1024 < v) {
      u.push_back(v);
      v = v / 2;
    }
    vl dp2(100005);
    rep(j, 100001) { dp2[j] = dp[v][j]; }
    if (u.size()) {
      reverse(u.begin(), u.end());
      for (auto c : u) {
        rep(j, 100001) {
          if (0 <= j - vw[c]) {
            maxs(dp2[j], dp2[j - vw[c]] + vv[c]);
          }
        }
      }
    }
    cout << dp2[l] << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  int value[N];
  int weight[N];
  for (int i = 0; i < N; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    value[i] = tmp1;
    weight[i] = tmp2;
  }
  int DP[512 + 1][100000 + 1];
  for (int i = 0; i < 512 + 1; i++) {
    for (int j = 0; j < 100000 + 1; j++) {
      DP[i][j] = 0;
    }
  }
  for (int i = 1; i < min(N, 512) + 1; i++) {
    int v, w;
    v = value[i - 1];
    w = weight[i - 1];
    for (int j = 100000; j > w - 1; j--) {
      DP[i][j] = max(DP[i / 2][j], DP[i / 2][j - w] + v);
    }
    for (int j = w - 1; j > -1; j--) {
      DP[i][j] = DP[i / 2][j];
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, l;
    cin >> v >> l;
    if (v <= 512) {
      cout << DP[v][l] << endl;
    } else {
      int vs[10];
      int cnt;
      cnt = 0;
      while (v > 512) {
        vs[cnt] = v;
        v = v / 2;
        cnt++;
      }
      int ans = 0;
      for (int i = 0; i < (1 << cnt); i++) {
        int wei = 0;
        int val = 0;
        for (int j = 0; j < cnt; j++) {
          if (i & (1 << j)) {
            wei += weight[vs[j] - 1];
            val += value[vs[j] - 1];
          }
        }
        if (wei > l) {
          continue;
        }
        ans = max(ans, DP[v][l - wei] + val);
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int value[N + 1];
  int weight[N + 1];
  for (int i = 1; i < N + 1; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    value[i] = tmp1;
    weight[i] = tmp2;
  }
  int DP[512 + 10][100000 + 10];
  for (int i = 0; i < 512 + 10; i++) {
    for (int j = 0; j < 100000 + 10; j++) {
      DP[i][j] = 0;
    }
  }
  for (int i = 1; i < min(N, 512) + 1; i++) {
    int v, w;
    v = value[i];
    w = weight[i];
    for (int j = 100000; j > w - 1; j--) {
      DP[i][j] = max(DP[i / 2][j], DP[i / 2][j - w] + v);
    }
    for (int j = w - 1; j > -1; j--) {
      DP[i][j] = DP[i / 2][j];
    }
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, l;
    cin >> v >> l;
    if (v <= 512) {
      cout << DP[v][l] << endl;
    } else {
      int vs[10];
      int cnt;
      cnt = 0;
      while (v > 512) {
        vs[cnt] = v;
        v = v / 2;
        cnt++;
      }
      int ans = 0;
      for (int i = 0; i < (1 << cnt); i++) {
        int w = 0;
        int v = 0;
        for (int j = 0; j < cnt; j++) {
          if (i & (1 << j)) {
            w += weight[vs[j]];
            v += value[vs[j]];
          }
        }
        if (w > l) {
          continue;
        }
        ans = max(ans, DP[v][l - w] + v);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 3e5 + 5, M = 1e5 + 5;
int n, q, dep[N], p[N], w[N], dp[512][M];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &p[i], &w[i]);
  for (int i = 1; i <= n && i < 512; i++)
    for (int j = 100000; j >= 0; j--) {
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + p[i]);
      dp[i][j] = max(dp[i][j], dp[i / 2][j]);
    }
  scanf("%d", &q);
  while (q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    vector<int> vc;
    while (v >= 512)
      vc.push_back(v), v >>= 1;
    int ans = dp[v][L];
    for (int i = 0; i < 1 << vc.size(); i++) {
      int wi = 0, pi = 0;
      for (int j = 0; j < vc.size(); j++)
        if (i >> j & 1) {
          wi += w[vc[j]];
          pi += p[vc[j]];
          if (wi > L)
            break;
        }
      if (wi <= L)
        ans = max(ans, dp[v][L - wi] + pi);
    }
    printf("%d\\n", ans);
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 3e5 + 5;
struct node {
  int w, p;
  double k;
  bool operator<(const node &o) const { return k > o.k; }
} a[N];
bool cmp(node a, node b) { return a.k < b.k; }
int n, m, f[23][23], g[23][23];
// f 最大价值，g最小重量
int nn, p[N], w[N];
int solve() {
  int ans = 0;
  memset(f, 0, sizeof(f));
  memset(g, inf, sizeof(g));
  f[0][0] = g[0][0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= i; j++) {
      if (j == 0)
        f[0][0] = g[0][0] = 0;
      else {
        if (f[i - 1][j] > f[i][j])
          f[i][j] = f[i - 1][j], g[i][j] = g[i - 1][j];
        else if (f[i - 1][j] == f[i][j])
          g[i][j] = min(g[i][j], g[i - 1][j]);
        if (g[i - 1][j - 1] + a[i].w <= m) {
          if (f[i - 1][j - 1] + a[i].p > f[i][j])
            f[i][j] = f[i - 1][j - 1] + a[i].p,
            g[i][j] = g[i - 1][j - 1] + a[i].w;
          else if (f[i - 1][j - 1] + a[i].p == f[i][j])
            g[i][j] = min(g[i][j], g[i - 1][j - 1] + a[i].w);
        }
      }
      ans = max(ans, f[i][j]);
    }
  return ans;
}
int main() {
  scanf("%d", &nn);
  for (int i = 1; i <= nn; i++)
    scanf("%d%d", &p[i], &w[i]);
  int q;
  scanf("%d", &q);
  while (q--) {
    int v;
    scanf("%d%d", &v, &m);
    n = 0;
    while (v)
      ++n, a[n].w = w[v], a[n].p = p[v], a[n].k = 1.0 * a[n].p / a[n].w,
           v >>= 1;
    sort(a + 1, a + 1 + n);
    int ans = solve();
    sort(a + 1, a + 1 + n, cmp);
    ans = max(ans, solve());
    for (int i = 1; i <= 15; i++) {
      for (int j = 1; j <= 1000; j++) {
        int x = rand() % n + 1, y = rand() % n + 1;
        swap(a[x], a[y]);
      }
      ans = max(ans, solve());
    }
    printf("%d\\n", ans);
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int dp[513][100001], v[(1 << 18) + 1], w[(1 << 18) + 1], n, a, b, q;
int f(int x, int y) {
  if (y < 0)
    return -1e9;
  if (x == 0)
    return 0;
  if (x < 513) {
    if (dp[x][y] != -1)
      return dp[x][y];
    return dp[x][y] =
               max({f(x, y - 1), f(x / 2, y - w[x]) + v[x], f(x / 2, y)});
  }
  return max(f(x / 2, y - w[x]) + v[x], f(x / 2, y));
}
int main(void) {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++)
    cin >> v[i + 1] >> w[i + 1];
  cin >> q;
  while (q--) {
    cin >> a >> b;
    cout << f(a, b) << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[1025][100001], v[(1 << 20)], w[(1 << 20)], n, a, b, q;
int f(int x, int y) {
  if (y < 0)
    return -1e9;
  if (x == 0)
    return 0;
  if (dp[x][y] != -1)
    return dp[x][y];
  if (x < 1025)
    return dp[x][y] =
               max({f(x, y - 1), f(x / 2, y - w[x]) + v[x], f(x / 2, y)});
  return max(f(x / 2, y - w[x]) + v[x], f(x / 2, y));
}
int main(void) {
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++)
    cin >> v[i + 1] >> w[i + 1];
  cin >> q;
  while (q--) {
    cin >> a >> b;
    cout << f(a, b) << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <unordered_map>
#define re register
using namespace std;
inline int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0')
    v = getchar();
  while (v >= '0')
    t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
int n, a[300002], b[300002], q, suma[300002], sumb[300002], mn[300002],
    p[513][100002];
inline int ask(re int x, re int y) {
  if (x <= 512)
    return p[x][y];
  if (y < mn[x])
    return 0;
  if (y > sumb[x])
    return suma[x];
  if (x == 1)
    return (y >= b[x]) * a[x];
  if (y >= b[x])
    return max(ask(x >> 1, y), a[x] + ask(x >> 1, y - b[x]));
  else
    return ask(x >> 1, y);
}
signed main() {
  n = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = read(), b[i] = read();
  mn[0] = 1e9;
  for (re int i = 1; i <= n; ++i)
    suma[i] = suma[i >> 1] + a[i], sumb[i] = sumb[i >> 1] + b[i],
    mn[i] = min(b[i], mn[i >> 1]);
  for (re int i = 1; i <= min(n, 512); ++i)
    for (re int j = 100000; j; --j)
      if (j >= b[i])
        p[i][j] = max(p[i >> 1][j], p[i >> 1][j - b[i]] + a[i]);
      else
        p[i][j] = p[i >> 1][j];
  q = read();
  while (q--) {
    re int x = read(), y = read();
    printf("%d\\n", ask(x, y));
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <unordered_map>
#define re register
using namespace std;
inline int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0')
    v = getchar();
  while (v >= '0')
    t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
int n, a[300002], b[300002], q, suma[300002], sumb[300002], mn[300002];
inline int ask(re int x, re int y) {
  if (y < mn[x])
    return 0;
  if (y > sumb[x])
    return suma[x];
  if (x == 1)
    return (y >= b[x]) * a[x];
  if (y >= b[x])
    return max(ask(x >> 1, y), a[x] + ask(x >> 1, y - b[x]));
  else
    return ask(x >> 1, y);
}
signed main() {
  n = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = read(), b[i] = read();
  mn[0] = 1e9;
  for (re int i = 1; i <= n; ++i)
    suma[i] = suma[i >> 1] + a[i], sumb[i] = sumb[i >> 1] + b[i],
    mn[i] = min(b[i], mn[i >> 1]);
  q = read();
  while (q--) {
    re int x = read(), y = read();
    printf("%d\\n", ask(x, y));
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
/*
2^18=262144
価値の高いものから選んでいくか
L=20

20 20
15 11
14 9
とかで失敗する

1
23
4567
89012345
2^18=2^9^2=512*512
DP２つ用意すればいける?
512まで先に用意しとく?
2^9
5000*100000とか
限界？
256くらいなら
2000*100000くらい
しといて
１発で出せるようにするとか

0 -1 -1 2 -1 -1
0  0  0 2  2  2
mapだとTLE
普通の配列に１０００個くらいいれる場合
bit全探索？
*/
vector<ll> V(263000, 0), W(263000, 0);
vector<vector<ll>> DP(520, vector<ll>(100010, 0));
ll N;

void dfs(ll v) {
  for (ll i = 0; i <= 100000; i++) {
    DP[v][i] = DP[v / 2][i];
    if (i - W[v] >= 0)
      DP[v][i] = max(DP[v][i], DP[v / 2][i - W[v]] + V[v]);
  }
}

int main() {
  cin >> N;
  for (ll i = 1; i <= N; i++)
    cin >> V[i] >> W[i];
  ll Q;
  cin >> Q;
  // DP用意(511以下
  ll n = min(511LL, N);
  for (ll i = 0; i <= n; i++)
    DP[i][0] = 0;
  for (ll i = 1; i <= n; i++)
    dfs(i);
  for (ll i = 0; i < Q; i++) {
    ll v, L;
    cin >> v >> L;
    ll vv = v, ans = 0;
    vector<pair<ll, ll>> C(0, {0, 0});
    while (vv > 511) {
      C.emplace_back(W[vv], V[vv]);
      vv /= 2;
    }
    ll csz = C.size();
    ll n = 1 << csz;
    for (ll j = 0; j < n; j++) {
      ll sw = 0, sv = 0, m = j;
      for (ll k = 0; k < csz; k++) {
        if (m >> k & 1) {
          sw += C[k].first;
          sv += C[k].second;
        }
      }
      if (sw > L)
        continue;
      ans = max(ans, sv + DP[vv][L - sw]);
    }
    if (v < 512)
      ans = DP[v][L];
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
/*
2^18=262144
価値の高いものから選んでいくか
L=20

20 20
15 11
14 9
とかで失敗する

1
23
4567
89012345
2^18=2^9^2=512*512
DP２つ用意すればいける?
512まで先に用意しとく?
2^9
5000*100000とか
限界？
256くらいなら
2000*100000くらい
しといて
１発で出せるようにするとか

0 -1 -1 2 -1 -1
0  0  0 2  2  2
mapだとTLE
普通の配列に１０００個くらいいれる場合

*/
vector<ll> V(263000, 0), W(263000, 0);
vector<vector<ll>> DP(520, vector<ll>(100010, 0));
ll N;

void dfs(ll v) {
  for (ll i = 0; i <= 100000; i++) {
    DP[v][i] = DP[v / 2][i];
    if (i - W[v] >= 0)
      DP[v][i] = max(DP[v][i], DP[v / 2][i - W[v]] + V[v]);
  }
}

int main() {
  cin >> N;
  for (ll i = 1; i <= N; i++)
    cin >> V[i] >> W[i];
  ll Q;
  cin >> Q;
  // DP用意(511以下
  ll n = min(511LL, N);
  for (ll i = 0; i <= n; i++)
    DP[i][0] = 0;
  for (ll i = 1; i <= n; i++)
    dfs(i);
  for (ll i = 0; i < Q; i++) {
    ll v, L;
    cin >> v >> L;
    ll vv = v, ans = 0;
    vector<ll> DP2(100010, 0), C; // 重さだけ格納
    C.emplace_back(0);
    while (vv > 511) {
      ll csz = C.size();
      for (ll j = csz - 1; j >= 0; j--) {
        if (C[j] + W[vv] <= L) {
          if (DP2[C[j] + W[vv]] == 0)
            C.emplace_back(j);
          DP2[C[j] + W[vv]] = max(DP2[C[j] + W[vv]], DP2[C[j]] + V[vv]);
        }
      }
      vv /= 2;
    }
    ll csz = C.size();
    for (ll j = 0; j < csz; j++)
      ans = max(ans, DP2[C[j]] + DP[vv][L - C[j]]);
    cout << ans << endl;
  }
}