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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> vs(N + 1), ws(N + 1);
  FOR(i, 1, N + 1) { cin >> vs[i] >> ws[i]; }

  int Q;
  cin >> Q;
  vector<pair<int, int>> qs(Q);
  vector<int> ms(N + 1);
  F0R(i, Q) {
    cin >> qs[i].first >> qs[i].second;
    chmax(ms[qs[i].first], qs[i].second);
  }

  RF0(i, N + 1) { chmax(ms[i / 2], ms[i]); }
  // Print(ms);

  vector<vector<int>> dp(2 << 9);
  dp[0].resize(ms[0] + 1);
  auto dfs = [&](auto dfs, int v, int depth) -> void {
    if (depth > 9)
      return;
    if (v > N || ms[v] == 0)
      return;

    // cout << v << "," << ms[v] << newl;
    dp[v].resize(ms[v] + 1);
    int p = v >> 1;
    copy(dp[p].begin(), dp[p].begin() + min(ws[v], ms[v] + 1), dp[v].begin());

    FOR(i, ws[v], ms[v] + 1) {
      dp[v][i] = max(dp[p][i], dp[p][i - ws[v]] + vs[v]);
    }
    // Print(dp[depth]);
    dfs(dfs, v * 2, depth + 1);
    dfs(dfs, v * 2 + 1, depth + 1);
  };
  dfs(dfs, 1, 0);

  F0R(i, Q) {
    int result = -1;
    if (qs[i].first < dp.size()) {
      result = dp[qs[i].first][qs[i].second];
    } else {
      auto dfs = [&](auto dfs, int v, int val, int w) -> void {
        if (v < dp.size()) {
          if (w <= qs[i].second) {
            chmax(result, dp[v][qs[i].second - w] + val);
          }
          return;
        }

        dfs(dfs, v >> 1, val + vs[v], w + ws[v]);
        dfs(dfs, v >> 1, val, w);
      };
      dfs(dfs, qs[i].first, 0, 0);
    }
    cout << result << newl;
  }
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

constexpr char newl = '\\n';
constexpr double eps = 1e-10;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << '\\n';
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Print(vector<T> v) {
  F0R(i, v.size()) { cout << v[i] << ' '; }
  cout << newl;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> vs(N + 1), ws(N + 1);
  FOR(i, 1, N + 1) { cin >> vs[i] >> ws[i]; }

  int Q;
  cin >> Q;
  vector<vector<pair<int, int>>> qs(N + 1);
  vector<int> rs(Q);
  vector<int> ms(N + 1);
  F0R(i, Q) {
    int v, L;
    cin >> v >> L;
    qs[v].emplace_back(i, L);
    chmax(ms[v], L);
  }

  RF0(i, N + 1) { chmax(ms[i / 2], ms[i]); }
  // Print(ms);

  vector<vector<int>> dp(20, vector<int>(ms[0] + 1));
  dp[0].resize(ms[0] + 1);
  vector<int> ru(20);
  auto dfs = [&](auto dfs, int v, int depth) -> void {
    if (v > N || ms[v] == 0)
      return;

    // cout << v << "," << ms[v] << newl;
    int p = depth - 1;
    int copySize = min(ws[v], ms[v] + 1);
    if (ru[depth] >= copySize) {
      ru[depth] = copySize;
    } else {
      copy(dp[p].begin() + ru[depth], dp[p].begin() + copySize,
           dp[depth].begin() + ru[depth]);
    }

    FOR(i, ws[v], ms[v] + 1) {
      dp[depth][i] = max(dp[p][i], dp[p][i - ws[v]] + vs[v]);
    }
    F0R(i, qs[v].size()) { rs[qs[v][i].first] = dp[depth][qs[v][i].second]; }
    // Print(dp[depth]);
    chmin(ru[depth + 1], ru[depth]);
    dfs(dfs, v * 2, depth + 1);
    dfs(dfs, v * 2 + 1, depth + 1);
  };
  dfs(dfs, 1, 1);

  F0R(i, Q) { cout << rs[i] << newl; }
}
#endif