prithivMLmods/Coder-Stat huggingface dataset:
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, k, a[maxn], d[maxn];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  while (k--) {
    for (int i = 0; i <= n + 1; i++)
      d[i] = 0;
    for (int i = 1; i <= n; i++) {
      int l = max(i - a[i], 1), r = min(i + a[i], n);
      d[l]++, d[r + 1]--;
    }
    for (int i = 1; i <= n; i++)
      d[i] += d[i - 1];
    for (int i = 1; i <= n; i++)
      a[i] = d[i];
  }
  for (int i = 1; i <= n; i++)
    printf("%d%s", a[i], i == n ? "\n" : " ");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, k, a[maxn], d[maxn];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  while (k--) {
    bool f = true;
    for (int i = 1; i <= n; i++)
      if (a[i] != n)
        f = false;
    if (f)
      break;
    for (int i = 0; i <= n + 1; i++)
      d[i] = 0;
    for (int i = 1; i <= n; i++) {
      int l = max(i - a[i], 1), r = min(i + a[i], n);
      d[l]++, d[r + 1]--;
    }
    for (int i = 1; i <= n; i++)
      d[i] += d[i - 1];
    for (int i = 1; i <= n; i++)
      a[i] = d[i];
  }
  for (int i = 1; i <= n; i++)
    printf("%d%s", a[i], i == n ? "\n" : " ");
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long num, k;
  long long tmp1, tmp2;
  long long Min = 0;

  cin >> num >> k;
  // vector<long long> vec(num,0);
  vector<long long> d(num, 0);

  for (int i = 0; i < num; i++)
    cin >> d.at(i);

  for (int i = 0; i < k; i++) {
    vector<long long> vec(num + 1, 0);
    for (int j = 0; j < num; j++) {
      tmp1 = max(Min, (j + 1) - d.at(j) - 1);
      tmp2 = min(num, (j + 1) + d.at(j));

      vec.at(tmp1)++;
      vec.at(tmp2)--;
    }

    for (int j = 0; j < num; j++) {
      vec.at(j + 1) += vec.at(j);
    }

    d = vec;
  }

  for (int i = 0; i < num; i++) {
    cout << d.at(i) << " ";
  }
}

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

int main() {
  long long num, k;
  long long tmp1, tmp2;
  long long Min = 0;

  cin >> num >> k;
  // vector<long long> vec(num,0);
  vector<long long> d(num, 0);

  for (int i = 0; i < num; i++)
    cin >> d.at(i);

  for (int i = 0; i < k; i++) {
    vector<long long> vec(num + 1, 0);
    for (int j = 0; j < num; j++) {
      tmp1 = max(Min, (j + 1) - d.at(j) - 1);
      tmp2 = min(num, (j + 1) + d.at(j));

      vec.at(tmp1)++;
      vec.at(tmp2)--;
    }

    for (int j = 0; j < num; j++) {
      vec.at(j + 1) += vec.at(j);
    }
    vec.pop_back();

    if (d == vec)
      break;

    d = vec;
  }

  for (int i = 0; i < num; i++) {
    cout << d.at(i) << " ";
  }
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v, vmin, vmax;
  rep(i, n) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  vector<int> org(n, 0);
  vector<int> s = v;

  if (k < 1000000) {
    rep(j, k) {
      v = s;
      s = org;
      vmin.clear();
      vmax.clear();
      rep(i, n) {
        vmin.push_back(i - v[i]);
        vmax.push_back(i + v[i]);
      }

      vmin.push_back(1000000);
      vmax.push_back(1000000);
      sort(vmin.begin(), vmin.end());
      sort(vmax.begin(), vmax.end());
      // if(vmin[n-1]<0 &&vmax[n-1]>n-1 ){
      //     rep(i,n){
      //         s[i] += (n-1)* (k-j);
      //     }
      //     break;
      // }
      int value = 0;
      int i = 0, mini = 0, maxi = 0;
      // i = max(0,vmin[0]);
      // s = org;
      while (i < n) {
        if (i >= vmin[mini]) {
          value++;
          mini++;
        } else if (i > vmax[maxi]) {
          value--;
          maxi++;
        } else {
          s[i] = value;
          i++;
        }
        // cout << i  <<" " <<   value<< " " << mini << " " <<maxi << endl;
      }
    }
  } else {
    rep(i, n) { s[i] = n; }
  }

  rep(i, n) { cout << s[i] << " "; }
  cout << endl;
}


Fixed submission:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v, vmin, vmax;
  rep(i, n) {
    int a;
    cin >> a;
    v.push_back(a);
  }
  vector<int> org(n, 0);
  vector<int> s = v;

  if (k < 100) {
    rep(j, k) {
      v = s;
      s = org;
      vmin.clear();
      vmax.clear();
      rep(i, n) {
        vmin.push_back(i - v[i]);
        vmax.push_back(i + v[i]);
      }

      vmin.push_back(1000000);
      vmax.push_back(1000000);
      sort(vmin.begin(), vmin.end());
      sort(vmax.begin(), vmax.end());
      // if(vmin[n-1]<0 &&vmax[n-1]>n-1 ){
      //     rep(i,n){
      //         s[i] += (n-1)* (k-j);
      //     }
      //     break;
      // }
      int value = 0;
      int i = 0, mini = 0, maxi = 0;
      // i = max(0,vmin[0]);
      // s = org;
      while (i < n) {
        if (i >= vmin[mini]) {
          value++;
          mini++;
        } else if (i > vmax[maxi]) {
          value--;
          maxi++;
        } else {
          s[i] = value;
          i++;
        }
        // cout << i  <<" " <<   value<< " " << mini << " " <<maxi << endl;
      }
    }
  } else {
    rep(i, n) { s[i] = n; }
  }

  rep(i, n) { cout << s[i] << " "; }
  cout << endl;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

