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

#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update>
    map_t;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define sz(x) (int)x.size()
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define REV(x) reverse(x.begin(), x.end())
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int n, tt;
const int mxN = 1 << 18;
int val[mxN], w[mxN];
const int mxW = 1e5 + 5;
int dp[(int)(1 << 9) + 5][mxW];

void dfs(int u) {
  if (u == 1) {
    F0R(i, mxW) {
      if (i < w[u])
        continue;
      dp[u][i] = val[u];
    }
  } else {
    F0R(i, mxW) {
      dp[u][i] = dp[u / 2][i];
      if (w[u] <= i)
        dp[u][i] = max(dp[u][i], dp[u / 2][i - w[u]] + val[u]);
    }
  }
  if (2 * u <= min((1 << 9), n))
    dfs(2 * u);
  if (2 * u + 1 <= min((1 << 9), n))
    dfs(2 * u + 1);
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  FOR(i, 1, n + 1)
  cin >> val[i] >> w[i];
  dfs(1);
  cin >> tt;
  while (tt--) {
    int v, W;
    cin >> v >> W;
    if (v <= (1 << 9)) {
      cout << dp[v][W] << "\\n";
      continue;
    }
    vector<int> trace;
    while (v > (1 << 9)) {
      trace.push_back(v);
      v /= 2;
    }
    REV(trace);
    int best = 0;
    F0R(i, 1 << sz(trace)) {
      int totVal = 0, totW = 0;
      int tmp = i;
      F0R(j, sz(trace)) {
        if (tmp % 2) {
          totVal += val[trace[j]];
          totW += w[trace[j]];
        }
        tmp /= 2;
      }
      if (totW > W)
        continue;
      best = max(best, dp[v][W - totW] + totVal);
    }
    cout << best << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
tree<int, int, greater<int>, rb_tree_tag, tree_order_statistics_node_update>
    map_t;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); ++i)
#define sz(x) (int)x.size()
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
#define REV(x) reverse(x.begin(), x.end())
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int n, tt;
const int mxN = 1 << 18;
int val[mxN], w[mxN];
const int mxW = 1e5 + 5;
int dp[(int)(1 << 9) + 5][mxW], dp2[(int)(1 << 9) + 5][mxW];

void dfs(int u) {
  if (u == 1) {
    F0R(i, mxW) {
      if (i < w[u])
        continue;
      dp[u][i] = val[u];
    }
  } else {
    F0R(i, mxW) {
      dp[u][i] = dp[u / 2][i];
      if (w[u] <= i)
        dp[u][i] = max(dp[u][i], dp[u / 2][i - w[u]] + val[u]);
    }
  }
  if (2 * u <= min((1 << 9), n))
    dfs(2 * u);
  if (2 * u + 1 <= min((1 << 9), n))
    dfs(2 * u + 1);
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  FOR(i, 1, n + 1)
  cin >> val[i] >> w[i];
  dfs(1);
  cin >> tt;
  while (tt--) {
    int v, W;
    cin >> v >> W;
    if (v <= (1 << 9)) {
      cout << dp[v][W] << "\\n";
      continue;
    }
    vector<int> trace;
    while (v > (1 << 9)) {
      trace.push_back(v);
      v /= 2;
    }
    trace.push_back(v);
    REV(trace);
    memset(dp2, 0, sizeof(dp2));
    F0R(j, mxW)
    dp2[0][j] = dp[trace[0]][j];
    FOR(i, 1, sz(trace)) {
      F0R(j, mxW) {
        dp2[i][j] = dp2[i - 1][j];
        if (w[trace[i]] <= j)
          dp2[i][j] =
              max(dp2[i][j], dp2[i - 1][j - w[trace[i]]] + val[trace[i]]);
      }
    }
    cout << dp2[sz(trace) - 1][W] << "\\n";
  }

  return 0;
}