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

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size() - 1)
#define ms(a, b) memset(a, b, sizeof a)
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define DF(i, a, b) for (int i = (a); i >= (b); --i)
#pragma GCC optimize(2)
using namespace std;
inline int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      w = -1;
  for (; isdigit(ch); ch = getchar())
    c = (c << 1) + (c << 3) + (ch ^ 48);
  return w * c;
}
const int M = (1 << 18) + 10;
int t[25], tot, tv[M], tw[M], v[M], w[M];
int mx[(1 << 9) + 10][100050], ini[M], mxl[M];
#define lowbit(x) (x & -x)
bool cmp(int x, int y) { return tw[x] < tw[y]; }
void dfs(int x, int fa, int lim) {
  if (x >= 512)
    return;
  mxl[x] = lim;
  DF(i, lim, 0) mx[x][i] = mx[fa][i];
  DF(i, lim, w[x]) mx[x][i] = max(mx[x][i], mx[x][i - w[x]] + v[x]);
  dfs((x << 1), x, min(100000, lim + w[x << 1]));
  dfs((x << 1 | 1), x, min(100000, lim + w[x << 1 | 1]));
  F(i, 1, lim) mx[x][i] = max(mx[x][i], mx[x][i - 1]);
}
int main() {
  //	freopen("1.in","r",stdin);
  //	freopen("1.out","w",stdout);
  int n = read();
  F(i, 1, n) v[i] = read(), w[i] = read();
  dfs(1, 0, w[1]);
  int Q = read();
  while (Q--) {
    int x = read(), l = read();
    tot = 0;
    int op = 0, ans = 0;
    for (; x >= 512; x >>= 1)
      t[++tot] = x, ini[(1 << op)] = x, op += (x >= 512);
    //		cout<<op<<"   gg\\n";
    F(i, 0, (1 << op) - 1) {
      int pos = lowbit(i);
      tv[i] = tv[i - pos] + v[ini[pos]];
      tw[i] = tw[i - pos] + w[ini[pos]];
      if (tw[i] <= l) {
        ans = max(ans, tv[i] + mx[x][min(l - tw[i], mxl[x])]);
      }
    }
    assert(!tv[0] && !tw[0]);
    cout << ans << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size() - 1)
#define ms(a, b) memset(a, b, sizeof a)
#define F(i, a, b) for (int i = (a); i <= (b); ++i)
#define DF(i, a, b) for (int i = (a); i >= (b); --i)
using namespace std;
inline int read() {
  char ch = getchar();
  int w = 1, c = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      w = -1;
  for (; isdigit(ch); ch = getchar())
    c = (c << 1) + (c << 3) + (ch ^ 48);
  return w * c;
}
const int M = (1 << 18) + 10;
int t[25], tot, tv[M], tw[M], v[M], w[M], p[M];
bool cmp(int x, int y) { return tw[x] < tw[y]; }
int main() {
  int n = read();
  F(i, 1, n) v[i] = read(), w[i] = read();
  int Q = read();
  while (Q--) {
    int x = read(), l = read();
    tot = 0;
    for (; x; x >>= 1)
      t[++tot] = x;
    int o = tot >> 1, no = tot - o;
    //		cout<<o<<" "<<no<<"   sb\\n";
    for (int i = 0; i < (1 << o); i++) {
      tv[i] = tw[i] = 0;
      for (int j = 0; j < o; j++) {
        if (i >> j & 1)
          tv[i] += v[t[j + 1]], tw[i] += w[t[j + 1]];
      }
      p[i] = i;
    }
    int ans = 0;
    sort(p, p + (1 << o), cmp);
    F(i, 1, (1 << o) - 1) tv[p[i]] = max(tv[p[i - 1]], tv[p[i]]);
    for (int i = 0; i < (1 << no); i++) {
      int t1 = 0, t2 = 0;
      for (int j = 0; j < no; j++) {
        if (i >> j & 1)
          t1 += v[t[j + 1 + o]], t2 += w[t[j + 1 + o]];
      }
      if (t2 > l)
        continue;
      tw[M - 1] = min(l - t2, tw[p[(1 << o) - 1]]);
      int pos = lower_bound(p, p + (1 << o), M - 1, cmp) - p;
      if (t2 + tw[p[pos]] > l)
        pos--;
      //			cout<<t2<<" "<<p[pos]<<" "<<" "<<l<<"    ok\\n";
      if (pos >= 0)
        ans = max(tv[p[pos]] + t1, ans);
    }
    cout << ans << "\\n";
  }
  return 0;
}