#define SEG_LEN (1 << 18)

int seg[SEG_LEN * 2];

// one point
long long int get(int ind) {
  ind += SEG_LEN;
  long long int sum = 0;
  sum += seg[ind];
  while (true) {
    ind /= 2;
    if (ind == 0)
      break;
    sum += seg[ind];
  }
  return sum;
}

// range
void add(int l, int r, int x) {
  l += SEG_LEN;
  r += SEG_LEN;
  while (l < r) {
    if (l % 2 == 1) {
      seg[l] += x;
      l++;
    }
    l /= 2;
    if (r % 2 == 1) {
      seg[r - 1] += x;
      r--;
    }
    r /= 2;
  }
}

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  rep(i, n) { add(i, i + 1, a[i]); }

  rep(c, 200) {
    if (c == k)
      break;
    vector<pair<ll, ll>> addd(n);
    rep(i, n) {
      int l = max(i - get(i), 0ll);
      int r = min(i + get(i), n - 1);

      addd[i] = make_pair(l, r);
    }

    vector<int> tmp(n);
    rep(i, n) tmp[i] = get(i);

    rep(i, n) {
      int l = addd[i].first;
      int r = addd[i].second;
      add(l, r + 1, 1);
    }

    rep(i, n) { add(i, i + 1, -tmp[i]); }
  }

  rep(j, n - 1) { cout << get(j) << " "; }
  cout << get(n - 1) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

#define SEG_LEN (1 << 18)

int seg[SEG_LEN * 2];

// one point
long long int get(int ind) {
  ind += SEG_LEN;
  long long int sum = 0;
  sum += seg[ind];
  while (true) {
    ind /= 2;
    if (ind == 0)
      break;
    sum += seg[ind];
  }
  return sum;
}

// range
void add(int l, int r, int x) {
  l += SEG_LEN;
  r += SEG_LEN;
  while (l < r) {
    if (l % 2 == 1) {
      seg[l] += x;
      l++;
    }
    l /= 2;
    if (r % 2 == 1) {
      seg[r - 1] += x;
      r--;
    }
    r /= 2;
  }
}

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

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  rep(i, n) { add(i, i + 1, a[i]); }

  rep(c, 200) {
    if (c == k)
      break;
    vector<pair<ll, ll>> addd(n);
    rep(i, n) {
      int l = max(i - get(i), 0ll);
      int r = min(i + get(i), n - 1);

      addd[i] = make_pair(l, r);
    }

    vector<int> tmp(n);
    rep(i, n) tmp[i] = get(i);

    rep(i, n) {
      int l = addd[i].first;
      int r = addd[i].second;
      add(l, r + 1, 1);
    }

    rep(i, n) { add(i, i + 1, -tmp[i]); }
    bool flg = true;
    rep(i, n) {
      if (get(i) != n) {
        flg = false;
        break;
      }
    }

    if (flg)
      break;
  }

  rep(j, n - 1) { cout << get(j) << " "; }
  cout << get(n - 1) << endl;

  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
ll N1097 = 1000000007LL;

