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

#include <bits/stdc++.h>

using namespace std;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i < N + 1; i++) {
    cin >> V[i] >> W[i];
  }
  vector<vector<int>> dp(min(N, 1 << 10) + 1, vector<int>(100001, 0));
  for (int i = 1; i < min(N, 1 << 10) + 1; i++) {
    int x = i / 2;
    for (int j = 0; j < 100001; j++) {
      dp[i][j] = dp[x][j];
      if (j - W[i] >= 0) {
        chmax(dp[i][j], dp[x][j - W[i]] + V[i]);
      }
    }
  }

  function<int(int, int)> rec = [&](int v, int l) {
    if (v < (1 << 10) + 1)
      return dp[v][l];
    int res = rec(v / 2, l);
    if (l - W[v] >= 0)
      chmax(res, rec(v / 2, l - W[v]) + V[v]);
    return res;
  };

  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int v, l;
    cin >> v >> l;
    cout << rec(v, l) << '\\n';
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> V(N + 1), W(N + 1);
  for (int i = 1; i < N + 1; i++) {
    cin >> V[i] >> W[i];
  }
  int maxl = 100000;
  vector<vector<int>> dp(min(N + 10, 1 << 17), vector<int>(maxl + 10, 0));
  int base = max(1, N - (1 << 17));
  for (int i = base; i < N + 1; i++) {
    int x = i, idx = 1;
    vector<vector<int>> dist(100, vector<int>(maxl + 10, 0));
    while (x > 0) {
      for (int w = 0; w < maxl + 1; w++) {
        chmax(dist[idx][w], dist[idx - 1][w]);
        if (w - W[x] >= 0)
          chmax(dist[idx][w], dist[idx - 1][w - W[x]] + V[x]);
      }
      x /= 2;
      idx++;
    }
    for (int j = 1; j < maxl + 1; j++) {
      dp[i - base][j] = dist[idx - 1][j];
    }
  }
  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int x, l;
    cin >> x >> l;
    if (x - base >= 0) {
      cout << dp[x - base][l] << '\\n';
    } else {
      int res = 0;
      vector<int> dp(l + 1, 0); // v
      queue<int> que;
      que.push(0); // v, w;
      while (x > 0) {
        int n = (int)que.size();
        for (int i = 0; i < n; i++) {
          auto q = que.front();
          que.pop();
          if (q + W[x] <= l) {
            chmax(dp[q + W[x]], dp[q] + V[x]);
            que.push(q + W[x]);
            res = max(res, dp[q + W[x]]);
          }
          que.push(q);
        }
        x /= 2;
      }
      cout << res << '\\n';
    }
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
long long knap[1024][100001];
int main() {
  int N;
  scanf("%d", &N);
  long long V[1000000], W[1000000];
  for (int i = 1; i <= N; i++) {
    scanf("%lld%lld", &V[i], &W[i]);
  }
  for (int i = 0; i <= min(1023, N); i++) {
    int k = i / 2;
    for (int j = 0; j <= 100000; j++) {
      if (j == 0)
        knap[i][j] = 0;
      else if (i == 0) {
        knap[i][j] = 0;
      } else {
        if (j - W[i] >= 0 && knap[k][j] < knap[k][j - W[i]] + V[i])
          knap[i][j] = knap[k][j - W[i]] + V[i];
        else
          knap[i][j] = knap[k][j];
      }
    }
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int v, L;
    scanf("%d%d", &v, &L);
    if (v >= 1024) {
      long long k = 1;
      int u[10];
      u[0] = v;
      int t = 0;
      while (u[t] >= 1024) {
        k = k * 2;
        u[t + 1] = u[t] / 2;
        t++;
      }
      long long answer = 0;
      for (int a = 0; a < k; a++) {
        long long val = 0;
        long long wei = 0;
        int K = 1;
        int J = 0;
        while (J < t) {
          if ((a / K) % 2 == 1) {
            val += V[u[J]];
            wei += W[u[J]];
          }
          J++;
          K = K * 2;
        }
        if (wei <= L && answer < val + knap[u[t]][L - wei])
          answer = val + knap[u[t]][L - wei];
      }
      printf("%lld\\n", answer);
    } else
      printf("%lld\\n", knap[v][L]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
long long knap[512][100001];
int main() {
  int N;
  scanf("%d", &N);
  long long V[1000000], W[1000000];
  for (int i = 1; i <= N; i++) {
    scanf("%lld%lld", &V[i], &W[i]);
  }
  for (int i = 0; i <= min(511, N); i++) {
    for (int j = 0; j <= 100000; j++) {
      if (j == 0)
        knap[i][j] = 0;
      else if (i == 0) {
        knap[i][j] = 0;
      } else {
        if (j - W[i] >= 0 && knap[i / 2][j] < knap[i / 2][j - W[i]] + V[i])
          knap[i][j] = knap[i / 2][j - W[i]] + V[i];
        else
          knap[i][j] = knap[i / 2][j];
      }
    }
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int v, L;
    scanf("%d%d", &v, &L);
    if (v >= 512) {
      long long k = 1;
      int u[10];
      u[0] = v;
      int t = 0;
      while (u[t] >= 512) {
        k = k * 2;
        u[t + 1] = u[t] / 2;
        t++;
      }
      long long answer = 0;
      for (int a = 0; a < k; a++) {
        long long val = 0;
        long long wei = 0;
        int K = 1;
        int J = 0;
        while (J < t) {
          if ((a / K) % 2 == 1) {
            val += V[u[J]];
            wei += W[u[J]];
          }
          J++;
          K = K * 2;
        }
        if (wei <= L && answer < val + knap[u[t]][L - wei])
          answer = val + knap[u[t]][L - wei];
      }
      printf("%lld\\n", answer);
    } else
      printf("%lld\\n", knap[v][L]);
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int maxl = 100000;
ll v[270000], w[270000];
vector<pl> pv[10000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 0; i++ < n;)
    cin >> v[i] >> w[i];
  int m = 11;
  for (int i = 1; i < min(n + 1, 1 << m); i++) {
    vector<pl> cv, v1;
    for (int u = i; u > 0; u /= 2)
      cv.eb(w[u], v[u]);
    v1.eb(0, 0);
    for (int i = 0; i < (int)cv.size(); i++)
      for (int j = 0, k = v1.size(); j < k; j++)
        if (v1[j].fi + cv[i].fi <= maxl)
          v1.eb(v1[j].fi + cv[i].fi, v1[j].se + cv[i].se);
    sort(all(v1),
         [](pl &x, pl &y) { return x.fi == y.fi ? x.se > y.se : x.fi < y.fi; });
    pv[i].eb(0, 0);
    for (pl &t : v1)
      if (t.se > pv[i].back().se)
        pv[i].eb(t);
  }
  int q;
  cin >> q;
  for (int qi = 0; qi < q; qi++) {
    int u, u2, l;
    cin >> u >> l;
    vector<pl> cv, v1;
    for (u2 = u; u2 >= 1 << m; u2 /= 2)
      cv.eb(w[u2], v[u2]);
    v1.eb(0, 0);
    for (int i = 0; i < (int)cv.size(); i++)
      for (int j = 0, k = v1.size(); j < k; j++)
        if (v1[j].fi + cv[i].fi <= l)
          v1.eb(v1[j].fi + cv[i].fi, v1[j].se + cv[i].se);
    ll ans = 0;
    for (pl &t : v1)
      ans = max(ans,
                t.se + (lower_bound(all(pv[u2]), pl(l - t.fi, INF)) - 1)->se);
    cout << ans << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
unordered_map<ll, ll> mem;
int v[520010], w[520010];
ll get(int u, int l) {
  if (l < 0)
    return -INF;
  if (u == 1)
    return w[1] > l ? 0 : v[1];
  if (mem[u * INF + l] != 0)
    return mem[u * INF + l] - 1;
  ll f = max(get(u / 2, l - w[u]) + v[u], get(u / 2, l));
  mem[u * INF + l] = f + 1;
  return f;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n;
  for (int i = 0; i++ < n;)
    cin >> v[i] >> w[i];
  cin >> q;
  while (q-- > 0) {
    int u, l;
    cin >> u >> l;
    cout << get(u, l) << '\\n';
  }
  cout.flush();
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

const int M = 512;
const int L = 100000;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> v(n + 1), w(n + 1);
  static int m[M][L + 1];

  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
    if (i >= M)
      continue;

    for (int j = 0; j < w[i]; j++) {
      m[i][j] = m[i / 2][j];
    }
    for (int j = w[i]; j <= L; j++) {
      m[i][j] = max(m[i / 2][j], m[i / 2][j - w[i]] + v[i]);
    }
  }

  int q;
  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d%d", &u, &l);
    int ans = 0;
    int bits = 0;
    while ((u >> bits) >= M)
      ++bits;
    const int t = u >> bits;
    for (int j = 0; j < (1 << bits); j++) {
      int vv = 0, ww = 0;
      for (int k = 0; k < bits; k++) {
        if ((j >> k) & 1) {
          vv += v[u >> k];
          ww += w[u >> k];
        }
      }
      if (ww > l)
        continue;
      ans = max(ans, vv + m[t][l - ww]);
    }
    printf("%d\\n", ans);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

const int M = 1024;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> v(n + 1), w(n + 1);
  vector<map<int, int>> m(M);

  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
    if (i >= M)
      continue;

    m[i][w[i]] = max(m[i][w[i]], v[i]);

    for (int j = i / 2; j > 0; j /= 2) {
      for (const auto &wv : m[j]) {
        const int wj = wv.first;
        const int vj = wv.second;
        m[i][w[i] + wj] = max(m[i][w[i] + wj], vj + v[i]);
        m[i][wj] = max(m[i][wj], vj);
      }
    }

    int prev = 0;

    for (auto &wv : m[i]) {
      prev = wv.second = max(prev, wv.second);
    }
  }

  int q;
  scanf("%d", &q);

  for (int i = 0; i < q; i++) {
    int u, l;
    scanf("%d%d", &u, &l);
    int ans = 0;
    int bits = 0;
    while ((u >> bits) >= M)
      ++bits;
    const int t = u >> bits;
    for (int j = 0; j < (1 << bits); j++) {
      int vv = 0, ww = 0;
      for (int k = 0; k < bits; k++) {
        if ((j >> k) & 1) {
          vv += v[u >> k];
          ww += w[u >> k];
        }
      }
      if (ww > l)
        continue;
      auto it = m[t].upper_bound(l - ww);
      if (it != m[t].begin())
        vv += (--it)->second;
      ans = max(ans, vv);
    }
    printf("%d\\n", ans);
  }

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

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

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)

const int mxN = (1 << 18) + 5;
const int mxW = 1e5 + 5;
int N, V[mxN], W[mxN], Q;
int dp[(1 << 9) + 5][mxW];

int lim = (1 << 9) - 1;
void dfs(int i) {
  int p = i / 2;
  FOR(x, 0, mxW - 1) {
    dp[i][x] = dp[p][x];
    if (x >= W[i])
      dp[i][x] = max(dp[i][x], dp[p][x - W[i]] + V[i]);
  }
  if (i * 2 <= lim)
    dfs(i * 2);
  if (i * 2 + 1 <= lim)
    dfs(i * 2 + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  lim = min(lim, N);
  FOR(i, 1, N) { cin >> V[i] >> W[i]; }
  dfs(1);
  cin >> Q;
  FOR(i, 1, Q) {
    int v, L;
    cin >> v >> L;
    if (v <= lim)
      cout << dp[v][L] << '\\n';
    else {
      vector<int> vec;
      int u;
      for (u = v; u > lim; u /= 2) {
        vec.push_back(u);
      }
      int ans = 0;
      int k = SZ(vec);
      FOR(x, 0, (1 << k) - 1) {
        int sw = 0, sv = 0;
        FOR(j, 0, k - 1) if (x & (1 << j)) sw += W[vec[j]], sv += V[vec[j]];
        if (sw <= L)
          ans = max(ans, sv + dp[u][L - sw]);
      }
      cout << ans << '\\n';
    }
  }
}


Error: TLE

Fixed submission:

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

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)

const int mxN = (1 << 18) + 5;
const int mxW = 1e5 + 5;
int N, V[mxN], W[mxN], Q;
int dp[(1 << 9) + 5][mxW];
int f[mxW];

int lim = (1 << 9) - 1;
void dfs(int i) {
  int p = i / 2;
  FOR(x, 0, mxW - 1) {
    dp[i][x] = dp[p][x];
    if (x >= W[i])
      dp[i][x] = max(dp[i][x], dp[p][x - W[i]] + V[i]);
  }
  if (i * 2 <= lim)
    dfs(i * 2);
  if (i * 2 + 1 <= lim)
    dfs(i * 2 + 1);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  lim = min(lim, N);
  FOR(i, 1, N) { cin >> V[i] >> W[i]; }
  dfs(1);
  cin >> Q;
  FOR(i, 1, Q) {
    int v, L;
    cin >> v >> L;
    if (v <= lim)
      cout << dp[v][L] << '\\n';
    else {
      memset(f, 0, sizeof f);
      int u;
      for (u = v; u > lim; u /= 2) {
        RFOR(x, L, W[u]) { f[x] = max(f[x], f[x - W[u]] + V[u]); }
      }
      int ans = 0;
      FOR(i, 0, L) { ans = max(ans, f[i] + dp[u][L - i]); }
      cout << ans << '\\n';
    }
  }
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int V[5 + (1 << 18)];
int W[5 + (1 << 18)];

int dp[5 + (1 << 8)][100005];

const int INF = 1012345678;

int S, K;

int rec(int v, int L_rem, int bonus_value) {
  if (L_rem < 0) {
    return -INF;
  } else if (v < (1 << K)) {
    return bonus_value + dp[v][L_rem];
  }

  return max(rec(v >> 1, L_rem, bonus_value),
             rec(v >> 1, L_rem - W[v], bonus_value + V[v]));
}

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);
  K = log2(N) / 2;

  // printf("1<<K=%d\\n", 1<<K);

  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= (1 << K); i++) {
    for (int j = 0; j <= 100000; j++) {
      int temp = dp[i >> 1][j];
      if (j >= W[i]) {
        temp = max(temp, V[i] + dp[i >> 1][j - W[i]]);
      }
      dp[i][j] = temp;
    }
  }

  while (Q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    printf("%d\\n", rec(v, L, 0));
  }

  return 0;
}


Error: TLE

Fixed submission:

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

int V[5 + (1 << 18)];
int W[5 + (1 << 18)];

int memo[5 + (1 << 8)][100005];

const int INF = 1012345678;

int S;

int dp(int i, int j) {
  if (i == 0) {
    return 0;
  } else if (memo[i][j] != -1) {
    return memo[i][j];
  } else {
    int temp = dp(i >> 1, j);
    if (j >= W[i]) {
      temp = max(temp, V[i] + dp(i >> 1, j - W[i]));
    }

    return memo[i][j] = temp;
  }
}

int K;

int rec(int v, int L_rem, int bonus_value) {
  if (L_rem < 0) {
    return -INF;
  } else if (v < (1 << K)) {
    return bonus_value + dp(v, L_rem);
  }

  return max(rec(v >> 1, L_rem, bonus_value),
             rec(v >> 1, L_rem - W[v], bonus_value + V[v]));
}

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);
  K = log2(N) / 2;

  // printf("1<<K=%d\\n", 1<<K);

  memset(memo, -1, sizeof(memo));
  while (Q--) {
    int v, L;
    scanf("%d%d", &v, &L);
    printf("%d\\n", rec(v, L, 0));
  }

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

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
const int MAXV = 1e5 + 5;
const int MAXM = 1 << 9;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int a[MAXM][MAXV];
int n, m, r, q, v[MAXN], w[MAXN];
int query(int x, int y) {
  if (x <= m)
    return a[x][y];
  else {
    int ans = query(x / 2, y);
    if (y >= w[x])
      chkmax(ans, v[x] + query(x / 2, y - w[x]));
    return ans;
  }
}
int main() {
  read(n), m = (1 << 9) - 1, r = 1e5;
  for (int i = 1; i <= n; i++)
    read(v[i]), read(w[i]);
  for (int i = 1; i <= m; i++) {
    int f = i / 2;
    for (int j = 1; j <= r; j++) {
      a[i][j] = a[f][j];
      if (j >= w[i])
        chkmax(a[i][j], v[i] + a[f][j - w[i]]);
    }
  }
  read(q);
  for (int i = 1; i <= q; i++) {
    int x, y;
    read(x), read(y);
    printf("%d\\n", query(x, y));
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
const int MAXV = 1e5 + 5;
const int MAXM = 1 << 9;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int a[MAXM][MAXV];
int n, m, r, q, v[MAXN], w[MAXN];
int query(int x, int y) {
  if (x <= m)
    return a[x][y];
  else {
    int ans = query(x / 2, y);
    if (y >= w[x])
      chkmax(ans, v[x] + query(x / 2, y - w[x]));
    return ans;
  }
}
int main() {
  read(n), m = n, r = (1 << 9) - 1;
  for (int i = 1; i <= n; i++)
    read(v[i]), read(w[i]);
  for (int i = 1; i <= m; i++) {
    int f = i / 2;
    for (int j = 1; j <= r; j++) {
      a[i][j] = a[f][j];
      if (j >= w[i])
        chkmax(a[i][j], v[i] + a[f][j - w[i]]);
    }
  }
  read(q);
  for (int i = 1; i <= q; i++) {
    int x, y;
    read(x), read(y);
    printf("%d\\n", query(x, y));
  }
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long v[300005], w[300005], dp[520][100005];
int n;
long long solve(int x, int y) {
  if (x <= 512)
    return dp[x][y];
  if (y < w[x])
    return solve(x / 2, y);
  return max(solve(x / 2, y), solve(x / 2, y - w[x]) + v[x]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= min(512, n); i++) {
    for (int j = 0; j <= 100000; j++) {
      if (j < w[i])
        dp[i][j] = dp[i / 2][j];
      else
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, w;
    cin >> v >> w;
    cout << solve(v, w) << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long v[300005], w[300005], sv[300005], sw[300005];
int n;
long long solve(int x, int y) {
  if (sw[x] <= y)
    return sv[x];
  if (x == 1) {
    if (y >= w[x])
      return v[x];
    else
      return 0;
  }
  if (y < w[x])
    return solve(x / 2, y);
  if (sv[x / 2] < v[x])
    return solve(x / 2, y - w[x]) + v[x];
  return max(solve(x / 2, y - w[x]) + v[x], solve(x / 2, y));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }
  sv[0] = sw[0] = 0;
  for (int i = 1; i <= n; i++) {
    sv[i] = sv[i / 2];
    sv[i] += v[i];
    sw[i] = sw[i / 2];
    sw[i] += w[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int v, w;
    cin >> v >> w;
    cout << solve(v, w) << "\\n";
  }
}
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 & 1 << j) {
          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], c;
    while (b > 511) {
      b /= 2;
      len++;
    }

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

    int ans = 0, vsum, wsum;

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

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

    cout << ans << endl;
  }

  return 0;
}