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

#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define st first
#define nd second
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
using namespace std;
const int N = (1 << 19), M = (1 << 11);
int L = 1e5;
vector<pii> calced[M];
vector<pii> backpack(vector<pii> items) {
  int sz = items.size();
  vector<pii> res;
  if (items.empty())
    return res;
  for (int mask = 0; mask < (1 << sz); ++mask) {
    int V = 0, W = 0;
    for (int i = 0; i < sz; ++i) {
      if (((1 << i) & mask) != 0) {
        V += items[i].st;
        W += items[i].nd;
      }
    }
    if (W <= L)
      res.push_back({W, V});
  }
  sort(res.begin(), res.end());
  int mx = -1;
  vector<pii> Res;
  for (auto i : res) {
    if (i.nd > mx) {
      Res.push_back(i);
      mx = i.nd;
    }
  }
  return Res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < M; ++i) {
    if (i > n)
      break;
    int V = i;
    vector<pii> items;
    while (V) {
      items.push_back({v[V], w[V]});
      V /= 2;
    }
    calced[i] = backpack(items);
  }
  int q;
  cin >> q;
  while (q--) {
    int V;
    cin >> V >> L;
    // int V1 = V;
    // cerr<<"V: "<<V<<'\\n';
    if (V == 1) {
      if (w[1] <= L)
        cout << v[1] << '\\n';
      else
        cout << 0 << '\\n';
      continue;
    }
    vector<pii> items;
    while (log2(V) > 10) {
      items.push_back({v[V], w[V]});
      V /= 2;
    }
    //	int sz = items.size();
    /*if(V1==12) {
    for(auto i: items) {
            cerr<<i.st<<' '<<i.nd<<'\\n';
    }
    }*/
    vector<pii> res1 = calced[V];
    // cerr<<V<<' '<<calced[V].size()<<'\\n';
    // for(auto i: calced[V]) {
    //	cerr<<i.st<<' '<<i.nd<<'\\n';
    // }
    vector<pii> res2 = backpack(items);
    int Size = res2.size();
    int j = Size - 1;
    int Ans = 0;
    for (auto i : res1) {
      if (i.st > L)
        break;
      // i.nd --> value
      // i.st --> weight
      //  i.st -- increasing
      while (j >= 0 && i.st + res2[j].st > L) {
        j--;
      }
      if (j == -1) {
        // if(V1==12)
        // cerr<<" "<<i.st<<' '<<i.nd<<'\\n';
        Ans = max(Ans, i.nd);
      } else {
        // if(V1==12)
        // cerr<<" "<<i.st<<' '<<i.nd<<"  "<<res2[j].st<<' '<<res2[j].nd<<'\\n';
        Ans = max(Ans, i.nd + res2[j].nd);
      }
    }
    cout << Ans << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define st first
#define nd second
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
using namespace std;
const int N = (1 << 19), M = (1 << 9);
int L = 1e5;
vector<pii> calced[M];
vector<pii> backpack(vector<pii> items) {
  int sz = items.size();
  vector<pii> res;
  if (items.empty())
    return res;
  for (int mask = 0; mask < (1 << sz); ++mask) {
    int V = 0, W = 0;
    for (int i = 0; i < sz; ++i) {
      if (((1 << i) & mask) != 0) {
        V += items[i].st;
        W += items[i].nd;
      }
    }
    if (W <= L)
      res.push_back({W, V});
  }
  sort(res.begin(), res.end());
  int mx = -1;
  vector<pii> Res;
  for (auto i : res) {
    if (i.nd > mx) {
      Res.push_back(i);
      mx = i.nd;
    }
  }
  return Res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < M; ++i) {
    if (i > n)
      break;
    int V = i;
    vector<pii> items;
    while (V) {
      items.push_back({v[V], w[V]});
      V /= 2;
    }
    calced[i] = backpack(items);
  }
  int q;
  cin >> q;
  while (q--) {
    int V;
    cin >> V >> L;
    // int V1 = V;
    // cerr<<"V: "<<V<<'\\n';
    if (V == 1) {
      if (w[1] <= L)
        cout << v[1] << '\\n';
      else
        cout << 0 << '\\n';
      continue;
    }
    vector<pii> items;
    while (log2(V) > 8) {
      items.push_back({v[V], w[V]});
      V /= 2;
    }
    //	int sz = items.size();
    /*if(V1==12) {
    for(auto i: items) {
            cerr<<i.st<<' '<<i.nd<<'\\n';
    }
    }*/
    vector<pii> res1 = calced[V];
    // cerr<<V<<' '<<calced[V].size()<<'\\n';
    // for(auto i: calced[V]) {
    //	cerr<<i.st<<' '<<i.nd<<'\\n';
    // }
    vector<pii> res2 = backpack(items);
    int Size = res2.size();
    int j = Size - 1;
    int Ans = 0;
    for (auto i : res1) {
      if (i.st > L)
        break;
      // i.nd --> value
      // i.st --> weight
      //  i.st -- increasing
      while (j >= 0 && i.st + res2[j].st > L) {
        j--;
      }
      if (j == -1) {
        // if(V1==12)
        // cerr<<" "<<i.st<<' '<<i.nd<<'\\n';
        Ans = max(Ans, i.nd);
      } else {
        // if(V1==12)
        // cerr<<" "<<i.st<<' '<<i.nd<<"  "<<res2[j].st<<' '<<res2[j].nd<<'\\n';
        Ans = max(Ans, i.nd + res2[j].nd);
      }
    }
    cout << Ans << '\\n';
  }
}