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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

vector<ll> v, w;
vector<vector<pair<ll, ll>>> query;
vector<ll> query_count;
vector<ll> ans;

void solve(vector<ll> &path, const vector<ll> &dp) {
  const ll cur = path.back();
  const ll nxt1 = 2 * (cur + 1) - 1;
  const ll nxt2 = 2 * (cur + 1);

  if (path.size() <= 9) {
    const ll vi = v.at(cur);
    const ll wi = w.at(cur);
    vector<ll> dp2(dp);
    RREP(i, dp.size()) {
      if (i - wi >= 0) {
        dp2.at(i) = max(dp2.at(i), dp2.at(i - wi) + vi);
      }
    }
    for (const auto p : query.at(cur)) {
      ans.at(p.first) = dp2.at(p.second);
    }

    if (nxt1 < v.size() && query_count.at(nxt1) > 0) {
      path.push_back(nxt1);
      solve(path, dp2);
      path.pop_back();
    }
    if (nxt2 < v.size() && query_count.at(nxt2) > 0) {
      path.push_back(nxt2);
      solve(path, dp2);
      path.pop_back();
    }
  } else {
    if (query.at(cur).size()) {
      vector<ll> tmp(path.begin() + 9, path.end());
      for (const auto p : query.at(cur)) {
        ll tmpans = 0;
        REP(i, (1 << tmp.size())) {
          ll tmpv = 0, tmpw = 0;
          REP(j, tmp.size()) {
            if ((i >> j) & 1) {
              tmpv += v.at(tmp.at(j));
              tmpw += w.at(tmp.at(j));
            }
          }
          if (tmpw > p.second) {
            continue;
          }
          tmpans = max(tmpans, tmpv + dp.at(p.second - tmpw));
        }
        ans.at(p.first) = tmpans;
      }
    }
    if (nxt1 < v.size() && query_count.at(nxt1) > 0) {
      path.push_back(nxt1);
      solve(path, dp);
      path.pop_back();
    }
    if (nxt2 < v.size() && query_count.at(nxt2) > 0) {
      path.push_back(nxt2);
      solve(path, dp);
      path.pop_back();
    }
  }
}

int main() {
  ll n;
  cin >> n;
  v.resize(n);
  w.resize(n);
  REP(i, n) { cin >> v.at(i) >> w.at(i); }
  query.resize(n);

  query_count.resize(n);
  fill(ALL(query_count), 0);

  ll q;
  cin >> q;
  ll maxl = 0;
  REP(i, q) {
    ll vi, li;
    cin >> vi >> li;
    vi--;
    query.at(vi).emplace_back(i, li);
    query_count.at(vi)++;
    maxl = max(maxl, li);
  }
  ans.resize(q);

  RREP(i, n) {
    const ll nxt1 = 2 * (i + 1) - 1;
    const ll nxt2 = 2 * (i + 1);
    if (nxt1 < n) {
      query_count.at(i) += query_count.at(nxt1);
    }
    if (nxt2 < n) {
      query_count.at(i) += query_count.at(nxt2);
    }
  }

  vector<ll> dp(maxl + 1, 0);
  vector<ll> path(1, 0);
  solve(path, dp);

  REP(i, q) { cout << ans.at(i) << endl; }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

vector<ll> v, w;
vector<vector<pair<ll, ll>>> query;
vector<ll> query_count;
vector<ll> ans;

void solve(ll cur, vector<ll> &dp) {
  const ll nxt1 = 2 * (cur + 1) - 1;
  const ll nxt2 = 2 * (cur + 1);

  const ll vi = v.at(cur);
  const ll wi = w.at(cur);
  vector<pair<ll, ll>> chpt;
  RREP(i, dp.size()) {
    if (i + wi < dp.size() && (i == 0 || dp.at(i) > 0) &&
        dp.at(i + wi) < dp.at(i) + vi) {
      chpt.emplace_back(i + wi, dp.at(i + wi));
      dp.at(i + wi) = dp.at(i) + vi;
    }
  }

  ll maxl = 0;
  for (const auto p : query.at(cur)) {
    maxl = max(maxl, p.second);
  }
  vector<ll> maxval(maxl + 1);
  maxval.at(0) = dp.at(0);
  for (ll i = 1; i <= maxl; ++i) {
    maxval.at(i) = max(maxval.at(i - 1), dp.at(i));
  }
  for (const auto p : query.at(cur)) {
    const ll qi = p.first;
    const ll li = p.second;
    ans.at(qi) = maxval.at(li);
  }

  if (nxt1 < v.size() && query_count.at(nxt1) > 0) {
    solve(nxt1, dp);
  }
  if (nxt2 < v.size() && query_count.at(nxt2) > 0) {
    solve(nxt2, dp);
  }

  for (const auto p : chpt) {
    dp.at(p.first) = p.second;
  }
}

int main() {
  ll n;
  cin >> n;
  v.resize(n);
  w.resize(n);
  REP(i, n) { cin >> v.at(i) >> w.at(i); }
  query.resize(n);

  query_count.resize(n);
  fill(ALL(query_count), 0);

  ll q;
  cin >> q;
  ll maxl = 0;
  REP(i, q) {
    ll vi, li;
    cin >> vi >> li;
    vi--;
    query.at(vi).emplace_back(i, li);
    query_count.at(vi)++;
    maxl = max(maxl, li);
  }
  ans.resize(q);

  RREP(i, n) {
    const ll nxt1 = 2 * (i + 1) - 1;
    const ll nxt2 = 2 * (i + 1);
    if (nxt1 < n) {
      query_count.at(i) += query_count.at(nxt1);
    }
    if (nxt2 < n) {
      query_count.at(i) += query_count.at(nxt2);
    }
  }

  vector<ll> dp(maxl + 1, 0);
  solve(0, dp);

  REP(i, q) { cout << ans.at(i) << endl; }
  return 0;
}