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

#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000;
struct query {
  int L, idx;
};
struct sack {
  int v, w;
};
int n, q, v[300000], w[300000], ans[100010];
vector<int> G[300000];
vector<query> qs[300010];
vector<sack> a;
bool used[300010];
int ma[4096];
vector<P> V;
int foo = 10;
int solve(int L, int typ = 0) {
  int N = a.size();
  if (N <= foo) {
    int ans = 0;
    rep(i, (1 << N)) {
      int vsum = 0, wsum = 0;
      rep(j, N) {
        if ((i >> j) & 1) {
          vsum += a[j].v, wsum += a[j].w;
        }
      }
      if (wsum <= L)
        chmax(ans, vsum);
    }
    return ans;
  }
  if (N == foo + 1) {
    V.clear();
    rep(i, (1 << foo)) {
      int vsum = 0, wsum = 0;
      rep(j, foo) {
        if ((i >> j) & 1) {
          vsum += a[j].v, wsum += a[j].w;
        }
      }
      V.push_back({wsum, vsum});
    }
    sort(all(V));
    fill(ma, ma + 4096, 0);
    ma[0] = V[0].second;
    for (int i = 1; i < V.size(); i++) {
      ma[i] = max(ma[i - 1], V[i].second);
    }
  }
  if (typ == 1)
    return -1;
  int ans = 0;
  rep(i, (1 << (N - foo))) {
    int vsum = 0, wsum = 0;
    rep(j, N - foo) {
      if ((i >> j) & 1) {
        vsum += a[foo + j].v, wsum += a[foo + j].w;
      }
    }
    if (wsum <= L) {
      ans = max(
          ans,
          ma[lower_bound(all(V), make_pair(L - wsum, INF)) - V.begin() - 1] +
              vsum);
    }
  }
  return ans;
}
void dfs(int x) {
  used[x] = true;
  a.pb({v[x], w[x]});
  if (a.size() == foo + 1)
    solve(100000, 1);
  for (query Q : qs[x]) {
    ans[Q.idx] = solve(Q.L);
  }
  for (int i : G[x]) {
    if (!used[i]) {
      dfs(i);
      a.pop_back();
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 2; i <= n; i++) {
    int j = i / 2;
    G[i - 1].pb(j - 1);
    G[j - 1].pb(i - 1);
  }
  rep(i, n) cin >> v[i] >> w[i];
  cin >> q;
  rep(i, q) {
    int V, L;
    cin >> V >> L;
    V--;
    qs[V].pb({L, i});
  }
  dfs(0);
  rep(i, q) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000;
struct query {
  int L, idx;
};
struct sack {
  int v, w;
};
int n, q, v[300000], w[300000], ans[100010];
vector<int> G[300000];
vector<query> qs[300010];
vector<sack> a;
bool used[300010];
int ma[1024];
vector<P> V;
int solve(int L, int typ = 0) {
  int N = a.size();
  if (N <= 9) {
    int ans = 0;
    rep(i, (1 << N)) {
      int vsum = 0, wsum = 0;
      rep(j, N) {
        if ((i >> j) & 1) {
          vsum += a[j].v, wsum += a[j].w;
        }
      }
      if (wsum <= L)
        chmax(ans, vsum);
    }
    return ans;
  }
  if (N == 10) {
    V.clear();
    rep(i, (1 << 9)) {
      int vsum = 0, wsum = 0;
      rep(j, 9) {
        if ((i >> j) & 1) {
          vsum += a[j].v, wsum += a[j].w;
        }
      }
      V.push_back({wsum, vsum});
    }
    sort(all(V));
    fill(ma, ma + 1024, 0);
    ma[0] = V[0].second;
    for (int i = 1; i < V.size(); i++) {
      ma[i] = max(ma[i - 1], V[i].second);
    }
  }
  if (typ == 1)
    return -1;
  int ans = 0;
  rep(i, (1 << (N - 9))) {
    int vsum = 0, wsum = 0;
    rep(j, N - 9) {
      if ((i >> j) & 1) {
        vsum += a[9 + j].v, wsum += a[9 + j].w;
      }
    }
    if (wsum <= L) {
      ans = max(
          ans,
          ma[lower_bound(all(V), make_pair(L - wsum, INF)) - V.begin() - 1] +
              vsum);
    }
  }
  return ans;
}
void dfs(int x) {
  used[x] = true;
  a.pb({v[x], w[x]});
  if (a.size() == 10)
    solve(100000, 1);
  for (query Q : qs[x]) {
    ans[Q.idx] = solve(Q.L);
  }
  for (int i : G[x]) {
    if (!used[i]) {
      dfs(i);
      a.pop_back();
    }
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 2; i <= n; i++) {
    int j = i / 2;
    G[i - 1].pb(j - 1);
    G[j - 1].pb(i - 1);
  }
  rep(i, n) cin >> v[i] >> w[i];
  cin >> q;
  rep(i, q) {
    int V, L;
    cin >> V >> L;
    V--;
    qs[V].pb({L, i});
  }
  dfs(0);
  rep(i, q) cout << ans[i] << endl;
  return 0;
}