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

#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

int MAX = (int)1e5;
int MAGIC = 1 << 9;

int Par(int node) { return (node - 1) / 2; }

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

  int n;
  cin >> n;
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i].second >> v[i].first;
  }

  vector<vector<int>> dp(MAGIC, vector<int>(MAX + 1));
  for (int i = 0; i <= MAX; ++i) {
    dp[0][i] = v[0].first <= i ? v[0].second : 0;
  }

  for (int i = 1; i < min(MAGIC, n); ++i) {
    int p = Par(i);
    for (int j = 0; j <= MAX; ++j) {
      dp[i][j] = dp[p][j];
      if (v[i].first <= j) {
        dp[i][j] = max(dp[i][j], dp[p][j - v[i].first] + v[i].second);
      }
    }
  }

  int q;
  cin >> q;
  while (q--) {
    int node, val;
    cin >> node >> val;
    --node;

    vector<int> nodes;
    while (node >= MAGIC) {
      nodes.emplace_back(node);
      node = Par(node);
    }

    int ans = 0;
    for (int mask = 0; mask < (1 << (int)nodes.size()); ++mask) {
      int V = 0, W = 0;
      for (int bit = 0; (1 << bit) <= mask; ++bit) {
        if ((mask & (1 << bit)) != 0) {
          W += v[nodes[bit]].first;
          V += v[nodes[bit]].second;
        }
      }

      if (W > val)
        continue;
      ans = max(ans, dp[node][val - W] + V);
    }

    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

void uax(int &a, int b) { a = max(a, b); }

int n;
vector<int> ans;
vector<pair<int, int>> v;
vector<vector<pair<int, int>>> qrs;

int Par(int node) { return (node - 1) / 2; }

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

  cin >> n;
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i].second >> v[i].first;
  }

  qrs.resize(n);
  int q;
  cin >> q;
  vector<map<int, int>> vals(n);
  for (int i = 0; i < q; ++i) {
    int node, val;
    cin >> node >> val;
    --node;
    qrs[node].emplace_back(val, i);
    vals[node][val] = 0;
  }

  for (int i = n - 1; i > 0; --i) {
    for (auto w : vals[i]) {
      vals[Par(i)][w.first] = 0;
      if (v[i].first <= w.first) {
        vals[Par(i)][w.first - v[i].first] = 0;
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    for (auto &w : vals[i]) {
      if (v[i].first <= w.first)
        w.second = v[i].second;

      if (i != 0) {
        uax(w.second, vals[Par(i)][w.first]);
        if (v[i].first <= w.first)
          uax(w.second, vals[Par(i)][w.first - v[i].first] + v[i].second);
      }

      //      dbg() name(1 + i) name(w.first) name(w.second) endl;
    }
  }

  ans.resize(q);

  for (int i = 0; i < n; ++i) {
    for (auto &qr : qrs[i]) {
      ans[qr.second] = vals[i][qr.first];
    }
  }

  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\\n';
  }
}
// void DFS(int node, map<int, int> &par_dp) {
//   if (node >= n) return;
//
//   map<int, int> dp;
//   for (auto &p : par_dp) {
//     uax(dp[p.first], p.second);
//     if (p.first + v[node].first <= (int)1e5) {
//       uax(dp[p.first + v[node].first], p.second + v[node].second);
//     }
//   }
//   int prev = -1;
//   map<int, int> real_dp;
//   for (auto &p : dp) {
//     if (p.second > prev) {
//       real_dp[p.first] = p.second;
//       prev = p.second;
//       // dbg() name(1 + node) name(p.first) name(p.second) endl;
//     } else {
//       // dbg() "Fail: " << name(1 + node) name(p.first) name(p.second) endl;
//     }
//   }
//
//   if (!qrs[node].empty()) {
//     auto it = real_dp.begin();
//     for (auto &qr : qrs[node]) {
//       while (next(it) != real_dp.end() && next(it)->first <= qr.first) {
//         ++it;
//       }
//
//       if (it->first <= qr.first) ans[qr.second] = it->second;
//     }
//   }
//
//   for (int son : {2 * node + 1, 2 * node + 2}) {
//     DFS(son, real_dp);
//   }
// }