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

#include <iostream>
#include <vector>

using namespace std;

const int L = 100000 + 1;

void calc(vector<vector<int>> &cache, int x, int n, const vector<int> &table,
          const vector<int> &Vs, const vector<int> &Ws) {
  if (x <= n) {
    const auto v = Vs[x - 1];
    const auto w = Ws[x - 1];
    vector<int> new_table = table;
    for (int w0 = L - 1; w0 >= 0; w0--) {
      const int w1 = w0 + w;
      const int v1 = new_table[w0] + v;
      if (w1 <= L && new_table[w1] < v1) {
        new_table[w1] = v1;
      }
    }
    calc(cache, x * 2, n, new_table, Vs, Ws);
    calc(cache, x * 2 + 1, n, new_table, Vs, Ws);
    cache[x - 1] = move(new_table);
  }
}

int main(void) {
  int N;
  cin >> N;
  vector<int> Vs(N);
  vector<int> Ws(N);
  for (int i = 0; i < N; i++) {
    cin >> Vs[i];
    cin >> Ws[i];
  }

  const int n = N < 1024 ? N : 1024;

  vector<vector<int>> cache(n);

  calc(cache, 1, n, vector<int>(L, 0), Vs, Ws);

  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int i, l;
    cin >> i;
    cin >> l;
    if (i <= n) {
      cout << cache[i - 1][l] << endl;
    } else {
      int j = i;
      vector<int> target_vs(0);
      vector<int> target_ws(0);
      while (j > n) {
        target_vs.push_back(Vs[j - 1]);
        target_ws.push_back(Ws[j - 1]);
        j /= 2;
      }
      const vector<int> &table = cache[j - 1];
      int topvalue = 0;
      int b_max = 1;
      for (int k = 0; k < target_ws.size(); k++) {
        b_max *= 2;
      }
      for (int b = 0; b < b_max; b++) {
        int v0 = 0;
        int w0 = 0;
        int x = 0;
        int b2 = b;
        while (b2 > 0) {
          if (b2 % 2 == 1) {
            v0 += target_vs[x];
            w0 += target_ws[x];
          }
          x += 1;
          b2 /= 2;
        }
        if (w0 <= l) {
          int v1 = v0 + table[l - w0];
          if (v1 > topvalue) {
            topvalue = v1;
          }
        }
      }
      cout << topvalue << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(void) {
  unsigned long long N;
  cin >> N;
  vector<int> Vs(N);
  vector<int> Ws(N);
  for (unsigned long long i = 0; i < N; i++) {
    cin >> Vs[i];
    cin >> Ws[i];
  }

  int Q;
  cin >> Q;

  for (int j = 0; j < Q; j++) {
    unsigned long long i;
    cin >> i;
    int l;
    cin >> l;
    map<int, int> table;
    table[0] = 0;
    while (i > 0) {
      map<int, int> new_table;
      auto v = Vs[i - 1];
      auto w = Ws[i - 1];
      for (const auto item : table) {
        auto w_min2 = item.first;
        auto v_sum2 = item.second;
        if (w_min2 <= l &&
            (new_table.count(w_min2) == 0 || new_table[w_min2] < v_sum2)) {
          new_table[w_min2] = v_sum2;
        }
        w_min2 += w;
        v_sum2 += v;
        if (w_min2 <= l &&
            (new_table.count(w_min2) == 0 || new_table[w_min2] < v_sum2)) {
          new_table[w_min2] = v_sum2;
        }
      }
      i = i / 2;
      table = move(new_table);
    }
    int max = 0;
    for (const auto item : table) {
      //			cout << item.first << "," << item.second <<
      //endl;
      if (item.second > max) {
        max = item.second;
      }
    }
    cout << max << endl;
  }

  return 0;
}