Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
// raja1999

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
// setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define int ll

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// std::ios::sync_with_stdio(false);
ll v[(1 << 18) + 5], w[(1 << 18) + 5];
ll brute(int n, int l) {
  ll val = 0, lol, haha, j, k, res = 0;
  vii vec;
  while (n >= 1) {
    vec.pb(mp(v[n], w[n]));
    n /= 2;
    val++;
  }
  rep(j, (1 << val)) {
    lol = 0;
    haha = 0;
    rep(k, vec.size()) {
      if (j & (1 << k)) {
        lol += vec[k].ff;
        haha += vec[k].ss;
      }
    }
    if (haha <= l) {
      res = max(res, lol);
    }
  }
  // cout<<res<<" "<<val<<endl;
  return res;
}
int u[100005], l[100005];
vector<pair<ll, ll>>::iterator it;
vector<vll> GG((1 << 10) + 5);
vector<vl> MM((1 << 10) + 5);
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, q, i, val, c, lol, haha, res, j, k, maxi, req, pos, rem, iinf, node;
  iinf = inf;
  iinf *= inf;
  vii vec;
  cin >> n;
  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  cin >> q;
  rep(i, q) { cin >> u[i] >> l[i]; }
  if (n < 128) {
    rep(i, q) { cout << brute(u[i], l[i]) << endl; }
  } else {
    int lev = 0;
    int n1 = n;
    while (n1 >= 1) {
      n1 /= 2;
      lev++;
    }
    val = (lev + 1) / 2;
    f(i, 1, n + 1) {
      n1 = i;
      c = 0;
      vec.clear();
      while (n1 >= 1) {
        c++;
        vec.pb(mp(v[n1], w[n1]));
        n1 /= 2;
      }
      if (c == val) {
        rep(j, (1 << val)) {
          lol = 0;
          haha = 0;
          rep(k, vec.size()) {
            if (j & (1 << k)) {
              lol += vec[k].ff;
              haha += vec[k].ss;
            }
          }
          if (haha <= 100000) {
            GG[i].pb(mp(haha, lol));
          }
        }
        sort(all(GG[i]));
        maxi = 0;
        MM[i].resize(GG[i].size() + 2);
        f(j, 0, GG[i].size()) {
          maxi = max(maxi, GG[i][j].ss);
          MM[i][j] = maxi;
        }
      }
    }
    rep(i, q) {
      n1 = u[i];
      c = 0;
      while (n1 >= 1) {
        c++;
        n1 /= 2;
      }
      if (c < val) {
        cout << brute(u[i], l[i]) << endl;
      } else {
        n1 = u[i];
        req = c - val;
        vec.clear();
        while (req--) {
          vec.pb(mp(v[n1], w[n1]));
          n1 /= 2;
        }
        node = n1;
        req = c - val;
        res = 0;
        rep(j, (1 << req)) {
          lol = 0;
          haha = 0;
          rep(k, vec.size()) {
            if (j & (1 << k)) {
              lol += vec[k].ff;
              haha += vec[k].ss;
            }
          }
          if (haha <= l[i]) {
            rem = l[i] - haha;
            it = upper_bound(GG[node].begin(), GG[node].end(), mp(rem, iinf));
            it--;
            pos = (it - GG[node].begin());
            res = max(res, lol + MM[node][pos]);
          }
        }
        cout << res << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
// raja1999

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
// setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define int ll

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

// std::ios::sync_with_stdio(false);
ll v[(1 << 18) + 5], w[(1 << 18) + 5];
ll brute(int n, int l) {
  ll val = 0, lol, haha, j, k, res = 0;
  vii vec;
  while (n >= 1) {
    vec.pb(mp(v[n], w[n]));
    n /= 2;
    val++;
  }
  rep(j, (1 << val)) {
    lol = 0;
    haha = 0;
    rep(k, vec.size()) {
      if (j & (1 << k)) {
        lol += vec[k].ff;
        haha += vec[k].ss;
      }
    }
    if (haha <= l) {
      res = max(res, lol);
    }
  }
  // cout<<res<<" "<<val<<endl;
  return res;
}
int u[100005], l[100005];
vector<pair<ll, ll>>::iterator it;
vector<vll> GG((1 << 10) + 5);
vector<vl> MM((1 << 10) + 5);
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, q, i, val, c, lol, haha, res, j, k, maxi, req, pos, rem, iinf, node;
  iinf = inf;
  iinf *= inf;
  vii vec;
  cin >> n;
  rep(i, n) { cin >> v[i + 1] >> w[i + 1]; }
  cin >> q;
  rep(i, q) { cin >> u[i] >> l[i]; }
  if (n < 128) {
    rep(i, q) { cout << brute(u[i], l[i]) << endl; }
  } else {
    int lev = 0;
    int n1 = n;
    while (n1 >= 1) {
      n1 /= 2;
      lev++;
    }
    val = (lev + 1) / 2;
    val++;
    f(i, 1, n + 1) {
      n1 = i;
      c = 0;
      vec.clear();
      while (n1 >= 1) {
        c++;
        vec.pb(mp(v[n1], w[n1]));
        n1 /= 2;
      }
      if (c == val) {
        rep(j, (1 << val)) {
          lol = 0;
          haha = 0;
          rep(k, vec.size()) {
            if (j & (1 << k)) {
              lol += vec[k].ff;
              haha += vec[k].ss;
            }
          }
          if (haha <= 100000) {
            GG[i].pb(mp(haha, lol));
          }
        }
        sort(all(GG[i]));
        maxi = 0;
        MM[i].resize(GG[i].size() + 2);
        f(j, 0, GG[i].size()) {
          maxi = max(maxi, GG[i][j].ss);
          MM[i][j] = maxi;
        }
      }
    }
    rep(i, q) {
      n1 = u[i];
      c = 0;
      while (n1 >= 1) {
        c++;
        n1 /= 2;
      }
      if (c < val) {
        cout << brute(u[i], l[i]) << endl;
      } else {
        n1 = u[i];
        req = c - val;
        vec.clear();
        while (req--) {
          vec.pb(mp(v[n1], w[n1]));
          n1 /= 2;
        }
        node = n1;
        req = c - val;
        res = 0;
        rep(j, (1 << req)) {
          lol = 0;
          haha = 0;
          rep(k, vec.size()) {
            if (j & (1 << k)) {
              lol += vec[k].ff;
              haha += vec[k].ss;
            }
          }
          if (haha <= l[i]) {
            rem = l[i] - haha;
            it = upper_bound(GG[node].begin(), GG[node].end(), mp(rem, iinf));
            it--;
            pos = (it - GG[node].begin());
            res = max(res, lol + MM[node][pos]);
          }
        }
        cout << res << endl;
      }
    }
  }
  return 0;
}