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

#include <bits/stdc++.h>

using namespace std;

#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define endl '\\n'
#define f first
#define s second

typedef long long ll;

const ll INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const ll MAX = 3e5 + 10;

ll n, memo[19][MAX];
int v[MAX], w[MAX], vans[MAX];

vector<vector<pair<ll, ll>>> vq(MAX);

vector<vector<int>> erase(20);

int log(int x) { return 32 - __builtin_clz(x) - 1; }

ll dp(int x, int L) {
  int pos = log(x);
  if (L < 0)
    return -LINF;
  if (!x)
    return 0;
  ll &p = memo[pos][L];
  erase[pos].push_back(L);
  if (p != -1)
    return p;

  return p = max(v[x] + dp(x / 2, L - w[x]), dp(x / 2, L));
}

void dfs(int x) {
  int pos = log(x);
  for (auto u : erase[pos])
    memo[pos][u] = -1;
  erase[pos].clear();

  for (auto u : vq[x])
    vans[u.s] = dp(x, u.f);

  if (2 * x <= n)
    dfs(2 * x);
  if (2 * x + 1 <= n)
    dfs(2 * x + 1);
}

int main() {
  _

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

  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll vi, L;
    cin >> vi >> L;

    vq[vi].push_back({L, i});
  }

  memset(memo, -1, sizeof memo);
  dfs(1);

  for (ll i = 0; i < q; i++)
    cout << vans[i] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define endl '\\n'
#define f first
#define s second

typedef long long ll;

const ll INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

const ll MAX = 3e5 + 10;

ll n, memo[19][MAX];
int v[MAX], w[MAX], vans[MAX];

vector<vector<pair<ll, ll>>> vq(MAX);

ll dp(int x, int L) {
  int pos = log2(x);
  if (L < 0)
    return -LINF;
  if (!x)
    return 0;
  ll &p = memo[pos][L];
  if (p != -1)
    return p;

  return p = max(v[x] + dp(x / 2, L - w[x]), dp(x / 2, L));
}

void dfs(int x = 1) {
  int pos = log2(x);
  for (auto u : vq[x])
    vans[u.s] = dp(x, u.f);

  if (2 * x <= n)
    dfs(2 * x);
  if (2 * x + 1 <= n)
    dfs(2 * x + 1);

  memset(memo[pos], -1, sizeof memo[pos]);
}

int main() {
  _

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

  ll q;
  cin >> q;
  for (ll i = 0; i < q; i++) {
    ll vi, L;
    cin >> vi >> L;

    vq[vi].push_back({L, i});
  }

  memset(memo, -1, sizeof memo);
  dfs();

  for (ll i = 0; i < q; i++)
    cout << vans[i] << endl;

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

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

// #define int long long

const int N = 18;

int v[1 << N], w[1 << N];
long long dp[1 << 10][100001];

int32_t main() {
  ios_base ::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];
    if (i >= 1024) {
      continue;
    }
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
      }
    }
  }

  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int vv, l;
    cin >> vv >> l;
    if (vv < 1024) {
      cout << dp[vv][l] << "\\n";
    } else {
      vector<int> idx;
      int u = vv;
      while (u >= 1024) {
        idx.push_back(u);
        u /= 2;
      }
      long long ans = 0;
      for (int j = 0; j < (1 << (int)idx.size()); j++) {
        long long ww = 0, vvv = 0;
        for (int k = 0; (1 << k) <= j; k++)
          if ((j >> k) & 1) {
            ww += w[idx[k]];
            vvv += v[idx[k]];
          }
        if (ww <= l) {
          ans = max(ans, dp[u][l - ww] + vvv);
        }
      }
      cout << ans << "\\n";
    }
  }
}


Error: TLE

Fixed submission:

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

// #define int long long

const int N = 18;

int v[1 << N], w[1 << N];
long long dp[513][100001];

long long f(int idx, int lft) {
  if (!idx) {
    return 0;
  }
  if (idx <= 512 && ~dp[idx][lft]) {
    return dp[idx][lft];
  }
  long long res = f(idx / 2, lft);
  if (lft >= w[idx]) {
    res = max(res, f(idx / 2, lft - w[idx]) + v[idx]);
  }
  if (idx <= 512) {
    dp[idx][lft] = res;
  }
  return res;
}

