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

/// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
// #define int long long
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define ar array
// #define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 1 << 18;
int n, q, p[MAXN + 1], val[MAXN + 1], w[MAXN + 1], done[MAXN];
ll dp[(1 << 10) + 1][200020];

signed main() {
  IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n;
  // n = (1<<18)-1;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> w[i];
    // val[i] = rand()%100000 + 1;
    // w[i] = rand()%100000 + 1;
    if (i != 1) {
      p[i] = i / 2;
    }
  }
  for (int i = 1; i < (1 << 10); i++) {
    vpi v;
    int cur = i;
    while (cur) {
      v.pb({val[cur], w[cur]});
      cur /= 2;
    }
    int N = v.size();
    for (int msk = 0; msk < (1 << N); msk++) {
      int W = 0;
      ll V = 0;
      for (int j = 0; j < N; j++) {
        if (msk & (1 << j)) {
          W += v[j].second;
          V += v[j].first;
        }
        if (W >= 100000)
          break;
      }
      if (W <= 100000) {
        dp[i][W] = max(dp[i][W], V);
      }
    }
    for (int W = 1; W <= 100000; W++) {
      dp[i][W] = max(dp[i][W], dp[i][W - 1]);
    }
    done[i] = 1;
  }
  cin >> q;
  // q = 100000;
  while (q--) {
    int u, l;
    cin >> u >> l;
    // u = n;
    // l = rand()%100000;
    ll ans = 0;
    int cur = u;
    vpi v;
    while (!done[cur]) {
      v.pb({val[cur], w[cur]});
      cur /= 2;
      // cerr << cur << endl;
    }
    int N = v.size();
    for (int i = 0; i < (1 << N); i++) {
      int W = 0;
      ll V = 0;
      for (int j = 0; j < N; j++) {
        if (i & (1 << j)) {
          W += v[j].second;
          V += v[j].first;
        }
        if (W >= l)
          break;
      }
      if (W <= l) {
        ans = max(ans, V + dp[cur][l - W]);
      }
    }
    cout << ans << endl;
  }

  return 0;
}
///....


Error: TLE

Fixed submission:

/// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
// #define int long long
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define ar array
// #define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 1 << 18;
int n, q, p[MAXN + 1], val[MAXN + 1], w[MAXN + 1], done[MAXN];
ll dp[(1 << 9) + 1][200020];

signed main() {
  IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n;
  // n = (1<<18)-1;
  for (int i = 1; i <= n; i++) {
    cin >> val[i] >> w[i];
    // val[i] = rand()%100000 + 1;
    // w[i] = rand()%100000 + 1;
    if (i != 1) {
      p[i] = i / 2;
    }
  }
  for (int i = 1; i < (1 << 9); i++) {
    vpi v;
    int cur = i;
    while (cur) {
      v.pb({val[cur], w[cur]});
      cur /= 2;
    }
    int N = v.size();
    for (int msk = 0; msk < (1 << N); msk++) {
      ll W = 0, V = 0;
      for (int j = 0; j < N; j++) {
        if (msk & (1 << j)) {
          W += v[j].second;
          V += v[j].first;
        }
        if (W >= 100000)
          break;
      }
      if (W <= 100000) {
        dp[i][W] = max(dp[i][W], V);
      }
    }
    for (int W = 1; W <= 100000; W++) {
      dp[i][W] = max(dp[i][W], dp[i][W - 1]);
    }
    done[i] = 1;
  }
  cin >> q;
  // q = 100000;
  while (q--) {
    int u, l;
    cin >> u >> l;
    // u = n;
    // l = rand()%100000 + 1;
    ll ans = 0;
    int cur = u;
    vpi v;
    while (!done[cur]) {
      v.pb({val[cur], w[cur]});
      cur /= 2;
      // cerr << cur << endl;
    }
    int N = v.size();
    for (int i = 0; i < (1 << N); i++) {
      ll W = 0, V = 0;
      for (int j = 0; j < N; j++) {
        if (i & (1 << j)) {
          W += v[j].second;
          V += v[j].first;
        }
        if (W >= l)
          break;
      }
      if (W <= l) {
        ans = max(ans, V + dp[cur][l - W]);
      }
    }
    cout << ans << endl;
  }

  return 0;
}
///....