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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 100005;
const int INF = 1001001001;
const int MOD = 1000000007;

int hight(int n) {
  int res = 0;
  while (n > 0)
    res++, n /= 2;
  return res;
}

int dp[515][MAX];

int main() {
  int N;
  cin >> N;

  vector<int> V(N + 1), W(N + 1);
  rep(i, N) cin >> V[i + 1] >> W[i + 1];

  // 二分木の高さ
  int k = hight(N);

  int m = 1 << (k / 2);
  rep(i, MAX) dp[1][i] = W[1] <= i ? V[1] : 0;

  auto init = [&]() {
    for (int i = 2; i < m; ++i) {
      for (int j = 0; j < MAX; ++j) {
        chmax(dp[i][j], dp[i / 2][j]);
        if (W[i] <= j)
          chmax(dp[i][j], dp[i / 2][j - W[i]] + V[i]);
      }
    }
  };

  init();

  int Q;
  cin >> Q;
  rep(q, Q) {
    int v, l;
    cin >> v >> l;

    if (v < m) {
      cout << dp[v][l] << endl;
      continue;
    }

    vector<P> vec;
    vec.emplace_back(0, 0);
    while (m <= v) {
      vector<P> old;
      swap(old, vec);
      for (auto &p : old) {
        vec.emplace_back(p);
        vec.emplace_back(p.first + V[v], p.second + W[v]);
      }
      v /= 2;
    }

    int ans = 0;
    for (auto &p : vec) {
      if (p.second > l)
        continue;
      chmax(ans, p.first + dp[v][l - p.second]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 100005;
const int INF = 1001001001;
const int MOD = 1000000007;

int hight(int n) {
  int res = 0;
  while (n > 0)
    res++, n /= 2;
  return res;
}

int dp[515][MAX];
int dp2[MAX];

int main() {
  int N;
  cin >> N;

  vector<int> V(N + 1), W(N + 1);
  rep(i, N) cin >> V[i + 1] >> W[i + 1];

  // 二分木の高さ
  int k = hight(N);

  int m = 1 << (k / 2);
  rep(i, MAX) dp[1][i] = W[1] <= i ? V[1] : 0;

  auto init = [&]() {
    for (int i = 2; i < m; ++i) {
      for (int j = 0; j < MAX; ++j) {
        chmax(dp[i][j], dp[i / 2][j]);
        if (W[i] <= j)
          chmax(dp[i][j], dp[i / 2][j - W[i]] + V[i]);
      }
    }
  };

  init();

  int Q;
  cin >> Q;
  rep(q, Q) {
    int v, l;
    cin >> v >> l;

    if (v < m) {
      cout << dp[v][l] << endl;
      continue;
    }

    memset(dp2, 0, sizeof(dp2));
    while (m <= v) {
      repr(i, l + 1) {
        if (i + W[v] <= l)
          chmax(dp2[i + W[v]], dp2[i] + V[v]);
      }
      v /= 2;
    }

    int ans = 0;
    rep(i, l + 1) { chmax(ans, dp[v][i] + dp2[l - i]); }
    cout << ans << endl;
  }
}