Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 06/13/20 21:03:09
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n;
  cin >> n;
  struct Data {
    int v = 0, w = 0;
  };
  vector<Data> ds(n + 1);
  for (int i = 0; i < n; i++) {
    int v, w;
    cin >> v >> w;
    ds[i + 1] = {v, w};
  }
  int m = min(1 << 9, n);
  vector<vector<int>> dp(m + 1, vector<int>(1e5 + 1, -1));
  dp[0][0] = 0;
  for (int t = 1; t <= m; t++) {
    dp[t] = dp[t / 2];
    for (int s = 1e5; s >= ds[t].w; s--) {
      if (dp[t / 2][s - ds[t].w] == -1)
        continue;
      dp[t][s] = max(dp[t][s], dp[t / 2][s - ds[t].w] + ds[t].v);
    }
  }
  for (int t = 1; t <= m; t++) {
    for (int s = 1; s <= 1e5; s++) {
      dp[t][s] = max(dp[t][s], dp[t][s - 1]);
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    int f = 0;
    while (v >> f > m) {
      f++;
    }
    int ans = dp[v >> f][l];
    if (v > m) {
      for (int mask = 1; mask < 1 << f; mask++) {
        int cv = 0, cw = 0;
        for (int j = 0; j < f; j++) {
          if ((mask & (1 << j)) == 0)
            continue;
          cv += ds[v >> (f - j - 1)].v;
          cw += ds[v >> (f - j - 1)].w;
          if (cw > l)
            break;
        }
        if (cw > l)
          continue;
        ans = max(ans, dp[v >> f][l - cw] + cv);
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 06/13/20 21:03:09
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n;
  cin >> n;
  struct Data {
    int v = 0, w = 0;
  };
  vector<Data> ds(n + 1);
  for (int i = 0; i < n; i++) {
    int v, w;
    cin >> v >> w;
    ds[i + 1] = {v, w};
  }
  int m = min(2 << 9, n);
  vector<vector<int>> dp(m + 1, vector<int>(1e5 + 1, -1));
  dp[0][0] = 0;
  for (int t = 1; t <= m; t++) {
    dp[t] = dp[t / 2];
    for (int s = 1e5; s >= ds[t].w; s--) {
      if (dp[t / 2][s - ds[t].w] == -1)
        continue;
      dp[t][s] = max(dp[t][s], dp[t / 2][s - ds[t].w] + ds[t].v);
    }
  }
  for (int t = 1; t <= m; t++) {
    for (int s = 1; s <= 1e5; s++) {
      dp[t][s] = max(dp[t][s], dp[t][s - 1]);
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int v, l;
    cin >> v >> l;
    int f = 0;
    while (v >> f > m) {
      f++;
    }
    int ans = dp[v >> f][l];
    if (v > m) {
      for (int mask = 1; mask < 1 << f; mask++) {
        int cv = 0, cw = 0;
        for (int j = 0; j < f; j++) {
          if ((mask & (1 << j)) == 0)
            continue;
          cv += ds[v >> (f - j - 1)].v;
          cw += ds[v >> (f - j - 1)].w;
          if (cw > l)
            break;
        }
        if (cw > l)
          continue;
        ans = max(ans, dp[v >> f][l - cw] + cv);
      }
    }
    cout << ans << endl;
  }

  return 0;
}