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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);

  ll n;
  cin >> n;
  vector<ll> v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];

  // ll m = 3;
  ll m = min(n, (1LL << 9));
  vector<vector<ll>> dp(m, vector<ll>(100001, 0));
  FOR(j, w[0], 100001) dp[0][j] = v[0];
  FOR(i, 1, m) {
    rep(j, 100001) {
      dp[i][j] = dp[(i - 1) / 2][j];
      if (j - w[i] >= 0)
        chmax(dp[i][j], dp[(i - 1) / 2][j - w[i]] + v[i]);
    }
  }

  ll q;
  cin >> q;
  while (q--) {
    ll vs, l;
    cin >> vs >> l;
    vs--;

    vector<ll> resv, resw;
    while (vs >= m) {
      resv.push_back(v[vs]);
      resw.push_back(w[vs]);
      vs = (vs - 1) / 2;
    }
    ll ans = 0;
    ll vn = resv.size();
    rep(S, (1 << vn)) {
      ll rv = 0, rw = 0;
      rep(i, vn) {
        if (S >> i & 1) {
          rv += resv[i];
          rw += resw[i];
        }
      }
      if (l - rw >= 0)
        chmax(ans, rv + dp[vs][l - rw]);
    }
    cout << ans << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);

  ll n;
  cin >> n;
  vector<ll> v(n), w(n);
  rep(i, n) cin >> v[i] >> w[i];

  ll m = min(n, (1LL << 9));
  vector<vector<ll>> dp(m, vector<ll>(100001, 0));
  FOR(j, w[0], 100001) dp[0][j] = v[0];
  FOR(i, 1, m) {
    rep(j, 100001) {
      dp[i][j] = dp[(i - 1) / 2][j];
      if (j - w[i] >= 0)
        chmax(dp[i][j], dp[(i - 1) / 2][j - w[i]] + v[i]);
    }
  }

  ll q;
  cin >> q;
  while (q--) {
    ll vs, l;
    cin >> vs >> l;
    vs--;

    vector<ll> resv, resw;
    while (vs >= m) {
      resv.push_back(v[vs]);
      resw.push_back(w[vs]);
      vs = (vs - 1) / 2;
    }
    vector<ll> resdp(100001, 0);
    rep(i, resv.size()) {
      FORR(j, 100000, resw[i]) {
        chmax(resdp[j], resdp[j - resw[i]] + resv[i]);
      }
    }
    ll ans = 0;
    rep(i, l + 1) { chmax(ans, dp[vs][i] + resdp[l - i]); }
    cout << ans << '\\n';
  }

  return 0;
}