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

/*
 * welcome to my code!
 * ------------------------------------------
 * author : lynmisakura(twitter : @andoreiji11)
 */

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

// macro templates

#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define dv(TYPE, VAL)                                                          \\
  TYPE VAL;                                                                    \\
  cin >> VAL;
#define vi(TYPE, NAME, SIZE, INIT) vector<TYPE> NAME(SIZE, (INIT))
#define vin(TYPE, NAME, SIZE)                                                  \\
  vector<TYPE> NAME(SIZE);                                                     \\
  rep(i, SIZE) cin >> NAME[i]
#define vout(ARRAY)                                                            \\
  for (auto i : ARRAY)                                                         \\
    cout << i << ' ';                                                          \\
  cout << '\\n'
#define vvi(TYPE, NAME, LOW, COL, INIT)                                        \\
  vector<vector<TYPE>> NAME(LOW, vector<TYPE>(COL, INIT))
#define vpi(T1, T2, NAME, SIZE, I1, I2)                                        \\
  vector<pair<T1, T2>> NAME(SIZE, make_pair(I1, I2))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define dup(x, y) (x + y - 1) / y
#define sz(v) (int64_t) v.size()
#define CUM(arr, cum)                                                          \\
  vector<ll> cum(sz(a) + 1, 0);                                                \\
  rep(i, sz(a)) cum[i + 1] = cum[i] + 1
#define PY(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "YES" << '\\n';                                                     \\
    return;                                                                    \\
  }
#define PN(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "NO" << '\\n';                                                      \\
    return;                                                                    \\
  }
#define PYN(COND) cout << ((COND) ? "YES" : "NO") << '\\n';
#define Py(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "Yes" << '\\n';                                                     \\
    return;                                                                    \\
  }
#define Pn(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "No" << '\\n';                                                      \\
    return;                                                                    \\
  }
#define Pyn(COND)                                                              \\
  cout << ((COND) ? "Yes" : "No") << '\\n';                                     \\
  return

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pi acos(-1)
#define fi first
#define se second
#define pq priority_queue

#define MULTITEST                                                              \\
  ;                                                                            \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  rep(_, t) main2();
#define IOB                                                                    \\
  ;                                                                            \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return false;
}

// templates end

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  const int limit = min(512, n);
  const int MAX = 1e5 + 1;

  vector<vector<int>> dp(limit, vector<int>(MAX));

  for (int i = 0; i < MAX; i++) {
    dp[0][i] = (i >= w[0] ? v[0] : 0);
  }
  for (int t = 1; t < limit; t++) {
    int p = (t - 1) / 2;
    for (int i = 0; i < MAX; i++) {
      chmax(dp[t][i], dp[p][i]);
      if (i >= w[t])
        chmax(dp[t][i], dp[p][i - w[t]] + v[t]);
    }
  }

  int q;
  cin >> q;
  rep(_, q) {
    int ver, L;
    cin >> ver >> L;
    ver--;
    vector<int> ids;
    while (ver >= limit) {
      ids.push_back(ver);
      ver = (ver - 1) / 2;
    }

    int ans = 0;
    int sz = (int)ids.size();

    for (int t = 0; t < (1 << sz); t++) {
      int vs = 0, ws = 0;
      for (int i = 0; i < sz; i++) {
        if (t & (1 << i)) {
          vs += v[ids[i]];
          ws += w[ids[i]];
        }
      }
      if (ws <= L) {
        chmax(ans, vs + dp[ver][L - ws]);
      }
    }
    cout << ans << '\\n';
  }
}


Error: TLE

Fixed submission:

/*
 * welcome to my code!
 * ------------------------------------------
 * author : lynmisakura(twitter : @andoreiji11)
 */

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

// macro templates

#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, n) for (int i = 0; i < n; i++)
#define dv(TYPE, VAL)                                                          \\
  TYPE VAL;                                                                    \\
  cin >> VAL;
#define vi(TYPE, NAME, SIZE, INIT) vector<TYPE> NAME(SIZE, (INIT))
#define vin(TYPE, NAME, SIZE)                                                  \\
  vector<TYPE> NAME(SIZE);                                                     \\
  rep(i, SIZE) cin >> NAME[i]
#define vout(ARRAY)                                                            \\
  for (auto i : ARRAY)                                                         \\
    cout << i << ' ';                                                          \\
  cout << '\\n'
#define vvi(TYPE, NAME, LOW, COL, INIT)                                        \\
  vector<vector<TYPE>> NAME(LOW, vector<TYPE>(COL, INIT))
#define vpi(T1, T2, NAME, SIZE, I1, I2)                                        \\
  vector<pair<T1, T2>> NAME(SIZE, make_pair(I1, I2))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define dup(x, y) (x + y - 1) / y
#define sz(v) (int64_t) v.size()
#define CUM(arr, cum)                                                          \\
  vector<ll> cum(sz(a) + 1, 0);                                                \\
  rep(i, sz(a)) cum[i + 1] = cum[i] + 1
#define PY(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "YES" << '\\n';                                                     \\
    return;                                                                    \\
  }
#define PN(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "NO" << '\\n';                                                      \\
    return;                                                                    \\
  }
#define PYN(COND) cout << ((COND) ? "YES" : "NO") << '\\n';
#define Py(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "Yes" << '\\n';                                                     \\
    return;                                                                    \\
  }
#define Pn(COND)                                                               \\
  if (COND) {                                                                  \\
    cout << "No" << '\\n';                                                      \\
    return;                                                                    \\
  }
#define Pyn(COND)                                                              \\
  cout << ((COND) ? "Yes" : "No") << '\\n';                                     \\
  return

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pi acos(-1)
#define fi first
#define se second
#define pq priority_queue

#define MULTITEST                                                              \\
  ;                                                                            \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  rep(_, t) main2();
#define IOB                                                                    \\
  ;                                                                            \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  } else
    return false;
}

// templates end

void main2() {}
int64_t n;
int64_t v[1 << 18], w[1 << 18];
int64_t x, l;
int64_t dp[20][100010];
void solve() {
  memset(dp, 0, sizeof(dp));
  dp[0][0] = INT_MAX;
  int64_t ans = 0;
  rep(i, n + 1) {
    if (x == 0) {
      cout << ans - INT_MAX << '\\n';
      return;
    }
    rep(j, l + 1) {
      chmax(dp[i + 1][j], dp[i][j]);
      chmax(ans, dp[i + 1][j]);
      if (j + w[x - 1] <= l) {
        chmax(dp[i + 1][j + w[x - 1]], dp[i][j] + v[x - 1]);
        chmax(ans, dp[i + 1][j + w[x - 1]]);
      }
    }
    x = (x / 2);
  }
}
int main(void) {
  IOB;
  // MULTITEST
  cin >> n;
  rep(i, n) { cin >> v[i] >> w[i]; }
  int64_t q;
  cin >> q;
  rep(_, q) {
    cin >> x >> l;
    solve();
  }
}