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

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using pii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define fore(x, c) for(auto &&x : c)
#define rep(i, a, n) for(int i = a, i##len = (int)(n); i < i##len; ++i)
#define rrep(i, a, n) for(int i = (int)(n - 1); i >= a; --i)
#define sz(c) ((int)c.size())
#define contains(c, x) (c.find(x) != end(c))
#define inseg(l, x, r) ((l) <= (x) && (x) < (r))
#define dump(...)
#define pb push_back
#define eb emplace_back
#define _ 0
const signed INF_ = 1001001001; const long long INF = 1001001001001001001LL;
const int DX[9] = {0,1,0,-1,1,1,-1,-1,0}, DY[9] = {-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
  is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a, const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a, const U &b){return a>b?a=b,1:0;}
template <class T> void psum(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap = priority_queue<T, deque<T>, greater<T>>;
struct before_main_function {
  before_main_function() {
      cin.tie(nullptr); ios::sync_with_stdio(0);
      cout << setprecision(15) << fixed;
  }
} before_main_function;
// clang-format on
//------------------8<------------------------------------8<--------------------

signed main() {
  int N;
  cin >> N;
  vi V(N + 1), W(N + 1);
  rep(i, 0, N) cin >> V[i + 1] >> W[i + 1];

  int M = min(1LL << 9, N);
  vvi dp(M + 1, vi(100005));
  auto dfs = [&](auto &self, int v) -> void {
    if (v > M)
      return;
    if (v == 1) {
      rep(i, 0, 100005) {
        if (i >= W[v])
          chmax(dp[v][i], V[v]);
      }
    } else {
      rep(i, 0, 100005) {
        chmax(dp[v][i], dp[v / 2][i]);
        if (i - W[v] >= 0)
          chmax(dp[v][i], dp[v / 2][i - W[v]] + V[v]);
      }
    }
    self(self, v * 2 + 0);
    self(self, v * 2 + 1);
  };
  dfs(dfs, 1);

  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int v, l;
    cin >> v >> l;
    int ans = 0;
    auto dfs2 = [&](auto &self, int v, int vs, int ws) -> void {
      if (v <= M) {
        if (l >= ws)
          chmax(ans, dp[v][l - ws] + vs);
      } else {
        self(self, v / 2, vs, ws);
        self(self, v / 2, vs + V[v], ws + W[v]);
      }
    };
    dfs2(dfs2, v, 0, 0);
    cout << ans << endl;
  }
  return (0 ^ _ ^ 0);
}


Error: TLE

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using pii = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(c) begin(c), end(c)
#define rall(c) rbegin(c), rend(c)
#define fore(x, c) for(auto &&x : c)
#define rep(i, a, n) for(int i = a, i##len = (int)(n); i < i##len; ++i)
#define rrep(i, a, n) for(int i = (int)(n - 1); i >= a; --i)
#define sz(c) ((int)c.size())
#define contains(c, x) (c.find(x) != end(c))
#define inseg(l, x, r) ((l) <= (x) && (x) < (r))
#define dump(...)
#define pb push_back
#define eb emplace_back
#define _ 0
const signed INF_ = 1001001001; const long long INF = 1001001001001001001LL;
const int DX[9] = {0,1,0,-1,1,1,-1,-1,0}, DY[9] = {-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
  is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a, const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a, const U &b){return a>b?a=b,1:0;}
template <class T> void psum(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap = priority_queue<T, deque<T>, greater<T>>;
struct before_main_function {
  before_main_function() {
      cin.tie(nullptr); ios::sync_with_stdio(0);
      cout << setprecision(15) << fixed;
  }
} before_main_function;
// clang-format on
//------------------8<------------------------------------8<--------------------

signed main() {
  int N;
  cin >> N;
  vi V(N + 1), W(N + 1);
  rep(i, 0, N) cin >> V[i + 1] >> W[i + 1];

  int M = min(1LL << 9, N);
  vvi dp(M + 1, vi(100005));
  auto dfs = [&](auto &self, int v, int vs, int ws) -> void {
    if (v > M)
      return;
    if (v == 1) {
      rep(i, 0, 100005) {
        if (i >= W[v])
          chmax(dp[v][i], V[v]);
      }
    } else {
      rep(i, 0, 100005) {
        chmax(dp[v][i], dp[v / 2][i]);
        if (i - W[v] >= 0)
          chmax(dp[v][i], dp[v / 2][i - W[v]] + V[v]);
      }
    }
    self(self, v * 2 + 0, vs, ws);
    self(self, v * 2 + 0, vs + V[v], ws + W[v]);
    self(self, v * 2 + 1, vs, ws);
    self(self, v * 2 + 1, vs + V[v], ws + W[v]);
  };
  dfs(dfs, 1, 0, 0);

  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int v, l;
    cin >> v >> l;
    int ans = 0;
    auto dfs2 = [&](auto &self, int v, int vs, int ws) -> void {
      if (v <= M) {
        if (l >= ws)
          chmax(ans, dp[v][l - ws] + vs);
      } else {
        self(self, v / 2, vs, ws);
        self(self, v / 2, vs + V[v], ws + W[v]);
      }
    };
    dfs2(dfs2, v, 0, 0);
    cout << ans << endl;
  }
  return (0 ^ _ ^ 0);
}