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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v, k) (lower_bound(all(v), k) - v.begin())
#define ub(v, k) (upper_bound(all(v), k) - v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T, vector<T>, greater<T>>
#define dame(a)                                                                \\
  {                                                                            \\
    out(a);                                                                    \\
    return 0;                                                                  \\
  }
#define decimal cout << fixed << setprecision(15);
#define dupli(a)                                                               \\
  {                                                                            \\
    sort(all(a));                                                              \\
    a.erase(unique(all(a)), a.end());                                          \\
  }
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
typedef tuple<ll, ll, ll, ll> PPP;
typedef multiset<ll> S;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
const ll inf = 1001001001001001001;
const ll INF = 1001001001;
const ll mod = 1000000007;
const double eps = 1e-10;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> void out(T a) { cout << a << '\\n'; }
template <class T> void outp(T a) {
  cout << '(' << a.fi << ',' << a.se << ')' << '\\n';
}
template <class T> void outvp(T v) {
  rep(i, v.size()) cout << '(' << v[i].fi << ',' << v[i].se << ')';
  cout << '\\n';
}
template <class T> void outvvp(T v) { rep(i, v.size()) outvp(v[i]); }
template <class T> void outv(T v) {
  rep(i, v.size()) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << '\\n';
}
template <class T> void outvv(T v) { rep(i, v.size()) outv(v[i]); }
template <class T> bool isin(T x, T l, T r) { return (l) <= (x) && (x) <= (r); }
template <class T> void yesno(T b) {
  if (b)
    out("yes");
  else
    out("no");
}
template <class T> void YesNo(T b) {
  if (b)
    out("Yes");
  else
    out("No");
}
template <class T> void YESNO(T b) {
  if (b)
    out("YES");
  else
    out("NO");
}
template <class T> void noyes(T b) {
  if (b)
    out("no");
  else
    out("yes");
}
template <class T> void NoYes(T b) {
  if (b)
    out("No");
  else
    out("Yes");
}
template <class T> void NOYES(T b) {
  if (b)
    out("NO");
  else
    out("YES");
}
void outs(ll a, ll b) {
  if (a >= inf - 100)
    out(b);
  else
    out(a);
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
ll k = 1 << 9, mx = 100005;
int main() {
  ll n, q;
  cin >> n;
  chmin(k, n);
  vi v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  cin >> q;
  vvi memo(k, vi(mx));
  rep(i, k) {
    chmax(memo[i][w[i]], v[i]);
    if (i) {
      ll p = (i - 1) / 2;
      rep(j, mx - w[i]) chmax(memo[i][w[i] + j], memo[p][j] + v[i]);
      rep(j, mx) chmax(memo[i][j], memo[p][j]);
    }
    REP(j, 1, mx) chmax(memo[i][j], memo[i][j - 1]);
  }
  rep(qq, q) {
    ll x, y;
    cin >> x >> y;
    x--;
    vi t;
    while (x >= k) {
      t.pb(x);
      x = (x - 1) / 2;
    }
    ll ans = memo[x][y];
    ll s = t.size();
    rep(bit, 1 << s) {
      ll V = 0, W = 0;
      rep(i, s) if (bit >> i & 1) {
        V += v[t[i]];
        W += w[t[i]];
        if (y - W < 0)
          break;
      }
      if (y - W >= 0)
        chmax(ans, V + memo[x][y - W]);
    }
    out(ans);
  }
}

Error: TLE

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define REP(i, k, n) for (long long i = k; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v, k) (lower_bound(all(v), k) - v.begin())
#define ub(v, k) (upper_bound(all(v), k) - v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T, vector<T>, greater<T>>
#define dame(a)                                                                \\
  {                                                                            \\
    out(a);                                                                    \\
    return 0;                                                                  \\
  }
#define decimal cout << fixed << setprecision(15);
#define dupli(a) a.erase(unique(all(a)), a.end())
typedef long long ll;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> PP;
typedef tuple<ll, ll, ll, ll> PPP;
typedef multiset<ll> S;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
const ll inf = 1001001001001001001;
const ll INF = 1001001001;
const ll mod = 1000000007;
const double eps = 1e-10;
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> void out(T a) { cout << a << '\\n'; }
template <class T> void outp(T a) {
  cout << '(' << a.fi << ',' << a.se << ')' << '\\n';
}
template <class T> void outvp(T v) {
  rep(i, v.size()) cout << '(' << v[i].fi << ',' << v[i].se << ')';
  cout << '\\n';
}
template <class T> void outvvp(T v) { rep(i, v.size()) outvp(v[i]); }
template <class T> void outv(T v) {
  rep(i, v.size()) {
    if (i)
      cout << ' ';
    cout << v[i];
  }
  cout << '\\n';
}
template <class T> void outvv(T v) { rep(i, v.size()) outv(v[i]); }
template <class T> bool isin(T x, T l, T r) { return (l) <= (x) && (x) <= (r); }
template <class T> void yesno(T b) {
  if (b)
    out("yes");
  else
    out("no");
}
template <class T> void YesNo(T b) {
  if (b)
    out("Yes");
  else
    out("No");
}
template <class T> void YESNO(T b) {
  if (b)
    out("YES");
  else
    out("NO");
}
template <class T> void noyes(T b) {
  if (b)
    out("no");
  else
    out("yes");
}
template <class T> void NoYes(T b) {
  if (b)
    out("No");
  else
    out("Yes");
}
template <class T> void NOYES(T b) {
  if (b)
    out("NO");
  else
    out("YES");
}
void outs(ll a, ll b) {
  if (a >= inf - 100)
    out(b);
  else
    out(a);
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll modpow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
/*vvp query;
vi ans;
vi f,g;
ll dfs(int i){
    if(f.size()){
        use=true;
        f.eb()
    }
    if(query[i].size()){
        vi a;
        rep(bit,1<<f.size()){

        }
    }
}*/
void solve() {
  ll n;
  cin >> n;
  vi v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];
  // query=vvp(n);
  ll q;
  cin >> q;
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    // query[a-1].eb(b,i);
    a--;
    vi t, u;
    while (a) {
      if (t.size() < 9)
        t.pb(a);
      else
        u.pb(a);
      a = (a - 1) / 2;
    }
    u.pb(0);
    vp x;
    rep(bit, 1 << t.size()) {
      ll res = 0, sum = 0;
      rep(j, t.size()) if (bit >> j & 1) {
        res += v[t[j]];
        sum += w[t[j]];
        if (sum > b)
          break;
      }
      if (sum <= b)
        x.pb(sum, res);
    }
    sort(all(x));
    // outvp(x);
    REP(i, 1, x.size()) {
      if (x[i].se <= x[i - 1].se) {
        x.erase(x.begin() + i);
        i--;
      }
    }
    // outvp(x);
    ll ans = x.back().se;
    rep(bit, 1 << u.size()) {
      ll res = 0, sum = 0;
      rep(j, u.size()) if (bit >> j & 1) {
        res += v[u[j]];
        sum += w[u[j]];
        if (sum > b)
          break;
      }
      if (sum <= b) {
        ll k = res;
        // out(k);
        ll j = lb(x, P(b - sum, inf)) - 1;
        if (j >= 0)
          k += x[j].se;
        chmax(ans, k);
      }
    }
    out(ans);
  }
  // ans=vi(q);
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}