ll N, K;
ll A[1000000];
ll d[1000000];
int main() {
  cin >> N >> K;
  for (ll i = 0; i < N; i++) {
    scanf("%ld", &A[i]);
  }
  //
  for (ll l = 0; l < K; l++) {
    for (ll i = 0; i <= N; i++) {
      d[i] = 0LL;
    }
    for (ll i = 0; i < N; i++) {
      ll a = A[i];
      d[max(0LL, i - a)] += 1;
      d[min(N, i + a + 1)] -= 1;
    }
    ll tmp = 0;
    for (ll i = 0; i < N; i++) {
      tmp += d[i];
      A[i] = tmp;
    }
  }
  for (ll i = 0; i < N; i++) {
    printf("%d ", A[i]);
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
ll N1097 = 1000000007LL;

ll N, K;
ll A[1000000];
ll d[1000000];
int main() {
  cin >> N >> K;
  for (ll i = 0; i < N; i++) {
    scanf("%ld", &A[i]);
  }
  //
  for (ll l = 0; l < K; l++) {
    for (ll i = 0; i <= N; i++) {
      d[i] = 0LL;
    }
    for (ll i = 0; i < N; i++) {
      ll a = A[i];
      d[max(0LL, i - a)] += 1;
      d[min(N, i + a + 1)] -= 1;
    }
    ll tmp = 0;
    for (ll i = 0; i < N; i++) {
      tmp += d[i];
      A[i] = tmp;
    }
    int f = 1;
    for (ll i = 0; i < N; i++) {
      if (A[i] != N) {
        f = 0;
        break;
      }
    }
    if (f) {
      break;
    }
  }
  for (ll i = 0; i < N; i++) {
    printf("%d ", A[i]);
  }
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

#define NDEBUG

std::vector<int> next(std::vector<int> v) {
  std::vector<int> vec(v.size() + 1);
  for (int i = 0; i < (int)v.size(); i++) {
    int L = std::max(0, i - v[i]);
    int R = std::min((int)v.size() - 1, i + v[i]);

    vec[L]++;
    vec[R + 1]--;
  }
  for (int i = 0; i < (int)v.size() - 1; i++)
    vec[i + 1] += vec[i];
  return vec;
};

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

#ifndef NDEBUG
  cout << 0 << ": ";
  for (auto v : a)
    cout << v << " ";
  cout << endl;

  for (int i = 0; i < k; i++) {
    a = next(a);

    cout << i + 1 << ": ";
    for (auto v : a)
      cout << v << " ";
    cout << endl;
  }
#endif

  for (int i = 0; i < k; i++) {
    auto v = next(a);

    if (v == a)
      break;
    a.swap(v);
  }

  for (auto v : a)
    cout << v << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

#define NDEBUG

std::vector<int> next(std::vector<int> v) {
  std::vector<int> vec(v.size() + 1);
  for (int i = 0; i < (int)v.size(); i++) {
    int L = std::max(0, i - v[i]);
    int R = std::min((int)v.size() - 1, i + v[i]);

    vec[L]++;
    vec[R + 1]--;
  }
  for (int i = 0; i < (int)v.size() - 1; i++)
    vec[i + 1] += vec[i];
  vec.pop_back();
  return vec;
};

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  std::vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

#ifndef NDEBUG
  cout << 0 << ": ";
  for (auto v : a)
    cout << v << " ";
  cout << endl;

  for (int i = 0; i < k; i++) {
    a = next(a);

    cout << i + 1 << ": ";
    for (auto v : a)
      cout << v << " ";
    cout << endl;
  }
#endif

  for (int i = 0; i < k; i++) {
    auto v = next(a);

    if (v == a)
      break;
    a.swap(v);
  }

  for (auto v : a)
    cout << v << " ";
  cout << endl;
  return 0;
}
Problem p02647 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> A(n);
  rep(i, n) cin >> A[i];

  if (n == 1) {
    cout << min(n, k) << endl;
  }

  else {
    for (int l = 1; l <= min(k, 50); l++) {
      vector<int> B(n, 0);
      for (int i = 0; i < n; i++) {
        int l = max(0, i - A[i]);
        int r = min(i + A[i], n - 1);
        B[l]++;
        if (B[r + 1] < n) {
          B[r + 1]--;
        }
      }

      for (int i = 1; i < n; i++)
        B[i] += B[i - 1]; // B complete

      rep(i, n) {
        A[i] = B[i]; // new A
      }
    }
  }

  rep(i, n) {
    if (i == n - 1) {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << endl;
      }
    } else {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << " ";
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> A(n);
  rep(i, n) cin >> A[i];

  if (n == 1) {
    cout << min(n, k) << endl;
  }

  else {
    for (int l = 1; l <= min(k, 50); l++) {
      vector<int> B(n, 0);
      for (int i = 0; i < n; i++) {
        int l = max(0, i - A[i]);
        int r = min(i + A[i], n - 1);
        B[l]++;
        if (r + 1 < n) {
          B[r + 1]--;
        }
      }

      for (int i = 1; i < n; i++)
        B[i] += B[i - 1]; // B complete

      rep(i, n) {
        A[i] = B[i]; // new A
      }
    }
  }

  rep(i, n) {
    if (i == n - 1) {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << endl;
      }
    } else {
      if (A[i] > n) {
        cout << n << endl;
      } else {
        cout << A[i] << " ";
      }
    }
  }
}
Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 2e9
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> a(n), b(n + 1, 0);
  rep(i, 0, n) cin >> a[i];

  while (k) {
    rep(i, 0, n) {
      b[max(0, i - a[i])]++;
      b[min(n, i + a[i] + 1)]--;
    }
    rep(i, 0, n) {
      b[i + 1] += b[i];
      a[i] = b[i];
      b[i] = 0;
    }
    k--;
  }
  rep(i, 0, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 2e9
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> a(n), b(n + 1, 0);
  rep(i, 0, n) cin >> a[i];

  if (k > 500)
    k = 500;
  while (k) {
    rep(i, 0, n) {
      b[max(0, i - a[i])]++;
      b[min(n, i + a[i] + 1)]--;
    }
    rep(i, 0, n) {
      b[i + 1] += b[i];
      a[i] = b[i];
      b[i] = 0;
    }
    k--;
  }
  rep(i, 0, n) cout << a[i] << " ";
  cout << endl;
  return 0;
}
Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int maxN = (1 << 18) + 10;
const int MAX = 1e5;
typedef long long ll;

int n, q;
#define pii pair<int, int>
pii a[maxN];
#define V first
#define W second

ll pre[maxN];
ll dp[512][MAX + 1];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].V >> a[i].W;
  }

  const int limit = 512;
  for (int j = 0; j <= MAX; ++j)
    dp[1][j] = (a[1].W > j ? 0 : a[1].V);

  for (int i = 2; i < min(limit, n + 1); ++i) {
    int par = i / 2;
    for (int j = 0; j <= MAX; ++j) {
      dp[i][j] = dp[par][j];
      if (j >= a[i].W)
        dp[i][j] = max(dp[i][j], dp[par][j - a[i].W] + a[i].V);
    }
  }

  cin >> q;
  while (q--) {
    int x, L;
    cin >> x >> L;
    vector<int> node;

    while (x >= limit) {
      node.push_back(x);
      x /= 2;
    }
    ll ans = 0;
    int sz = node.size();
    for (int i = 0; i < (1 << sz); ++i) {
      ll Le = 0;
      int Ri = 0;
      for (int j = 0; j < sz; j)
        if ((i >> j) & 1) {
          Le = Le + a[node[j]].V;
          Ri = Ri + a[node[j]].W;
        }
      if (Ri <= L)
        ans = max(ans, dp[x][L - Ri] + Le);
    }
    cout << ans << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxN = (1 << 18) + 10;
const int MAX = 1e5;
typedef long long ll;

int n, q;
#define pii pair<int, int>
pii a[maxN];
#define V first
#define W second

ll pre[maxN];
ll dp[512][MAX + 1];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //  freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].V >> a[i].W;
  }

  const int limit = 512;
  for (int j = 0; j <= MAX; ++j)
    dp[1][j] = (a[1].W > j ? 0 : a[1].V);

  for (int i = 2; i < min(limit, n + 1); ++i) {
    int par = i / 2;
    for (int j = 0; j <= MAX; ++j) {
      dp[i][j] = dp[par][j];
      if (j >= a[i].W)
        dp[i][j] = max(dp[i][j], dp[par][j - a[i].W] + a[i].V);
    }
  }

  cin >> q;
  while (q--) {
    int x, L;
    cin >> x >> L;
    vector<int> node;

    while (x >= limit) {
      node.push_back(x);
      x /= 2;
    }
    ll ans = 0;
    int sz = node.size();
    for (int i = 0; i < (1 << sz); ++i) {
      ll Le = 0;
      int Ri = 0;
      for (int j = 0; j < sz; j++)
        if ((i >> j) & 1) {
          Le = Le + a[node[j]].V;
          Ri = Ri + a[node[j]].W;
        }
      if (Ri <= L)
        ans = max(ans, dp[x][L - Ri] + Le);
    }
    cout << ans << '\n';
  }
  return 0;
}
Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <ctime>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1);
  vector<int> W(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> V[i] >> W[i];

  int Q;
  cin >> Q;
  vector<int> v(Q);
  vector<int> L(Q);
  for (int i = 0; i < Q; i++)
    cin >> v[i] >> L[i];

  int D = 0;
  while ((1 << D) - 1 < N)
    D++;
  int TD = D / 2;
  vector<vector<int>> T(1 << TD, vector<int>(100001));
  for (int i = W[1]; i <= 100000; i++)
    T[1][i] = V[1];
  for (int i = 2; i < 1 << TD; i++)
    for (int j = 0; j <= 100000; j++) {
      T[i][j] = T[i / 2][j];
      if (j >= W[i])
        T[i][j] = max(T[i][j], T[i / 2][j - W[i]] + V[i]);
    }

  vector<int> W2;
  vector<int> V2;
  for (int q = 0; q < Q; q++) {
    int t = v[q];
    W2.clear();
    V2.clear();
    while (t >= 1 << TD) {
      W2.push_back(W[t]);
      V2.push_back(V[t]);
      t /= 2;
    }
    int ans = 0;
    for (int b = 0; b < 1 << (int)W2.size(); b++) {
      int w = 0;
      int v = 0;
      for (int i = 0; i < (int)W2.size(); i++)
        if (b >> i & 1) {
          w += W2[i];
          v += V2[i];
        }
      if (w <= L[q])
        ans = max(ans, v + T[t][L[q] - w]);
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <ctime>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> V(N + 1);
  vector<int> W(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> V[i] >> W[i];

  int Q;
  cin >> Q;
  vector<int> v(Q);
  vector<int> L(Q);
  for (int i = 0; i < Q; i++)
    cin >> v[i] >> L[i];

  int D = 0;
  while ((1 << D) - 1 < N)
    D++;
  int TD = D / 2;
  if (TD == 0)
    TD = 1;
  vector<vector<int>> T(1 << TD, vector<int>(100001));
  for (int i = W[1]; i <= 100000; i++)
    T[1][i] = V[1];
  for (int i = 2; i < 1 << TD; i++)
    for (int j = 0; j <= 100000; j++) {
      T[i][j] = T[i / 2][j];
      if (j >= W[i])
        T[i][j] = max(T[i][j], T[i / 2][j - W[i]] + V[i]);
    }

  vector<int> W2;
  vector<int> V2;
  for (int q = 0; q < Q; q++) {
    int t = v[q];
    W2.clear();
    V2.clear();
    while (t >= 1 << TD) {
      W2.push_back(W[t]);
      V2.push_back(V[t]);
      t /= 2;
    }
    int ans = 0;
    for (int b = 0; b < 1 << (int)W2.size(); b++) {
      int w = 0;
      int v = 0;
      for (int i = 0; i < (int)W2.size(); i++)
        if (b >> i & 1) {
          w += W2[i];
          v += V2[i];
        }
      if (w <= L[q])
        ans = max(ans, v + T[t][L[q] - w]);
    }
    cout << ans << endl;
  }
}
Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll N;
vector<ll> V, W;
ll Q;
const ll K = 9, L = 100000;
ll I;
vector<vector<ll>> Dp;
vector<pair<ll, ll>> C;

ll solve(ll i, ll l) {
  C.clear();
  C.push_back({l, 0});
  while (i > I) {
    ll n = C.size();
    for (ll k = 0; k < n; ++k) {
      ll l, v;
      tie(l, v) = C[k];
      if (l >= W[i])
        C.push_back({l - W[k], v + V[i]});
    }
    i /= 2;
  }
  ll res = 0;
  for (auto lv : C) {
    ll l, v;
    tie(l, v) = lv;
    res = max(res, v + Dp[i][l]);
  }
  return res;
}
int main() {
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (ll i = 1; i <= N; ++i)
    cin >> V[i] >> W[i];
  I = min(N, (1ll << K) - 1);
  Dp.resize(I + 1);
  for (auto &dp : Dp)
    dp.resize(L + 1);
  for (ll i = 1; i <= I; ++i) {
    for (ll l = 0; l <= L; ++l) {
      Dp[i][l] = Dp[i / 2][l];
      if (W[i] <= l)
        Dp[i][l] = max(Dp[i][l], Dp[i / 2][l - W[i]] + V[i]);
    }
  }
  cin >> Q;
  for (ll q = 0; q < Q; ++q) {
    ll i, l;
    cin >> i >> l;
    cout << solve(i, l) << endl;
  }
  return 0;
}


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

typedef long long ll;

ll N;
vector<ll> V, W;
ll Q;
const ll K = 9, L = 100000;
ll I;
vector<vector<ll>> Dp;
vector<pair<ll, ll>> C;

ll solve(ll i, ll l) {
  C.clear();
  C.push_back({l, 0});
  while (i > I) {
    ll n = C.size();
    for (ll k = 0; k < n; ++k) {
      ll l, v;
      tie(l, v) = C[k];
      if (l >= W[i])
        C.push_back({l - W[i], v + V[i]});
    }
    i /= 2;
  }
  ll res = 0;
  for (auto lv : C) {
    ll l, v;
    tie(l, v) = lv;
    res = max(res, v + Dp[i][l]);
  }
  return res;
}
int main() {
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (ll i = 1; i <= N; ++i)
    cin >> V[i] >> W[i];
  I = min(N, (1ll << K) - 1);
  Dp.resize(I + 1);
  for (auto &dp : Dp)
    dp.resize(L + 1);
  for (ll i = 1; i <= I; ++i) {
    for (ll l = 0; l <= L; ++l) {
      Dp[i][l] = Dp[i / 2][l];
      if (W[i] <= l)
        Dp[i][l] = max(Dp[i][l], Dp[i / 2][l - W[i]] + V[i]);
    }
  }
  cin >> Q;
  for (ll q = 0; q < Q; ++q) {
    ll i, l;
    cin >> i >> l;
    cout << solve(i, l) << endl;
  }
  return 0;
}
Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 515
#define MAXL 100005

using namespace std;
int dp[MAXN][MAXL];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &v[i], &w[i]);
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i < min(n + 1, 1 << 9); ++i) {
    int p = i / 2;
    for (int j = 1; j < MAXL; ++j)
      dp[i][j] = max(dp[p][j], j >= w[i] ? dp[p][j - w[i]] + v[i] : 0);
  }
  int q;
  scanf("%d", &q);
  string ans;
  for (int i = 0; i < q; ++i) {
    int vi, l;
    scanf("%d%d", &vi, &l);
    if (vi < (1 << 9)) {
      ans += to_string(dp[vi][l]) + "\n";
      continue;
    }
    vector<int> path;
    while (vi >= (1 << 9)) {
      path.emplace_back(vi);
      vi >>= 1;
    }
    int best = 0;
    int m = path.size();
    for (int j = 0; j < (1 << m); ++j) {
      int vs = 0, ws = 0;
      for (int k = 0; k < m; ++k)
        if (j & (1 << k)) {
          vs += v[path[i]];
          ws += w[path[i]];
        }
      if (ws > l)
        continue;
      best = max(best, vs + dp[vi][l - ws]);
    }
    ans += to_string(best) + "\n";
  }
  printf("%s", ans.c_str());
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXN 515
#define MAXL 100005

using namespace std;
int dp[MAXN][MAXL];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &v[i], &w[i]);
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i < min(n + 1, 1 << 9); ++i) {
    int p = i / 2;
    for (int j = 1; j < MAXL; ++j)
      dp[i][j] = max(dp[p][j], j >= w[i] ? dp[p][j - w[i]] + v[i] : 0);
  }
  int q;
  scanf("%d", &q);
  string ans;
  for (int i = 0; i < q; ++i) {
    int vi, l;
    scanf("%d%d", &vi, &l);
    if (vi < (1 << 9)) {
      ans += to_string(dp[vi][l]) + "\n";
      continue;
    }
    vector<int> path;
    while (vi >= (1 << 9)) {
      path.emplace_back(vi);
      vi >>= 1;
    }
    int best = 0;
    int m = path.size();
    for (int j = 0; j < (1 << m); ++j) {
      int vs = 0, ws = 0;
      for (int k = 0; k < m; ++k)
        if (j & (1 << k)) {
          vs += v[path[k]];
          ws += w[path[k]];
        }
      if (ws > l)
        continue;
      best = max(best, vs + dp[vi][l - ws]);
    }
    ans += to_string(best) + "\n";
  }
  printf("%s", ans.c_str());
}
Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, Q;
#define Maxn 100010
int V[Maxn * 10], W[Maxn * 10];
int f[512][100010];
int seq1[Maxn], seq2[Maxn], cnt;
int to[Maxn];
int val1[Maxn], val2[Maxn];
void dfs(int u) {
  if (u >= 512)
    return;
  if (u > n)
    return;
  int fa = u / 2;
  for (int i = 0; i <= 100000; ++i) {
    f[u][i] = f[fa][i];
    if (i >= W[u])
      f[u][i] = max(f[u][i], f[fa][i - W[u]] + V[u]);
  }
  dfs(u << 1);
  dfs(u << 1 | 1);
}

