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

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;
const ll INF = 1000000009000000009;

int n, qq;
int v[N], w[N];

vector<pair<int, ll>> vv[N];

void dfs(int x, int d) {
  if (x > n)
    return;
  if (d > 11)
    return;
  vv[x] = vv[x / 2];
  for (int i = 0; i < vv[x / 2].size(); ++i) {
    vv[x].push_back(
        m_p(min(N, vv[x / 2][i].fi + v[x]), vv[x / 2][i].se + w[x]));
  }
  sort(vv[x].begin(), vv[x].end());
  for (int i = 1; i < vv[x].size(); ++i)
    vv[x][i].se = max(vv[x][i].se, vv[x][i - 1].se);
  dfs(x * 2, d + 1);
  dfs(x * 2 + 1, d + 1);
}

void solv() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &w[i], &v[i]);
  vv[0].push_back(m_p(0, 0));
  dfs(1, 0);
  // return;
  scanf("%d", &qq);
  while (qq--) {
    int g, m;
    scanf("%d%d", &g, &m);
    vector<pair<int, int>> v;
    while (g) {
      if (!vv[g].empty())
        break;
      v.push_back(m_p(::v[g], w[g]));
      g /= 2;
    }
    ll ans = 0;
    for (int x = 0; x < (1 << sz(v)); ++x) {
      int sv = 0;
      ll sw = 0;
      for (int i = 0; i < sz(v); ++i) {
        if ((x & (1 << i))) {
          sv += v[i].fi;
          sw += v[i].se;
          sv = min(sv, N);
        }
      }
      int i = upper_bound(all(vv[g]), m_p(m - sv, INF)) - vv[g].begin() - 1;
      if (i >= 0)
        ans = max(ans, sw + vv[g][i].se);
    }
    printf("%lld\\n", ans);
  }
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false), cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 300005;
const ll INF = 1000000009000000009;

int n, qq;
int v[N], w[N];

void solv() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &w[i], &v[i]);
  scanf("%d", &qq);
  while (qq--) {
    int x, m;
    scanf("%d%d", &x, &m);
    vector<pair<int, int>> v;
    while (x) {
      v.push_back(m_p(::v[x], w[x]));
      x /= 2;
    }
    vector<pair<int, ll>> vv;
    for (int x = 0; x < (1 << (sz(v) / 2)); ++x) {
      int sv = 0;
      ll sw = 0;
      for (int i = 0; i < sz(v) / 2; ++i) {
        if ((x & (1 << i))) {
          sv += v[i].fi;
          sw += v[i].se;
          sv = min(sv, N);
        }
      }
      vv.push_back(m_p(sv, sw));
    }
    sort(all(vv));
    for (int i = 1; i < vv.size(); ++i) {
      vv[i].se = max(vv[i].se, vv[i - 1].se);
    }
    ll ans = 0;
    for (int x = 0; x < (1 << (sz(v) / 2 + sz(v) % 2)); ++x) {
      int sv = 0;
      ll sw = 0;
      for (int i = 0; i < sz(v) / 2 + sz(v) % 2; ++i) {
        if ((x & (1 << i))) {
          sv += v[i + sz(v) / 2].fi;
          sw += v[i + sz(v) / 2].se;
          sv = min(sv, N);
        }
      }
      int u = upper_bound(all(vv), m_p(m - sv, INF)) - vv.begin() - 1;
      if (u >= 0)
        ans = max(ans, vv[u].se + sw);
    }
    printf("%lld\\n", ans);
  }
}

int main() {
#ifdef SOMETHING
  freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // SOMETHING
  // ios_base::sync_with_stdio(false), cin.tie(0);
  solv();
  return 0;
}

// while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}