int32_t main() {
  ios_base ::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];
  }
  memset(dp, -1, sizeof dp);
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int u, l;
    cin >> u >> l;
    cout << f(u, l) << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

constexpr int C = 9;

int N, V[(1 << 18) + 1], W[(1 << 18) + 1], Q;
vector<pair<int, int>> WV[(1 << C) + 1];

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  WV[1].push_back({0, 0});
  WV[1].push_back({W[1], V[1]});
  for (int i = 2; i < (1 << C); i++) {
    WV[i] = WV[i / 2];
    for (auto p : WV[i / 2]) {
      WV[i].push_back({p.first + W[i], p.second + V[i]});
    }
    sort(WV[i].begin(), WV[i].end());
    for (int j = 0; j < WV[i].size() - 1; j++) {
      WV[i][j + 1].second = max(WV[i][j].second, WV[i][j + 1].second);
    }
  }
  cin >> Q;
  while (Q--) {
    int v, L;
    cin >> v >> L;
    vector<pair<int, int>> vp;
    vp.push_back({0, 0});
    while (v >= (1 << C)) {
      for (int i = vp.size() - 1; i >= 0; i--) {
        vp.push_back({vp[i].first + W[v], vp[i].second + V[v]});
      }
      v /= 2;
    }
    int res = 0;
    for (auto a : vp) {
      if (a.first > L)
        continue;
      auto it = lower_bound(WV[v].begin(), WV[v].end(),
                            pair<int, int>{L - a.first + 1, -1});
      it--;
      res = max(res, a.second + it->second);
    }
    cout << res << endl;
  }
}


Error: TLE

Fixed submission:

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

int N, V[(1 << 18) + 1], W[(1 << 18) + 1], Q;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> Q;
  while (Q--) {
    int t, L;
    cin >> t >> L;
    vector<int> v, w;
    bool f = true;
    while (t) {
      if (f)
        v.push_back(t);
      else
        w.push_back(t);
      f ^= true;
      t /= 2;
    }
    vector<pair<int, int>> A, B;
    for (int i = 0; i < (1 << v.size()); i++) {
      int WW, VV;
      WW = 0;
      VV = 0;
      for (int j = 0; j < v.size(); j++) {
        if (i & (1 << j)) {
          WW += W[v[j]];
          VV += V[v[j]];
        }
      }
      A.push_back({WW, VV});
    }
    A.push_back({0, 0});
    B.push_back({0, 0});
    for (int i = 0; i < (1 << w.size()); i++) {
      int WW, VV;
      WW = VV = 0;
      for (int j = 0; j < w.size(); j++) {
        if (i & (1 << j)) {
          WW += W[w[j]];
          VV += V[w[j]];
        }
      }
      B.push_back({WW, VV});
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    for (int i = 0; i < A.size() - 1; i++) {
      A[i + 1].second = max(A[i].second, A[i + 1].second);
    }
    int res = 0;
    for (pair<int, int> p : B) {
      if (p.first > L)
        continue;
      auto it =
          lower_bound(A.begin(), A.end(), pair<int, int>{L - p.first + 1, -1});
      it--;
      res = max(res, p.second + it->second);
    }
    cout << res << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define eb emplace_back
#define fi first
#define se second

using namespace std;

const int nm = (1 << 18) + 2;
const int mm = 100002;
const int nm2 = (1 << 9) + 2;

int n, w[nm], v[nm];
int f[nm2][mm];
int m;

int solve(int u, int l) {
  if (u < nm2) {
    return f[u][l];
  }
  int res = solve(u >> 1, l);
  if (w[u] <= l) {
    res = max(res, solve(u >> 1, l - w[u]) + v[u]);
  }
  return res;
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &v[i], &w[i]);
  }
  for (int i = 1; i < nm2 && i <= n; ++i) {
    for (int j = 1; j < mm; ++j) {
      f[i][j] = f[i >> 1][j];
      if (w[i] <= j) {
        f[i][j] = max(f[i][j], f[i >> 1][j - w[i]] + v[i]);
      }
    }
  }
  scanf("%d", &m);
  while (m--) {
    int u, l;
    scanf("%d%d", &u, &l);
    printf("%d\\n", solve(u, l));
  }
#ifdef LOCAL
  cout << setprecision(6) << fixed << (double)clock() / CLOCKS_PER_SEC << "\\n";
#endif
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define eb emplace_back
#define fi first
#define se second

using namespace std;

const int nm = (1 << 18) + 2;

int n, m;
int v[nm], w[nm];
int na, a[nm];
int nb;
pair<int, int> b[nm];
int res;
int s[nm];

void doit(int i, int sv, int sw, int l) {
  if (i > na / 2) {
    b[++nb] = {sw, sv};
    return;
  }
  doit(i + 1, sv, sw, l);
  if (sw + w[a[i]] <= l) {
    doit(i + 1, sv + v[a[i]], sw + w[a[i]], l);
  }
}

void doit2(int i, int sv, int sw, int l, int u) {
  while (u > 0 && b[u].fi + sw > l) {
    u--;
  }
  if (i > na) {
    res = max(res, b[u].se + sv);
    return;
  }
  if (sv + s[i] + b[u].se <= res) {
    return;
  }
  if (sv + s[i + 1] + b[u].se > res) {
    doit2(i + 1, sv, sw, l, u);
  }
  if (sw + w[a[i]] <= l) {
    doit2(i + 1, sv + v[a[i]], sw + w[a[i]], l, u);
  }
}

bool cmp(int i, int j) { return (ll)v[i] * w[j] > (ll)v[j] * w[i]; }

int calc(int vv, int l) {
  na = 0;
  while (vv) {
    if (w[vv] <= l) {
      a[++na] = vv;
    }
    vv >>= 1;
  }
  sort(a + 1, a + na + 1, cmp);
  s[na + 1] = 0;
  for (int i = na; i >= 1; --i) {
    s[i] = s[i + 1] + v[a[i]];
  }
  nb = 0;
  doit(1, 0, 0, l);
  sort(b + 1, b + nb + 1);
  for (int i = 1; i <= nb; ++i) {
    b[i].se = max(b[i - 1].se, b[i].se);
  }
  res = 0;
  doit2(na / 2 + 1, 0, 0, l, nb);
  return res;
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &v[i], &w[i]);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    int v, l;
    scanf("%d%d", &v, &l);
    printf("%d\\n", calc(v, l));
  }
