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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

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

  ll n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  ll MAX_W = 100001LL;
  vector<vector<ll>> dp(1LL << 10, vector<ll>(MAX_W, 0));

  for (ll i = 1; i < 1LL << 10; i++) {
    if (i > n)
      break;
    for (ll j = 1; j < MAX_W; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i / 2][j]);
      if (j - w[i] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
      }
    }
  }
  // cout << dp[1][40000] << endl;
  // cout << "struct" << endl;
  // return 0;

  ll query;
  cin >> query;
  for (ll q = 0; q < query; q++) {
    ll val, lim;
    cin >> val >> lim;
    if (val < (1LL << 10)) {
      cout << dp[val][lim] << endl;
      continue;
    }

    ll ans = 0;
    int len = (int)(log2(val)) - 10 + 1;
    for (int i = 0; i < (1 << len); i++) {
      ll vtemp = 0, wtemp = 0, index = val;
      for (int j = 0; j < len; j++) {
        if ((i >> j) & 1) {
          vtemp += v[index];
          wtemp += w[index];
        }
        index /= 2;
        if (index < 1LL << 10) {
          // compare
          if (lim - wtemp < 0)
            break;
          ans = max(ans, vtemp + dp[index][lim - wtemp]);
          break;
        }
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pull = pair<ull, ll>;
using pis = pair<int, string>;
#define INF 1000000000
#define LLINF 10000000000000000ll
#define MOD 1000000007LL

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

  ll n;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];

  ll MAX_W = 100001LL;
  vector<vector<ll>> dp(1LL << 9, vector<ll>(MAX_W, 0));

  for (ll i = 1; i < 1LL << 9; i++) {
    if (i > n)
      break;
    for (ll j = 1; j < MAX_W; j++) {
      dp[i][j] = max(dp[i][j - 1], dp[i / 2][j]);
      if (j - w[i] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - w[i]] + v[i]);
      }
    }
  }

  ll query;
  cin >> query;
  for (ll q = 0; q < query; q++) {
    ll val, lim;
    cin >> val >> lim;
    if (val < (1LL << 9)) {
      cout << dp[val][lim] << endl;
      continue;
    }

    ll ans = 0;
    int len = (int)(log2(val)) - 8;
    for (int i = 0; i < (1 << len); i++) {
      ll vtemp = 0, wtemp = 0, index = val;
      for (int j = 0; j < len; j++) {
        if ((i >> j) & 1) {
          vtemp += v[index];
          wtemp += w[index];
        }
        index /= 2;
        if (index < 1LL << 9) {
          // compare
          if (lim - wtemp < 0)
            break;
          ans = max(ans, vtemp + dp[index][lim - wtemp]);
          break;
        }
      }
    }
    cout << ans << endl;
  }
}