int lowbit(int x) { return x & (-x); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &V[i], &W[i]);
  }
  dfs(1);
  scanf("%d", &Q);
  int u, L;
  for (int i = 0; i < 9; ++i)
    to[(1 << i)] = i;
  while (Q--) {
    scanf("%d%d", &u, &L);
    if (u < 512)
      printf("%d\n", f[u][L]);
    else {
      cnt = 0;
      while (u >= 4) {
        seq1[cnt] = W[u];
        seq2[cnt] = V[u];
        u >>= 1;
        cnt++;
      }
      int Ans = f[u][L];
      for (int i = 1; i < (1 << cnt); ++i) {
        int d = lowbit(i);
        val1[i] = val1[i ^ d] + seq1[to[d]];
        val2[i] = val2[i ^ d] + seq2[to[d]];
        if (val1[i] <= L)
          Ans = max(Ans, f[u][L - val1[i]] + val2[i]);
      }
      printf("%d\n", Ans);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, Q;
#define Maxn 100010
int V[Maxn * 10], W[Maxn * 10];
int f[512][100010];
int seq1[Maxn], seq2[Maxn], cnt;
int to[Maxn];
int val1[Maxn], val2[Maxn];
void dfs(int u) {
  if (u >= 512)
    return;
  if (u > n)
    return;
  int fa = u / 2;
  for (int i = 0; i <= 100000; ++i) {
    f[u][i] = f[fa][i];
    if (i >= W[u])
      f[u][i] = max(f[u][i], f[fa][i - W[u]] + V[u]);
  }
  dfs(u << 1);
  dfs(u << 1 | 1);
}

int lowbit(int x) { return x & (-x); }

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &V[i], &W[i]);
  }
  dfs(1);
  scanf("%d", &Q);
  int u, L;
  for (int i = 0; i < 9; ++i)
    to[(1 << i)] = i;
  while (Q--) {
    scanf("%d%d", &u, &L);
    if (u < 512)
      printf("%d\n", f[u][L]);
    else {
      cnt = 0;
      while (u >= 512) {
        seq1[cnt] = W[u];
        seq2[cnt] = V[u];
        u >>= 1;
        cnt++;
      }
      int Ans = f[u][L];
      for (int i = 1; i < (1 << cnt); ++i) {
        int d = lowbit(i);
        val1[i] = val1[i ^ d] + seq1[to[d]];
        val2[i] = val2[i ^ d] + seq2[to[d]];
        if (val1[i] <= L)
          Ans = max(Ans, f[u][L - val1[i]] + val2[i]);
      }
      printf("%d\n", Ans);
    }
  }
  return 0;
}
Problem p02648 (C++)

