Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;

int ipow(int a, int b) {
  int r = 1;
  while (b >= 1) {
    if (b % 2 == 1)
      r *= a;
    b /= 2;
    a *= a;
  }
  return r;
}

int main() {
  int N;
  cin >> N;
  vector<int> V, W;
  for (int i = 1; i < N + 1; i++) {
    int v, w;
    cin >> v >> w;
    V.push_back(v);
    W.push_back(w);
  }
  vector<int> qV, qL;
  int Q;
  cin >> Q;
  int maxL = 0;
  for (int i = 0; i < Q; i++) {
    int v, L;
    cin >> v >> L;
    maxL = max(maxL, L);
    v--;
    qV.push_back(v);
    qL.push_back(L);
  }
  vector<vector<int>> dp(min(N, 256), vector<int>(maxL + 1, -1));
  for (int i = 0; i < dp.size(); i++) {
    for (int j = 0; j <= maxL; j++) {
      if (i == 0) {
        dp[i][j] = W[0] <= j ? V[0] : 0;
        // cout<<dp[i][j]<<" ";
        continue;
      }
      dp[i][j] = max(dp[i][j], dp[(i + 1) / 2 - 1][j]);
      dp[i][j] = max(dp[i][j],
                     j - W[i] >= 0 ? dp[(i + 1) / 2 - 1][j - W[i]] + V[i] : -1);
      // cout<<dp[i][j]<<" ";
    } // cout<<endl;
  }
  for (int q = 0; q < Q; q++) {
    int v = qV[q];
    int l = qL[q];
    if (v < dp.size()) {
      // cout<<"!!"<<endl;
      cout << dp[v][l] << endl;
      continue;
    }
    int P = v;
    vector<int> road;
    while (P >= dp.size()) {
      road.push_back(P);
      P = (P + 1) / 2 - 1;
    }
    int res = 0;
    // cout<<road.size()<<" "<<ipow(2,road.size())<<endl;
    for (int i = 0; i < ipow(2, road.size()); i++) {
      int val = 0;
      int wei = 0;
      for (int j = 0; j < road.size(); j++) {
        if ((i >> j) & 1) {
          val += V[road[j]];
          wei += W[road[j]];
        }
      }
      if (wei > l)
        continue;
      val += dp[P][l - wei];
      res = max(res, val);
    }
    cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;

int ipow(int a, int b) {
  int r = 1;
  while (b >= 1) {
    if (b % 2 == 1)
      r *= a;
    b /= 2;
    a *= a;
  }
  return r;
}

int main() {
  int N;
  cin >> N;
  vector<int> V, W;
  for (int i = 1; i < N + 1; i++) {
    int v, w;
    cin >> v >> w;
    V.push_back(v);
    W.push_back(w);
  }
  vector<int> qV, qL;
  int Q;
  cin >> Q;
  int maxL = 0;
  for (int i = 0; i < Q; i++) {
    int v, L;
    cin >> v >> L;
    maxL = max(maxL, L);
    v--;
    qV.push_back(v);
    qL.push_back(L);
  }
  vector<vector<int>> dp(min(N, 700), vector<int>(maxL + 1, -1));
  for (int i = 0; i < dp.size(); i++) {
    for (int j = 0; j <= maxL; j++) {
      if (i == 0) {
        dp[i][j] = W[0] <= j ? V[0] : 0;
        // cout<<dp[i][j]<<" ";
        continue;
      }
      dp[i][j] = max(dp[i][j], dp[(i + 1) / 2 - 1][j]);
      dp[i][j] = max(dp[i][j],
                     j - W[i] >= 0 ? dp[(i + 1) / 2 - 1][j - W[i]] + V[i] : -1);
      // cout<<dp[i][j]<<" ";
    } // cout<<endl;
  }
  for (int q = 0; q < Q; q++) {
    int v = qV[q];
    int l = qL[q];
    if (v < dp.size()) {
      // cout<<"!!"<<endl;
      cout << dp[v][l] << endl;
      continue;
    }
    int P = v;
    vector<int> road;
    while (P >= dp.size()) {
      road.push_back(P);
      P = (P + 1) / 2 - 1;
    }
    int res = 0;
    // cout<<road.size()<<" "<<ipow(2,road.size())<<endl;
    for (int i = 0; i < ipow(2, road.size()); i++) {
      int val = 0;
      int wei = 0;
      for (int j = 0; j < road.size(); j++) {
        if ((i >> j) & 1) {
          val += V[road[j]];
          wei += W[road[j]];
        }
      }
      if (wei > l)
        continue;
      val += dp[P][l - wei];
      res = max(res, val);
    }
    cout << res << endl;
  }
}