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

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

ll dp[600][100100];
int k = 9;

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

  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  rep(i, 600) rep(j, 100100) dp[i][j] = 0;
  for (int cur = 1; cur <= min((1 << k), n); cur++) {
    int par = cur / 2;
    for (int i = 100000; i >= 0; i--) {
      chmax(dp[cur][i], dp[par][i]);
      if (i + w[cur] < 100100)
        chmax(dp[cur][i + w[cur]], dp[par][i] + v[cur]);
    }
  }

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

    if (node < (1 << k)) {
      cout << dp[node][l] << endl;
      continue;
    }
    vector<int> vv, ww;
    for (; node >= (1 << k); node >>= 1)
      vv.push_back(v[node]), ww.push_back(w[node]);

    int t = (int)vv.size();
    ll ans = 0;
    for (int bit = 0; bit < (1 << t); bit++) {
      ll wsum = 0, vsum = 0;
      rep(i, t) if ((bit >> i) & 1) wsum += ww[i], vsum += vv[i];
      if (wsum <= l)
        chmax(ans, vsum + dp[node][l - wsum]);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(v)                                                               \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

// ナップサック問題，半分全列挙
ll knapsack_bruteforce(ll W, vector<ll> &v, vector<ll> &w) {
  assert(v.size() == w.size());
  vector<ll> v1, v2, w1, w2;
  rep(i, v.size()) {
    if (i % 2)
      v2.push_back(v[i]), w2.push_back(w[i]);
    else
      v1.push_back(v[i]), w1.push_back(w[i]);
  }
  int n = (int)v1.size(), m = (int)v2.size();

  auto fill = [&](vector<ll> &v, vector<ll> &w) {
    int k = (int)v.size();
    vector<pair<ll, ll>> al; // w,v
    rep(bit, 1 << k) {
      ll wsum = 0, vsum = 0;
      rep(i, n) if ((bit >> i) & 1) wsum += w[i], vsum += v[i];
      if (wsum <= W)
        al.push_back(make_pair(wsum, vsum));
    }
    sort(ALL(al));
    vector<pair<ll, ll>> ret;
    ret.push_back(al[0]);
    for (int i = 1; i < al.size(); i++)
      if (ret.back().second < al[i].second)
        ret.push_back(al[i]);
    return ret;
  };

  auto a1 = fill(v1, w1), a2 = fill(v2, w2);
  ll ret = 0;
  for (auto x : a1) {
    ll rem = W - x.first;
    auto ite = lower_bound(ALL(a2), make_pair(rem + 1, -1ll));
    ite = prev(ite);
    chmax(ret, x.second + ite->second);
  }
  return ret;
}

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

  int n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  int q;
  cin >> q;
  while (q--) {
    int u;
    ll l;
    cin >> u >> l;

    vector<ll> wp, vp;
    for (int i = 0; u; i++, u >>= 1) {
      wp.push_back(w[u]);
      vp.push_back(v[u]);
    }
    // cout<<u<<" "<<l<<endl;
    // debug(wp);
    // debug(vp);
    cout << knapsack_bruteforce(l, vp, wp) << endl;
  }
  return 0;
}