Buggy submission (status: 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 = 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;
}


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 = (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;
}
Problem p02648 (C++)

Buggy submission (status: 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 >= 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;
  }
}

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 >= 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;
  }
}
Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N (1 << 19)
#define M 100005
#define K (1 << 8)

LL n, m, q, a[N], b[N], c[N], f[K + 5][M];

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; ++i) {
    scanf("%lld%lld", a + i, b + i);
  }
  m = 100000;
  for (LL i = 1; i <= K && i <= n; ++i) {
    LL t = 0;
    for (LL j = i; j; j /= 2)
      c[t++] = j;
    for (LL j = 0; j < 1 << t; ++j) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (j >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= m)
        f[i][w] = max(f[i][w], v);
    }
    for (LL j = 1; j <= m; ++j)
      f[i][j] = max(f[i][j], f[i][j - 1]);
  }
  scanf("%lld", &q);
  while (q--) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    LL t = 0, ans = 0;
    for (; x > K; x /= 2)
      c[t++] = x;
    for (LL i = 0; i < 1 << t; ++i) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (i >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= y)
        ans = max(ans, f[x][y - w] + v);
    }
    printf("%lld\n", ans);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
#define N (1 << 19)
#define M 100005
#define K (1 << 9)

LL n, m, q, a[N], b[N], c[N], f[K + 5][M];

