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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const ll INF = 1E+18;

ll dp[515][100005];

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

  dp[0][0] = 0LL;
  rep(i, 512) {
    if (i == n)
      break;
    rep(j, 100002) {

      if (i == 0) {
        if (j >= w[0])
          dp[i][j] = v[0];
      } else {
        dp[i][j] = dp[(i + 1) / 2 - 1][j];

        if (j - w[i] >= 0) {
          chmax(dp[i][j], dp[(i + 1) / 2 - 1][j - w[i]] + v[i]);
        }
      }
    }
  }

  ll q;
  cin >> q;

  rep(qi, q) {
    ll u, l;
    cin >> u >> l;
    u--;
    if (u < 512) {
      cout << dp[u][l] << endl;
      continue;
    }
    ll num = 0;
    ll temp = u;
    vector<ll> iw;
    vector<ll> iv;
    while (temp >= 512) {
      iw.push_back(w[temp]);
      iv.push_back(v[temp]);
      num++;
      temp = (temp + 1) / 2 - 1;
    }
    ll ans = 0;

    rep(i, 1LL << num) {
      ll sumw = 0;
      ll sumv = 0;
      rep(j, num) {
        if ((i >> j & 1) == 0)
          continue;
        sumw += iw[j];
        sumv += iv[j];
      }
      if (l - sumw >= 0) {
        chmax(ans, dp[temp][l - sumw] + sumv);
      }
    }

    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const ll INF = 1E+18;

ll dp[515][100005];

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

  dp[0][0] = 0LL;
  rep(i, 512) {
    if (i == n)
      break;
    rep(j, 100002) {

      if (i == 0) {
        if (j >= w[0])
          dp[i][j] = v[0];
      } else {
        dp[i][j] = dp[(i + 1) / 2 - 1][j];

        if (j - w[i] >= 0) {
          chmax(dp[i][j], dp[(i + 1) / 2 - 1][j - w[i]] + v[i]);
        }
      }
    }
  }

  ll q;
  cin >> q;

  rep(qi, q) {
    ll u, l;
    cin >> u >> l;
    u--;
    if (u < 512) {
      cout << dp[u][l] << endl;
      continue;
    }
    map<ll, ll> mp;
    mp[l] = 0;
    while (u >= 512) {
      for (auto e : mp) {
        if (e.first - w[u] >= 0) {
          chmax(mp[e.first - w[u]], e.second + v[u]);
        }
      }
      u = (u + 1) / 2 - 1;
    }
    ll ans = 0;
    for (auto e : mp) {
      chmax(ans, dp[u][e.first] + e.second);
    }
    cout << ans << endl;
  }

  return 0;
}