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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int dp[1 << 9][100010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vi v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  rep(i, 1 << 9) rep(j, 100010) dp[i][j] = -inf;
  dp[0][0] = 0;
  rep2(i, 1, min(1 << 9, n + 1)) rep(j, 100010) {
    int par = i / 2;
    chmax(dp[i][j], dp[par][j]);
    if (j >= w[i - 1])
      chmax(dp[i][j], dp[par][j - w[i - 1]] + v[i - 1]);
  }
  rep(i, 1 << 9) rep(j, 100009) chmax(dp[i][j + 1], dp[i][j]);
  int q;
  cin >> q;
  rep(_, q) {
    int u, l;
    cin >> u >> l;
    if (u < 1 << 9) {
      cout << dp[u][l] << "\\n";
      continue;
    }
    vi can;
    while (u >= 1 << 9) {
      can.pb(u);
      u /= 2;
    }
    int ans = 0;
    int sz = can.size();
    rep(bit, 1 << sz) {
      int nw = 0, nv = 0;
      rep(i, sz) if (bit >> i & 1) {
        nw += w[can[i] - 1];
        nv += v[can[i] - 1];
      }
      if (nw > l)
        continue;
      chmax(ans, nv + dp[u][l - nw]);
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vi v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  int q;
  cin >> q;
  rep(_, q) {
    int u, l;
    cin >> u >> l;
    u--;
    vi can;
    can.pb(u);
    while (u > 0) {
      u = (u - 1) / 2;
      can.pb(u);
    }
    int sz = can.size();
    vp fi, se;
    rep(bit, 1 << (sz / 2 + 1)) {
      int sv = 0, sw = 0;
      rep(i, sz / 2 + 1) {
        if (bit >> i & 1) {
          sv += v[can[i]];
          sw += w[can[i]];
        }
        if (sw > l)
          continue;
        fi.eb(sw, sv);
      }
    }
    rep(bit, 1 << ((sz - (sz / 2 + 1)))) {
      int sv = 0, sw = 0;
      rep(i, sz - (sz / 2 + 1)) {
        if (bit >> i & 1) {
          sv += v[can[i + sz / 2 + 1]];
          sw += w[can[i + sz / 2 + 1]];
        }
        if (sw > l)
          continue;
        se.eb(sw, sv);
      }
    }
    sort(all(se));
    int ans = 0;
    if (se.size()) {
      rep(i, se.size() - 1) chmax(se[i + 1].second, se[i].second);
      ans = se.back().second;
    }
    for (auto p : fi) {
      auto it = upper_bound(all(se), l - p.first, [](const int &i, const P &p) {
        return i < p.first;
      });
      if (it == se.begin()) {
        chmax(ans, p.second);
        continue;
      }
      it--;
      chmax(ans, p.second + it->second);
    }
    cout << ans << "\\n";
  }
}