int main() {
  scanf("%lld", &n);
  for (LL i = 1; i <= n; ++i) {
    scanf("%lld%lld", a + i, b + i);
  }
  m = 100000;
  for (LL i = 1; i <= K && i <= n; ++i) {
    LL t = 0;
    for (LL j = i; j; j /= 2)
      c[t++] = j;
    for (LL j = 0; j < 1 << t; ++j) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (j >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= m)
        f[i][w] = max(f[i][w], v);
    }
    for (LL j = 1; j <= m; ++j)
      f[i][j] = max(f[i][j], f[i][j - 1]);
  }
  scanf("%lld", &q);
  while (q--) {
    LL x, y;
    scanf("%lld%lld", &x, &y);
    LL t = 0, ans = 0;
    for (; x > K; x /= 2)
      c[t++] = x;
    for (LL i = 0; i < 1 << t; ++i) {
      LL w = 0, v = 0;
      for (LL k = 0; k < t; ++k) {
        if (i >> k & 1) {
          v += a[c[k]];
          w += b[c[k]];
        }
      }
      if (w <= y)
        ans = max(ans, f[x][y - w] + v);
    }
    printf("%lld\n", ans);
  }

  return 0;
}
Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 200009;
const int MOD = 1e9 + 7;
int V[maxn], W[maxn], n, q, dp[555][100009];
int main() {
#ifdef DEBUG
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> W[i] >> V[i];
  for (int i = 1; i <= 512; ++i) {
    for (int j = 0; j <= 100000; ++j)
      dp[i][j] = dp[i >> 1][j];
    for (int j = 0; j <= 100000; ++j)
      if (j >= V[i])
        dp[i][j] = max(dp[i >> 1][j - V[i]] + W[i], dp[i][j]);
  }
  cin >> q;
  for (int ts = 1; ts <= q; ++ts) {
    int v, l, ans = 0;
    cin >> v >> l;
    if (v <= 512) {
      ans = dp[v][l];
    } else {
      vector<int> idx;
      for (; v > 512; v >>= 1)
        idx.push_back(v);
      int totv, totw;
      for (int i = 0; i < (1 << Sz(idx)); ++i) {
        totv = totw = 0;
        for (int j = 0; j < Sz(idx); ++j)
          if (i >> j & 1)
            totv += V[idx[j]], totw += W[idx[j]];
        if (totv <= l) {
          ans = max(dp[v][l - totv] + totw, ans);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
// 2020.06.14 10:06:45


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Sz(a) ((int)(a).size())
const int maxn = 1 << 19;
const int MOD = 1e9 + 7;
int V[maxn], W[maxn], n, q, dp[555][100009];
int main() {
#ifdef DEBUG
  freopen("a.in", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> W[i] >> V[i];
  for (int i = 1; i <= 512; ++i) {
    for (int j = 0; j <= 100000; ++j)
      dp[i][j] = dp[i >> 1][j];
    for (int j = 0; j <= 100000; ++j)
      if (j >= V[i])
        dp[i][j] = max(dp[i >> 1][j - V[i]] + W[i], dp[i][j]);
  }
  cin >> q;
  for (int ts = 1; ts <= q; ++ts) {
    int v, l, ans = 0;
    cin >> v >> l;
    if (v <= 512) {
      ans = dp[v][l];
    } else {
      vector<int> idx;
      for (; v > 512; v >>= 1)
        idx.push_back(v);
      int totv, totw;
      for (int i = 0; i < (1 << Sz(idx)); ++i) {
        totv = totw = 0;
        for (int j = 0; j < Sz(idx); ++j)
          if (i >> j & 1)
            totv += V[idx[j]], totw += W[idx[j]];
        if (totv <= l) {
          ans = max(dp[v][l - totv] + totw, ans);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
// 2020.06.14 10:06:45
Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vl = vector<long>;
using vvl = vector<vector<long>>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

long K = 9;
long D = 512;

vvl dp(D + 1, vl(100010, 0));
vector<long> V(262144, 0), W(262144, 1e6);

void dfs(int now, int d) {
  long v = V.at(now - 1);
  long w = W.at(now - 1);
  rep(j, 100001) {
    if (j - w >= 0)
      dp.at(now).at(j) =
          max(dp.at(now / 2).at(j), dp.at(now / 2).at(j - w) + v);
    else
      dp.at(now).at(j) = dp.at(now / 2).at(j);
  }
  if (d == K - 1)
    return;
  dfs(now * 2, d + 1);
  dfs(now * 2 + 1, d + 1);
}

long search(int now, long v, long w, long L) {
  if (now < D) {
    if (w > L)
      return 0;
    else
      return v + dp.at(now).at(L - w);
  }
  return max(search(now / 2, v + V.at(now - 1), w + W.at(now - 1), L),
             search(now / 2, v, w, L));
}

int main() {
  long n;
  cin >> n;
  rep(i, n) cin >> V.at(i) >> W.at(i);
  dfs(1, 0);
  int q;
  cin >> q;
  rep(i, q) {
    long v, l;
    cin >> v >> l;
    if (v < D) {
      cout << dp[v][l] << "\n";
    } else {
      cout << search(v, 0, 0, l) << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vl = vector<long>;
using vvl = vector<vector<long>>;
using pll = pair<long, long>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

long K = 9;
long D = 512;

vvl dp(D + 1, vl(100010, 0));
vector<long> V(262144, 0), W(262144, 1e6);

void dfs(int now, int d) {
  long v = V.at(now - 1);
  long w = W.at(now - 1);
  rep(j, 100001) {
    if (j - w >= 0)
      dp.at(now).at(j) =
          max(dp.at(now / 2).at(j), dp.at(now / 2).at(j - w) + v);
    else
      dp.at(now).at(j) = dp.at(now / 2).at(j);
  }
  if (d == K - 1)
    return;
  dfs(now * 2, d + 1);
  dfs(now * 2 + 1, d + 1);
}

long search(int now, long v, long w, long L) {
  if (now < D) {
    if (w > L)
      return 0;
    else
      return v + dp.at(now).at(L - w);
  }
  return max(search(now / 2, v + V.at(now - 1), w + W.at(now - 1), L),
             search(now / 2, v, w, L));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long n;
  cin >> n;
  rep(i, n) cin >> V.at(i) >> W.at(i);
  dfs(1, 0);
  int q;
  cin >> q;
  rep(i, q) {
    long v, l;
    cin >> v >> l;
    if (v < D) {
      cout << dp[v][l] << "\n";
    } else {
      cout << search(v, 0, 0, l) << "\n";
    }
  }
}
Problem p02648 (C++)

Buggy submission (status: 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;
}

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 (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;

const int SAKAIME = 2048;
const int MAX_W = 300000;
const int MAX_N = 100000;

vector<vector<int>> memo(SAKAIME + 1, vector<int>(MAX_W + 1, 0));
vector<Pii> VW(MAX_N + 1);

int dfs(int v, int l) {
  if (v <= SAKAIME)
    return memo[v][l];

  int t = 0;
  if (l - VW[v].second >= 0)
    t = VW[v].first + dfs(v / 2, l - VW[v].second);
  return max(dfs(v / 2, l), t);
}

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> VW[i].first >> VW[i].second;

  for (int i = 1; i <= min(SAKAIME, N); i++) {
    for (int j = 1; j <= MAX_W; j++) {
      if (j - VW[i].second >= 0)
        memo[i][j] = VW[i].first + memo[i / 2][j - VW[i].second];
      memo[i][j] = max(memo[i / 2][j], memo[i][j]);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, l;
    cin >> v >> l;
    cout << dfs(v, l) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;

const int SAKAIME = 2048;
const int MAX_W = 100000;
const int MAX_N = 300000;

vector<vector<int>> memo(SAKAIME + 1, vector<int>(MAX_W + 1, 0));
vector<Pii> VW(MAX_N + 1);

int dfs(int v, int l) {
  if (v <= SAKAIME)
    return memo[v][l];

  int t = 0;
  if (l - VW[v].second >= 0)
    t = VW[v].first + dfs(v / 2, l - VW[v].second);
  return max(dfs(v / 2, l), t);
}

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> VW[i].first >> VW[i].second;

  for (int i = 1; i <= min(SAKAIME, N); i++) {
    for (int j = 1; j <= MAX_W; j++) {
      if (j - VW[i].second >= 0)
        memo[i][j] = VW[i].first + memo[i / 2][j - VW[i].second];
      memo[i][j] = max(memo[i / 2][j], memo[i][j]);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int v, l;
    cin >> v >> l;
    cout << dfs(v, l) << endl;
  }
}
Problem p02648 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef int I;
#define REP(i, n) for (I i = 0; i < (I)(n); i++)

I n;
I vw[26214][2], dp[1024][100001];
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] or !k) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  // vw.resize(n);
  REP(i, n) {
    I x, y;
    cin >> vw[i][0] >> vw[i][1];
  }
  c1 = 1024;
  c2 = 100000;
  // dp.resize(c1);REP(i,c1)dp[i].resize(c2+1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int I;
#define REP(i, n) for (I i = 0; i < (I)(n); i++)

I n;
I vw[323456][2], dp[1024][100001];
I c1, c2;

void dfs(I i) {
  I j = i / 2;
  if (i > 1) {
    for (I k = c2; k + 1; k--) {
      if (dp[j - 1][k] or !k) {
        I ne = k + vw[i - 1][1];
        if (ne <= c2)
          dp[i - 1][ne] = max(dp[j - 1][k] + vw[i - 1][0], dp[i - 1][ne]);
      }
      dp[i - 1][k] = dp[j - 1][k];
    }
  }
  if (i * 2 <= min(c1, n))
    dfs(i * 2);
  if (i * 2 + 1 <= min(c1, n))
    dfs(i * 2 + 1);
}

signed main() {
  cin >> n;
  // vw.resize(n);
  REP(i, n) {
    I x, y;
    cin >> vw[i][0] >> vw[i][1];
  }
  c1 = 1024;
  c2 = 100000;
  // dp.resize(c1);REP(i,c1)dp[i].resize(c2+1);
  dp[0][vw[0][1]] = vw[0][0];
  dfs(1);
  REP(i, c1) REP(j, c2) dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]);
  I q;
  cin >> q;
  REP(i, q) {
    I v, l;
    cin >> v >> l;
    vector<I> cand;
    while (v > c1) {
      cand.push_back(v);
      v = v / 2;
    }
    I r = cand.size();
    I ans = 0;
    REP(j, 1 << r) {
      I v_sub = 0;
      I w_sub = 0;
      REP(k, r) {
        if (j >> k & 1) {
          v_sub += vw[cand[k] - 1][0];
          w_sub += vw[cand[k] - 1][1];
        }
      }
      if (w_sub <= l)
        ans = max(ans, dp[v - 1][l - w_sub] + v_sub);
    }
    cout << ans << endl;
  }
}