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

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

ll N, Q;

vector<pair<ll, ll>> tree;
vector<pair<ll, ll>> que;
vector<vector<ll>> best_per_weight;
int max_idx = 1;
ll max_l;

ll brute_force(int v, ll w, ll acc_val, ll max_wq) {
  if (v < max_idx) {
    return best_per_weight[v][max_wq - w] + acc_val;
  } else {
    ll c_value = tree[v].first;
    ll c_weight = tree[v].second;
    int parent = (v - 1) / 2;

    ll not_pick = brute_force(parent, w, acc_val, max_wq);

    ll pick;
    if (c_weight + w > max_wq)
      return not_pick;
    else {
      pick = brute_force(parent, c_weight + w, acc_val + c_value, max_wq);
    }

    return pick > not_pick ? pick : not_pick;
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    ll v, w;
    cin >> v >> w;
    tree.push_back(make_pair(v, w));
  }

  cin >> Q;
  max_l = 0;
  for (int i = 0; i < Q; i++) {
    ll v, l;
    cin >> v >> l;
    que.push_back(make_pair(v - 1, l));
    if (max_l < l)
      max_l = l;
  }

  max_idx = 1024 > N ? N : 1024;

  for (int i = 0; i < max_idx; i++) {
    // vector<ll> b_val(max_l+1, 0);
    vector<ll> b_val;
    if (i == 0) {
      ll c_value = tree[i].first;
      ll c_weight = tree[i].second;
      if (c_weight <= max_l) {
        for (int k = 0; k < c_weight; k++) {
          b_val.push_back(0);
        }
        for (int k = c_weight; k < max_l + 1; k++) {
          b_val.push_back(c_value);
        }
      }
    } else {
      ll c_value = tree[i].first;
      ll c_weight = tree[i].second;
      for (int j = 0; j < max_l + 1; j++) {
        int parent = (i - 1) / 2;
        int prev_idx = j - c_weight;
        if (prev_idx < 0)
          b_val.push_back(best_per_weight[parent][j]);
        else {
          ll tmp = best_per_weight[parent][prev_idx] + c_value;
          tmp = tmp > best_per_weight[parent][j] ? tmp
                                                 : best_per_weight[parent][j];
          b_val.push_back(tmp);
        }
      }
    }
    best_per_weight.push_back(b_val);
  }
  //
  // cout << "FInish" << endl;

  for (auto q : que) {
    int v = q.first;
    int L = q.second;
    ll best_val = brute_force(v, 0, 0, L);
    cout << best_val << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

ll N, Q;

vector<pair<ll, ll>> tree;
vector<pair<ll, ll>> que;
vector<vector<ll>> best_per_weight;
int max_idx = 1;
ll max_l;

ll brute_force(int v, ll w, ll acc_val, ll max_wq) {
  if (v < max_idx) {
    return best_per_weight[v][max_wq - w] + acc_val;
  } else {
    ll c_value = tree[v].first;
    ll c_weight = tree[v].second;
    int parent = (v - 1) / 2;

    ll not_pick = brute_force(parent, w, acc_val, max_wq);

    ll pick;
    if (c_weight + w > max_wq)
      return not_pick;
    else {
      pick = brute_force(parent, c_weight + w, acc_val + c_value, max_wq);
    }

    return pick > not_pick ? pick : not_pick;
  }
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    ll v, w;
    cin >> v >> w;
    tree.push_back(make_pair(v, w));
  }

  cin >> Q;
  max_l = 0;
  for (int i = 0; i < Q; i++) {
    ll v, l;
    cin >> v >> l;
    que.push_back(make_pair(v - 1, l));
    if (max_l < l)
      max_l = l;
  }

  int max_idx = 1024 > N ? N : 1024;

  for (int i = 0; i < max_idx; i++) {
    // vector<ll> b_val(max_l+1, 0);
    vector<ll> b_val;
    if (i == 0) {
      ll c_value = tree[i].first;
      ll c_weight = tree[i].second;
      if (c_weight <= max_l) {
        for (int k = 0; k < c_weight; k++) {
          b_val.push_back(0);
        }
        for (int k = c_weight; k < max_l + 1; k++) {
          b_val.push_back(c_value);
        }
      }
    } else {
      ll c_value = tree[i].first;
      ll c_weight = tree[i].second;
      for (int j = 0; j < max_l + 1; j++) {
        int parent = (i - 1) / 2;
        int prev_idx = j - c_weight;
        if (prev_idx < 0)
          b_val.push_back(best_per_weight[parent][j]);
        else {
          ll tmp = best_per_weight[parent][prev_idx] + c_value;
          tmp = tmp > best_per_weight[parent][j] ? tmp
                                                 : best_per_weight[parent][j];
          b_val.push_back(tmp);
        }
      }
    }
    best_per_weight.push_back(b_val);
  }
  //
  // cout << "FInish" << endl;

  for (auto q : que) {
    int v = q.first;
    int L = q.second;
    ll best_val = brute_force(v, 0, 0, L);
    cout << best_val << endl;
  }

  return 0;
}