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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
long double eps = 1e-9;
long double pi = acos(-1);

int dp[(1 << 9) + 1][100001];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  int a[n + 1], b[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  int m = 9;
  for (int i = 1; i <= (1 << m); i++) {
    if (i > n)
      break;
    int nx = i / 2;
    for (int j = 0; j <= 100000; j++) {
      chmax(dp[i][j], dp[nx][j]);
      if (j + b[i] <= 100000)
        chmax(dp[i][j + b[i]], dp[nx][j] + a[i]);
      if (j)
        chmax(dp[i][j], dp[i][j - 1]);
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, l;
    cin >> v >> l;
    if (v <= (1 << m)) {
      cout << dp[v][l] << "\\n";
      continue;
    }
    // v,v/2,...
    int ans = 0;
    queue<array<int, 3>> que;
    que.push({v, 0, 0});
    while (que.size()) {
      array<int, 3> now = que.front();
      que.pop();
      int w = now[1], val = now[2], p = now[0];
      if (p <= (1 << m)) {
        chmax(ans, dp[p][l - w] + val);
      } else {
        if (w + b[p] <= l)
          que.push({p / 2, w + b[p], val + a[p]});
        que.push({p / 2, w, val});
      }
    }
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
long double eps = 1e-9;
long double pi = acos(-1);

int readInt() {
  bool minus = false;
  int result = 0;
  char ch;
  ch = getchar();
  while (true) {
    if (ch == '-')
      break;
    if (ch >= '0' && ch <= '9')
      break;
    ch = getchar();
  }
  if (ch == '-')
    minus = true;
  else
    result = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9')
      break;
    result = result * 10 + (ch - '0');
  }
  if (minus)
    return -result;
  else
    return result;
}

int dp[(1 << 8) + 1][100001];

signed main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // cout << fixed << setprecision(20);

  int n;
  cin >> n;
  int a[n + 1], b[n + 1];
  for (int i = 1; i <= n; i++) {
    a[i] = readInt();
    b[i] = readInt();
  }
  int m = 8;
  for (int i = 1; i <= (1 << m); i++) {
    if (i > n)
      break;
    int nx = i / 2;
    for (int j = 0; j <= 100000; j++) {
      chmax(dp[i][j], dp[nx][j]);
      if (j + b[i] <= 100000)
        chmax(dp[i][j + b[i]], dp[nx][j] + a[i]);
      if (j)
        chmax(dp[i][j], dp[i][j - 1]);
    }
  }
  int q;
  cin >> q;
  int v, l;
  while (q--) {
    v = readInt(), l = readInt();
    if (v <= (1 << m)) {
      printf("%d\\n", dp[v][l]);
      // cout << dp[v][l] << "\\n";
      continue;
    }
    // v,v/2,...
    int ans = 0;
    queue<array<int, 3>> que;
    que.push({v, 0, 0});
    while (que.size()) {
      array<int, 3> now = que.front();
      que.pop();
      int w = now[1], val = now[2], p = now[0];
      if (p <= (1 << m)) {
        chmax(ans, dp[p][l - w] + val);
      } else {
        if (w + b[p] <= l)
          que.push({p / 2, w + b[p], val + a[p]});
        que.push({p / 2, w, val});
      }
    }
    printf("%d\\n", ans);
    // cout << ans << "\\n";
  }
}