#ifdef LOCAL
  cout << setprecision(6) << fixed << (double)clock() / CLOCKS_PER_SEC << "\\n";
#endif
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int n, q;
long long dp[1028][100009];
ll v[300009];
ll w[300009];

int limit = 1024;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < limit; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
      }
    }
  }
  cin >> q;
  for (int tt = 1; tt <= q; tt++) {
    int s, L;
    cin >> s >> L;
    if (s < 512) {
      cout << dp[s][L] << '\\n';
      continue;
    }
    ll best = 0;
    int extra = 0;
    int u = s;
    while (u >= limit) {
      extra++;
      u /= 2;
    }
    for (int msk = 0; msk < (1 << extra); msk++) {
      ll wsum = 0;
      ll vsum = 0;
      u = s;
      int mm = msk;
      for (int d = 0; d < extra; d++) {
        if (mm % 2) {
          vsum += v[u];
          wsum += w[u];
        }
        mm /= 2;
        u /= 2;
      }
      if (wsum > L)
        continue;
      best = max(best, vsum + dp[u][L - wsum]);
    }
    cout << best << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;

int n, q;
ll dp[529][100009];
ll v[300009];
ll w[300009];

int limit = 512;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < limit; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
      }
    }
  }
  cin >> q;
  for (int tt = 1; tt <= q; tt++) {
    int s, L;
    cin >> s >> L;
    if (s < 512) {
      cout << dp[s][L] << '\\n';
      continue;
    }
    ll best = 0;
    int extra = 0;
    int u = s;
    while (u >= limit) {
      extra++;
      u /= 2;
    }
    for (int msk = 0; msk < (1 << extra); msk++) {
      ll wsum = 0;
      ll vsum = 0;
      u = s;
      int mm = msk;
      for (int d = 0; d < extra; d++) {
        if (mm % 2) {
          vsum += v[u];
          wsum += w[u];
        }
        mm /= 2;
        u /= 2;
      }
      if (wsum > L)
        continue;
      best = max(best, vsum + dp[u][L - wsum]);
    }
    cout << best << '\\n';
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
const int MX = 1e5 + 5;
vector<vector<int>> dp(512, vector<int>(MX));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n, q;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i];
  for (int i = 1; i < 512; ++i) {
    for (int j = 1e5; j >= w[i]; --j) {
      dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
    }
    for (int j = w[i] - 1; j >= 0; --j) {
      dp[i][j] = dp[i / 2][j];
    }
  }

  cin >> q;
  rep(_, q) {
    int u, l;
    cin >> u >> l;
    vector<int> ids;
    while (u >= 512) {
      ids.push_back(u);
      u >>= 1;
    }
    int ans = 0;
    int sz = ids.size();
    rep(t, (1 << sz)) {
      int sv = 0;
      int sw = 0;
      rep(i, sz) {
        if (t & (1 << i)) {
          sv += v[ids[i]];
          sw += w[ids[i]];
        }
      }
      if (sw <= l) {
        ans = max(ans, sv + dp[u][l - sw]);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
const int MX = 1e5 + 5;
vector<vector<int>> dp(512, vector<int>(MX));

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n, q;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> v[i] >> w[i];
  for (int i = 1; i < 512; ++i) {
    for (int j = 1e5; j >= w[i]; --j) {
      dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
    }
    for (int j = w[i] - 1; j >= 0; --j) {
      dp[i][j] = dp[i / 2][j];
    }
  }

  cin >> q;
  rep(_, q) {
    int u, l;
    cin >> u >> l;
    vector<int> ids;
    while (u >= 256) {
      ids.push_back(u);
      u >>= 1;
    }
    int ans = 0;
    int sz = ids.size();
    rep(t, (1 << sz)) {
      int sv = 0;
      int sw = 0;
      rep(i, sz) {
        if (t & (1 << i)) {
          sv += v[ids[i]];
          sw += w[ids[i]];
        }
      }
      if (sw <= l) {
        ans = max(ans, sv + dp[u][l - sw]);
      }
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int n, q, v[300005], w[300005];
int dp[1 << 9][100005], p[20], r[20];
int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int m = min(n, (1 << 9) - 1);
  for (int i = w[0]; i < 100001; i++) {
    dp[0][i] = v[0];
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < 100001; j++) {
      if (j < w[i])
        dp[i][j] = dp[(i - 1) / 2][j];
      else
        dp[i][j] = max(dp[(i - 1) / 2][j], dp[(i - 1) / 2][j - w[i]] + v[i]);
    }
  }
  cin >> q;
  while (q--) {
    int x, l, s = 0, ans = 0;
    cin >> x >> l;
    x--;
    while (x >= m) {
      p[s] = v[x];
      r[s] = w[x];
      s++;
      x = (x - 1) / 2;
    }
    for (int i = 0; i < 1 << s; i++) {
      int t = l, u = 0;
      for (int j = 0; j < s; j++) {
        if (i >> j & 1) {
          t -= r[j];
          u += p[j];
        }
        if (t < 0)
          break;
      }
      if (t >= 0) {
        ans = max(ans, u + dp[x][t]);
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll n;
ll V[300005], W[300005];
ll q;
ll v, l;
ll a[20], b[20];
ll c;
P ps[1 << 9];
ll INF = 10000000000000000;
int main(void) {
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> V[i] >> W[i];
  }
  cin >> q;
  while (q > 0) {
    q--;
    cin >> v >> l;
    ll vv = v;
    c = 0;
    while (vv > 0) {
      vv = vv / 2;
      c++;
    }
    vv = v;
    for (ll i = 0; i < c; i++) {
      a[i] = V[vv];
      b[i] = W[vv];
      // cout<<vv<<" "<<a[i]<<" "<<b[i]<<endl;
      vv = vv / 2;
    }
    ll n2 = c / 2;
    for (ll i = 0; i < 1 << n2; i++) {
      ll sw = 0, sv = 0;
      for (ll j = 0; j < n2; j++) {
        if (i >> j & 1) {
          sw += b[j];
          sv += a[j];
        }
      }
      ps[i] = make_pair(sw, sv);
    }
    sort(ps, ps + (1 << n2));
    ll m = 1;
    for (ll i = 1; i < 1 << n2; i++) {
      if (ps[m - 1].second < ps[i].second) {
        ps[m++] = ps[i];
      }
    }
    ll res = 0;
    for (ll i = 0; i < 1 << (c - n2); i++) {
      ll sw = 0, sv = 0;
      for (ll j = 0; j < c - n2; j++) {
        if (i >> j & 1) {
          sw += b[j + n2];
          sv += a[j + n2];
        }
      }
      if (sw <= l) {
        ll tv = (lower_bound(ps, ps + m, make_pair(l - sw, INF)) - 1)->second;
        res = max(res, sv + tv);
      }
    }
    cout << res << endl;
  }
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef int LL;
typedef double dl;
#define opt operator
#define pb push_back
const LL maxn = 1e5 + 9, mod = 998244353, up = 1e5;
LL Read() {
  LL x(0), f(1);
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3ll) + (x << 1ll) + c - '0';
    c = getchar();
  }
  return x * f;
}
LL L, n, q;
LL a[1 << 9][maxn], v[maxn << 3], w[maxn << 3];
void chkmax(LL &x, LL y) { x = std::max(x, y); }
LL Query(LL x, LL l) {
  if (x <= L)
    return a[x][l];
  else {
    LL ret(Query(x / 2, l));
    if (l >= w[x])
      chkmax(ret, Query(x / 2, l - w[x]) + v[x]);
    return ret;
  }
}
int main() {
  n = Read();
  L = (1 << 9) - 1;
  for (LL i = 1; i <= n; ++i) {
    v[i] = Read();
    w[i] = Read();
  }
  for (LL i = 1; i <= L; ++i) {
    LL f(i / 2);
    for (LL j = 1; j <= up; ++j) {
      a[i][j] = a[f][j];
      if (j >= w[i])
        chkmax(a[i][j], a[f][j - w[i]] + v[i]);
    }
  }
  q = Read();
  while (q--) {
    LL x(Read()), l(Read());
    printf("%d\\n", Query(x, l));
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
typedef int LL;
typedef double dl;
#define opt operator
#define pb push_back
const LL maxn = 1e5 + 9, mod = 998244353, up = 1e5;
LL Read() {
  LL x(0), f(1);
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3ll) + (x << 1ll) + c - '0';
    c = getchar();
  }
  return x * f;
}
LL L, n, q;
LL a[501][maxn], v[maxn << 3], w[maxn << 3];
void chkmax(LL &x, LL y) { x = std::max(x, y); }
LL Query(LL x, LL l) {
  if (x <= L)
    return a[x][l];
  else {
    LL ret(0);
    if (l >= w[x])
      ret = Query(x / 2, l - w[x]) + v[x];
    chkmax(ret, Query(x / 2, l));
  }
}
int main() {
  n = Read();
  L = (1 << 9) - 1;
  for (LL i = 1; i <= n; ++i) {
    v[i] = Read();
    w[i] = Read();
  }
  for (LL i = 1; i <= L; ++i) {
    LL f(i / 2);
    for (LL j = 1; j <= up; ++j) {
      a[i][j] = a[f][j];
      if (j >= w[i])
        chkmax(a[i][j], a[f][j - w[i]] + v[i]);
    }
  }
  q = Read();
  while (q--) {
    LL x(Read()), l(Read());
    printf("%d\\n", Query(x, l));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
int n, q, v[1 << 18], w[1 << 18];
ll f[1024][100010];

int main() {
  scanf("%d", &n);
  int K = 10;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &v[i], &w[i]);
    if (log2(i) >= K)
      continue;
    memcpy(f[i], f[i >> 1], sizeof(f[i]));
    for (int j = 100000; j >= w[i]; j--) {
      f[i][j] = max(f[i][j], f[i][j - w[i]] + v[i]);
    }
  }
  scanf("%d", &q);
  for (int i = 1, u, L; i <= q; i++) {
    scanf("%d %d", &u, &L);
    vector<int> V;
    while (log2(u) >= K) {
      V.push_back(u), u >>= 1;
    }
    ll ans = 0;
    for (int j = 0; j < 1 << V.size(); j++) {
      ll s1 = 0, s2 = 0;
      for (int k = 0; k < V.size(); k++) {
        if (j >> k & 1)
          s1 += w[V[k]], s2 += v[V[k]];
      }
      if (s1 <= L)
        ans = max(ans, s2 + f[u][L - s1]);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
int n, q, v[1 << 18], w[1 << 18];
ll f[600][100010];

int main() {
  scanf("%d", &n);
  int K = log2(n) / 2;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &v[i], &w[i]);
    if (log2(i) >= K)
      continue;
    memcpy(f[i], f[i >> 1], sizeof(f[i]));
    for (int j = 100000; j >= w[i]; j--) {
      f[i][j] = max(f[i][j], f[i][j - w[i]] + v[i]);
    }
  }
  scanf("%d", &q);
  for (int i = 1, u, L; i <= q; i++) {
    scanf("%d %d", &u, &L);
    vector<int> V;
    while (log2(u) >= K) {
      V.push_back(u), u >>= 1;
    }
    ll ans = 0;
    for (int j = 0; j < 1 << V.size(); j++) {
      ll s1 = 0, s2 = 0;
      for (int k = 0; k < V.size(); k++) {
        if (j >> k & 1)
          s1 += w[V[k]], s2 += v[V[k]];
      }
      if (s1 <= L)
        ans = max(ans, s2 + f[u][L - s1]);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 300010, M = 100010;
int n, m, q, ans = 0;
int v[N], w[N], d[N], f[1030][M];
inline void dfs1(int x) {
  d[x] = d[x / 2] + 1;
  for (int k = 0; k <= 1e5; k++)
    f[x][k] = f[x / 2][k];
  for (int k = w[x]; k <= 1e5; k++)
    f[x][k] = max(f[x][k], f[x / 2][k - w[x]] + v[x]);
  if (d[x] == m)
    return;
  dfs1(x << 1);
  dfs1(x << 1 | 1);
  return;
}
inline void dfs2(int x, int y, int z) {
  if (d[x] == m) {
    ans = max(ans, z + f[x][y]);
    return;
  }
  if (y >= w[x])
    dfs2(x / 2, y - w[x], z + v[x]);
  dfs2(x / 2, y, z);
  return;
}
int main() {
  scanf("%d", &n);
  m = log2(n) / 2;
  if (m == 0)
    m = 1;
  for (int k = 1; k <= n; k++)
    scanf("%d%d", &v[k], &w[k]);
  dfs1(1);
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ans = 0;
    if (d[x])
      printf("%d\\n", f[x][y]);
    else
      dfs2(x, y, 0ll), printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 300010, M = 100010;
int n, m, q;
int v[N], w[N], d[N];
long long ans = 0ll;
long long f[1030][M];
inline void dfs1(int x) {
  d[x] = d[x / 2] + 1;
  for (int k = 0; k <= 1e5; k++)
    f[x][k] = f[x / 2][k];
  for (int k = w[x]; k <= 1e5; k++)
    f[x][k] = max(f[x][k], f[x / 2][k - w[x]] + v[x]);
  if (d[x] == m)
    return;
  dfs1(x << 1);
  dfs1(x << 1 | 1);
  return;
}
inline void dfs2(int x, int y, long long z) {
  if (d[x] == m) {
    ans = max(ans, z + f[x][y]);
    return;
  }
  if (y >= w[x])
    dfs2(x / 2, y - w[x], z + v[x]);
  dfs2(x / 2, y, z);
  return;
}
int main() {
  scanf("%d", &n);
  m = log2(n) / 2;
  if (m == 0)
    m = 1;
  for (int k = 1; k <= n; k++)
    scanf("%d%d", &v[k], &w[k]);
  dfs1(1);
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    ans = 0ll;
    if (d[x])
      printf("%lld\\n", f[x][y]);
    else
      dfs2(x, y, 0ll), printf("%lld\\n", ans);
  }
  return 0;
}