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

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("avx,avx2")
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")

// # include <x86intrin.h>
#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;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

int rnd(int l, int r) { return uniform_int_distribution<int>(l, r)(gen); }

int N, V[1 << 18], W[1 << 18];
int dptable[1024][100001];
int dp(int i, int j) {
  if (i < 1024)
    return dptable[i][j];
  if (j < W[i])
    return dp(i / 2, j);
  return max(dp(i / 2, j - W[i]) + V[i], dp(i / 2, j));
}
int main() {
  int Q, v, L;
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i)
    scanf("%d %d", &V[i], &W[i]);
  for (int i = 1; i < 1024; ++i) {
    for (int j = 0; j <= 100000; ++j) {
      if (j < W[i])
        dptable[i][j] = dptable[i >> 1][j];
      else
        dptable[i][j] =
            max(dptable[i >> 1][j], dptable[i >> 1][j - W[i]] + V[i]);
    }
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    scanf("%d %d", &v, &L);
    printf("%d\\n", dp(v, L));
  }
  return 0;
}
// B...a

Error: TLE

Fixed submission:

// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
// #pragma GCC target("avx,avx2")
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")

// # include <x86intrin.h>
#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;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(), x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i, x, y) for (ll i = x; i <= y; i++)
#define FOr(i, x, y) for (ll i = x; i >= y; i--)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red

void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void setIO(string s = "") {
  // cin.exceptions(cin.failbit);
  // throws exception when do smth illegal
  // ex. try to read letter into int
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = (1 << 18);
const int N = 1e5 + 123;
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());

int rnd(int l, int r) { return uniform_int_distribution<int>(l, r)(gen); }

int n, m, k;
const int K = 0; //(1<<9);
int dp[K + 1][N];

int a[mxn], b[mxn];

int main() {
  SpeedForce;
  setIO();

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
  }
  for (int i = 1; i <= K; ++i) {
    for (int w = N - 1; w >= b[i]; --w) {
      dp[i][w] = max(dp[i / 2][w], dp[i / 2][w - b[i]] + a[i]);
    }
  }

  cin >> m;
  while (m--) {
    int v, L;
    cin >> v >> L;
    vector<int> st;
    while (v > K) {
      st.pb(v);
      v /= 2;
    }
    int ans = 0;
    int len = sz(st);
    for (int msk = 0; msk < (1 << len); ++msk) {
      int A = 0, B = 0;
      for (int i = 0; i < len; ++i)
        if (msk & (1 << i)) {
          A += a[st[i]];
          B += b[st[i]];
          if (B > L)
            break;
        }
      if (B > L)
        continue;
      ans = max(ans, dp[v][L - B] + A);
    }
    cout << ans << '\\n';
  }

  return Accepted;
}

// B...a