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

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

using ll = long long;

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
const ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> nodes(n + 1), queries;
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    nodes[i + 1] = {v, w};
  }
  int q;
  cin >> q;
  int lmax = 0;
  rep(i, q) {
    int v, l;
    cin >> v >> l;
    lmax = max(l, lmax);
    queries.emplace_back(v, l);
  }
  const int n1 = (1 << 10) - 1;
  vector<vector<int>> dps(n1 + 1, vector<int>(lmax + 1, 0));
  rng(i, 1, min(n1, n) + 1) {
    int v = nodes[i].first;
    int w = nodes[i].second;
    rep(k, lmax + 1) {
      dps[i][k] = dps[i / 2][k];
      if (k >= w)
        dps[i][k] = max(dps[i][k], dps[i / 2][k - w] + v);
    }
  }
  vector<int> ans(q, 0);
  rep(i, q) {
    int v = queries[i].first;
    int l = queries[i].second;
    if (n1 < v) {
      int lv = v, cnt = 0;
      while (lv > n1) {
        lv /= 2;
        cnt++;
      }
      rep(j, 1 << cnt) {
        int b = j, ltv = v;
        int tv = 0, tw = 0;
        rep(k, cnt) {
          if (b & 1) {
            tv += nodes[ltv].first;
            tw += nodes[ltv].second;
          }
          b >>= 1;
          ltv /= 2;
        }
        if (tw <= l)
          ans[i] = max(ans[i], tv + dps[lv][l - tw]);
      }
    } else {
      ans[i] = dps[v][l];
    }
  }
  for (int a : ans)
    cout << a << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using ll = long long;

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
const ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> nodes(n + 1), queries;
  rep(i, n) {
    int v, w;
    cin >> v >> w;
    nodes[i + 1] = {v, w};
  }
  int q;
  cin >> q;
  int lmax = 0;
  rep(i, q) {
    int v, l;
    cin >> v >> l;
    lmax = max(l, lmax);
    queries.emplace_back(v, l);
  }
  const int n1 = (1 << 9) - 1;
  vector<vector<int>> dps(n1 + 1, vector<int>(lmax + 1, 0));
  rng(i, 1, min(n1, n) + 1) {
    int v = nodes[i].first;
    int w = nodes[i].second;
    rep(k, lmax + 1) {
      dps[i][k] = dps[i / 2][k];
      if (k >= w)
        dps[i][k] = max(dps[i][k], dps[i / 2][k - w] + v);
    }
  }
  vector<int> ans(q, 0);
  rep(i, q) {
    int v = queries[i].first;
    int l = queries[i].second;
    if (n1 < v) {
      int lv = v, cnt = 0;
      while (lv > n1) {
        lv /= 2;
        cnt++;
      }
      rep(j, 1 << cnt) {
        int b = j, ltv = v;
        int tv = 0, tw = 0;
        rep(k, cnt) {
          if (b & 1) {
            tv += nodes[ltv].first;
            tw += nodes[ltv].second;
          }
          b >>= 1;
          ltv /= 2;
        }
        if (tw <= l)
          ans[i] = max(ans[i], tv + dps[lv][l - tw]);
      }
    } else {
      ans[i] = dps[v][l];
    }
  }
  for (int a : ans)
    cout << a << endl;
  return 0;
}