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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;

int dep(int n) {
  int ret = 0;
  while (n >= 2) {
    n /= 2;
    ret++;
  }
  return ret;
}

int func(int n, int j) {
  while (j--)
    n /= 2;
  return n;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // input
  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  REPS(i, n) cin >> v[i] >> w[i];
  int q;
  cin >> q;
  int M_L = 0;
  int t[q], l[q];
  REP(i, q) {
    cin >> t[i] >> l[i];
    chmax(M_L, l[i]);
  }
  // 半分全列挙
  int V = min((1ll << 9) - 1, n);
  vector<vector<int>> dp(V + 1, vector<int>(M_L + 1, 0));
  REPS(i, V) REP(j, M_L + 1) {
    chmax(dp[i][j], dp[i / 2][j]);
    if (j - w[i] > -1)
      chmax(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
  }
  int z, o, ans, hoge[9];
  REP(i, q) {
    if (t[i] <= V)
      cout << dp[t[i]][l[i]] << '\\n';
    else {
      z = dep(t[i]) - 8;
      o = func(t[i], z);
      ans = 0;
      REP(j, z) hoge[j] = func(t[i], j);
      for (int b = 0; b < (1ll << z); b++) {
        int sumw = 0;
        int sumv = 0;
        REP(j, z) if ((b >> j) & 1) {
          sumw += w[hoge[j]];
          sumv += v[hoge[j]];
        }
        if (l[i] - sumw > -1)
          chmax(ans, dp[o][l[i] - sumw] + sumv);
      }
      cout << ans << '\\n';
    }
  }
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, l, r) for (long long i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (long long i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e18;

int dep(int n) {
  int ret = 0;
  while (n >= 2) {
    n /= 2;
    ret++;
  }
  return ret;
}

int func(int n, int j) {
  while (j--)
    n /= 2;
  return n;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // input
  int n;
  cin >> n;
  int v[n + 1], w[n + 1];
  REPS(i, n) cin >> v[i] >> w[i];
  int q;
  cin >> q;
  int M_L = 0;
  int t[q], l[q];
  REP(i, q) {
    cin >> t[i] >> l[i];
    chmax(M_L, l[i]);
  }
  // 半分全列挙
  int V = min((1ll << 9) - 1, n);
  vector<vector<int>> dp(V + 1, vector<int>(M_L + 1, 0));
  REPS(i, V) REP(j, M_L + 1) {
    chmax(dp[i][j], dp[i / 2][j]);
    if (j - w[i] > -1)
      chmax(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
  }
  int z, o, ans, hoge;
  REP(i, q) {
    if (t[i] <= V)
      cout << dp[t[i]][l[i]] << '\\n';
    else {
      z = dep(t[i]) - 8;
      o = func(t[i], z);
      ans = 0;
      for (int b = 0; b < (1ll << z); b++) {
        int sumw = 0;
        int sumv = 0;
        REP(j, z) if ((b >> j) & 1) {
          hoge = func(t[i], j);
          sumw += w[hoge];
          sumv += v[hoge];
        }
        if (l[i] - sumw > -1)
          chmax(ans, dp[o][l[i] - sumw] + sumv);
      }
      cout << ans << '\\n';
    }
  }
}