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

/*input
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("unroll-loops,no-stack-protector")
// order_of_key #of elements less than x
//  find_by_order kth element
#define ll long long
#define ld long double
#define pii pair<int, int>
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
#define f first
#define s second
#define pb push_back
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP1(i, n) for (int i = 1; i <= n; i++)
#define FILL(n, x) memset(n, x, sizeof(n))
#define ALL(_a) _a.begin(), _a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define MP make_pair
const ll INF64 = 4e12;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;
#define lowb(x) x & (-x)
#define MNTO(x, y) x = min(x, (__typeof__(x))y)
#define MXTO(x, y) x = max(x, (__typeof__(x))y)
ll mult(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
ll mypow(ll a, ll b) {
  if (b <= 0)
    return 1;
  ll res = 1LL;
  while (b) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
const ll maxn = 3e5 + 5;
const ll maxlg = __lg(maxn) + 2;
pii arr[maxn];
int dp[513][100005];
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, q;
  cin >> n;
  REP1(i, n) cin >> arr[i].f >> arr[i].s;
  REP1(i, min((1 << 9) - 1, n)) {
    REP(j, 100005) {
      dp[i][j] = dp[i / 2][j];
      if (j >= arr[i].s)
        MXTO(dp[i][j], dp[i / 2][j - arr[i].s] + arr[i].f);
    }
  }
  cin >> q;
  while (q--) {
    int x, w;
    cin >> x >> w;
    vector<pii> v;
    int rt;
    while (x) {
      if (x < (1 << 9)) {
        rt = x;
        break;
      }
      v.pb(arr[x]);
      x /= 2;
    }
    ll ans = 0;
    REP(i, (1 << (sz(v)))) {
      int s1 = 0, s2 = 0;
      REP(j, sz(v)) {
        if (i & (1 << j)) {
          s1 += v[j].f;
          s2 += v[j].s;
        }
      }
      if (s2 <= w) {
        MXTO(ans, dp[rt][w - s2] + s1);
      }
    }
    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

/*input
15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#pragma GCC optimize("unroll-loops,no-stack-protector")
// order_of_key #of elements less than x
//  find_by_order kth element
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
#define f first
#define s second
#define pb push_back
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP1(i, n) for (int i = 1; i <= n; i++)
#define FILL(n, x) memset(n, x, sizeof(n))
#define ALL(_a) _a.begin(), _a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define MP make_pair
const ll INF64 = 4e12;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const ld PI = acos(-1);
const ld eps = 1e-9;
#define lowb(x) x & (-x)
#define MNTO(x, y) x = min(x, (__typeof__(x))y)
#define MXTO(x, y) x = max(x, (__typeof__(x))y)
ll mult(ll a, ll b) { return ((a % MOD) * (b % MOD)) % MOD; }
ll mypow(ll a, ll b) {
  if (b <= 0)
    return 1;
  ll res = 1LL;
  while (b) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
const ll maxn = 3e5 + 5;
const ll maxlg = __lg(maxn) + 2;
pii arr[maxn];
pii v[19];
pii l[1000], r[1000];

int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, q;
  cin >> n;
  REP1(i, n) cin >> arr[i].f >> arr[i].s;
  cin >> q;
  while (q--) {
    int x, w;
    cin >> x >> w;
    int ptr = 0;
    while (x) {
      if (arr[x].s <= w)
        v[ptr++] = arr[x];
      x /= 2;
    }
    assert(ptr <= 18);
    int sl = 0, sr = 0;
    map<int, int> mp, mp2;
    REP(i, (1 << (ptr / 2))) {
      int s1 = 0, s2 = 0;
      REP(j, ptr / 2) {
        if (i & (1 << j)) {
          s1 += v[j].f;
          s2 += v[j].s;
        }
      }
      if (s2 <= w)
        MNTO(mp[s2], -s1);
    }
    REP(i, (1 << ((ptr + 1) / 2))) {
      int s1 = 0, s2 = 0;
      REP(j, (ptr + 1) / 2) {
        if (i & (1 << j)) {
          s1 += v[j + (ptr / 2)].f;
          s2 += v[j + (ptr / 2)].s;
        }
      }
      if (s2 <= w)
        MXTO(mp2[s2], s1);
    }
    assert(sl < 1000 and sr < 1000);
    for (auto x : mp)
      l[sl++] = x;
    for (auto x : mp2)
      r[sr++] = x;
    REP1(i, sr - 1) { MXTO(r[i].s, r[i - 1].s); }
    int pt = sr - 1;
    int ans = 0;
    for (int i = 0; i < sl;) {
      // cout<<l[i].f<<' '<<l[i].s<<'\\n';
      while (pt >= 0 and (r[pt].f + l[i].f) > w)
        --pt;
      if (pt >= 0)
        MXTO(ans, r[pt].s - l[i].s);
      else {
        break;
      }
      int z = l[i].f;
      while (l[i].f == z)
        ++i;
    }
    cout << ans << '\\n';
  }
}