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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long V[1 << 18];
long long W[1 << 18];
pair<long long, long long> wv[1 << 9];
int v[100000];
int L[100000];
long long dp[1 << 10][100001];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> L[i];
  }
  int max_L = *max_element(L, L + Q);

  if (W[0] <= max_L) {
    for (int i = W[0]; i <= max_L; i++) {
      dp[0][i] = V[0];
    }
  }

  for (int i = 1; i < min((1 << 10), N); i++) {
    int prev = (i + 1) / 2 - 1;
    for (int j = 0; j <= max_L; j++) {
      dp[i][j] = max(dp[i][j], dp[prev][j]);
      if (j >= W[i]) {
        dp[i][j] = max(dp[i][j], V[i] + dp[prev][j - W[i]]);
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    int cur_v = v[i];
    long long cur_L = L[i];
    if (cur_v <= (1 << 10)) {
      cout << dp[cur_v - 1][cur_L] << endl;
    } else {
      int wv_size = 0;
      while (true) {
        wv[wv_size] = make_pair(W[cur_v - 1], V[cur_v - 1]);
        wv_size++;
        cur_v /= 2;
        if (cur_v <= (1 << 10)) {
          break;
        }
      }

      long long res = 0;
      for (int bit_set = 0; bit_set < (1 << wv_size); bit_set++) {
        long long sw = 0;
        long long sv = 0;
        for (int j = 0; j < wv_size; j++) {
          if (((bit_set >> j) & 1) == 1) {
            sw += wv[j].first;
            sv += wv[j].second;
          }
        }
        if (sw <= cur_L) {
          res = max(res, sv + dp[cur_v - 1][cur_L - sw]);
        }
      }
      cout << res << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
long long V[1 << 18];
long long W[1 << 18];
pair<long long, long long> wv[1 << 9];
int v[100000];
int L[100000];
long long dp[1 << 9][100001];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> V[i] >> W[i];
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> v[i] >> L[i];
  }
  int max_L = *max_element(L, L + Q);

  if (W[0] <= max_L) {
    for (int i = W[0]; i <= max_L; i++) {
      dp[0][i] = V[0];
    }
  }

  for (int i = 1; i < min((1 << 9), N); i++) {
    int prev = (i + 1) / 2 - 1;
    for (int j = 0; j <= max_L; j++) {
      dp[i][j] = max(dp[i][j], dp[prev][j]);
      if (j >= W[i]) {
        dp[i][j] = max(dp[i][j], V[i] + dp[prev][j - W[i]]);
      }
    }
  }

  for (int i = 0; i < Q; i++) {
    int cur_v = v[i];
    long long cur_L = L[i];
    if (cur_v <= (1 << 9)) {
      cout << dp[cur_v - 1][cur_L] << endl;
    } else {
      int wv_size = 0;
      while (true) {
        wv[wv_size] = make_pair(W[cur_v - 1], V[cur_v - 1]);
        wv_size++;
        if (cur_v <= (1 << 9)) {
          break;
        } else {
          cur_v /= 2;
        }
      }

      long long res = 0;
      for (int bit_set = 0; bit_set < (1 << wv_size); bit_set++) {
        long long sw = 0;
        long long sv = 0;
        for (int j = 0; j < wv_size; j++) {
          if (((bit_set >> j) & 1) == 1) {
            sw += wv[j].first;
            sv += wv[j].second;
          }
        }
        if (sw <= cur_L) {
          res = max(res, sv + dp[cur_v - 1][cur_L - sw]);
        }
      }
      cout << res << endl;
    }
  }

  return 0;
}