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

#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt")
#endif

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int M = 10;

vector<pair<int, int>> solve_knapsack(vector<int> cost, vector<int> w) {
  vector<pair<int, int>> ans;
  int n = (int)cost.size();
  for (int mask = 0; mask < (1 << n); mask++) {
    int sum_cost = 0;
    int sum_w = 0;
    for (int i = 0; i < n; i++) {
      if ((mask >> i) & 1) {
        sum_cost += cost[i];
        sum_w += w[i];
      }
    }
    ans.push_back({sum_w, sum_cost});
  }
  sort(ans.begin(), ans.end());
  vector<pair<int, int>> nans;
  int mx = -1;
  for (auto [wt, cst] : ans) {
    if (mx < cst) {
      nans.push_back({wt, cst});
      mx = cst;
    }
  }
  return nans;
}

int solve(vector<int> cost, vector<int> w, vector<pair<int, int>> &lp, int L) {
  auto rp = solve_knapsack(cost, w);
  int pnt = (int)rp.size() - 1;
  int ans = 0;
  for (auto [sw, sc] : lp) {
    while (pnt >= 0 && sw + rp[pnt].first > L) {
      pnt--;
    }
    if (pnt >= 0) {
      ans = max(ans, sc + rp[pnt].second);
    }
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> h(n + 1);
  vector<int> cost(n + 1);
  vector<int> w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> cost[i] >> w[i];
    if (i > 1) {
      h[i] = h[i / 2] + 1;
    }
  }
  vector<vector<pair<int, int>>> knacks(n + 1);
  for (int i = 1; i <= n; i++) {
    if (h[i] <= M) {
      vector<int> cc;
      vector<int> cw;
      int v = i;
      while (v > 0) {
        cc.push_back(cost[v]);
        cw.push_back(w[v]);
        v /= 2;
      }
      // cerr << "+ " << i << endl;
      knacks[i] = (solve_knapsack(cc, cw));
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    vector<int> cc;
    vector<int> cw;
    while (h[v] > M) {
      cc.push_back(cost[v]);
      cw.push_back(w[v]);
      v /= 2;
    }
    // assert(h[v] <= M);
    // cerr << v << endl;
    cout << solve(cc, cw, knacks[v], L) << '\\n';
  }
}


Error: TLE

Fixed submission:

#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt")
#endif

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

vector<pair<int, int>> solve_knapsack(vector<int> cost, vector<int> w) {
  vector<pair<int, int>> ans;
  int n = (int)cost.size();
  for (int mask = 0; mask < (1 << n); mask++) {
    int sum_cost = 0;
    int sum_w = 0;
    for (int i = 0; i < n; i++) {
      if ((mask >> i) & 1) {
        sum_cost += cost[i];
        sum_w += w[i];
      }
    }
    ans.push_back({sum_w, sum_cost});
  }
  sort(ans.begin(), ans.end());
  vector<pair<int, int>> nans;
  int mx = -1;
  for (auto [wt, cst] : ans) {
    if (mx < cst) {
      nans.push_back({wt, cst});
      mx = cst;
    }
  }
  return nans;
}

int solve(vector<int> cost, vector<int> w, int L) {
  vector<int> cost_l, cost_r;
  vector<int> w_l, w_r;
  int n = (int)cost.size();
  for (int i = 0; i < n; i++) {
    if (i < n / 2) {
      cost_l.push_back(cost[i]);
      w_l.push_back(w[i]);
    } else {
      cost_r.push_back(cost[i]);
      w_r.push_back(w[i]);
    }
  }
  auto lp = solve_knapsack(cost_l, w_l);
  auto rp = solve_knapsack(cost_r, w_r);
  int pnt = (int)rp.size() - 1;
  int ans = 0;
  for (auto [sw, sc] : lp) {
    while (pnt >= 0 && sw + rp[pnt].first > L) {
      pnt--;
    }
    if (pnt >= 0) {
      ans = max(ans, sc + rp[pnt].second);
    }
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> cost(n + 1);
  vector<int> w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> cost[i] >> w[i];
  }
  int q;
  cin >> q;
  while (q--) {
    int v, L;
    cin >> v >> L;
    vector<int> cc;
    vector<int> cw;
    while (v > 0) {
      cc.push_back(cost[v]);
      cw.push_back(w[v]);
      // cerr << cost[v] << ' ' << w[v] << '\\n';
      v /= 2;
    }
    cout << solve(cc, cw, L) << '\\n';
  }
}