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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

const int VS = 1 << 8;
const int W = 1e5;
void cmax(int &lhs, int rhs) { lhs = max(lhs, rhs); }
vector<int> v, w;
vector<vector<int>> dp(VS, vector<int>(W + 1));

ll dfs(int ver, int y, int vsum, int wsum) {
  if (ver < VS) {
    return wsum <= y ? dp[ver][y - wsum] + vsum : 0;
  }
  int par = (ver - 1) / 2;
  return max(dfs(par, y, vsum, wsum),
             dfs(par, y, vsum + v[ver], wsum + w[ver]));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  v.resize(n), w.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  int sz = min(VS, n);
  for (int i = 0; i < sz; i++) {
    if (i != 0) {
      int par = (i - 1) / 2;
      dp[i] = dp[par];
      for (int j = 0; j <= W - w[i]; j++) {
        cmax(dp[i][j + w[i]], dp[par][j] + v[i]);
      }
    } else {
      for (int i = w[0]; i <= W; i++) {
        dp[0][i] = v[0];
      }
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    cout << dfs(x, y, 0, 0) << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

const int VS = 1 << 8;
const int W = 1e5;
void cmax(int &lhs, int rhs) { lhs = max(lhs, rhs); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  int sz = min(VS, n);
  vector<vector<int>> dp(sz, vector<int>(W + 1));

  for (int i = 0; i < sz; i++) {
    int par = (i - 1) / 2;
    for (int j = 0; j <= W; j++) {
      cmax(dp[i][j], dp[par][j]);
    }
    if (i != 0) {
      for (int j = 0; j <= W - w[i]; j++) {
        cmax(dp[i][j + w[i]], dp[par][j] + v[i]);
      }
      for (int j = 1; j <= W; j++) {
        cmax(dp[i][j], dp[i][j - 1]);
      }
    } else {
      for (int i = w[0]; i <= W; i++) {
        dp[0][i] = v[0];
      }
    }
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--;

    vector<int> vvs;
    vector<int> vws;
    int ver = x;
    while (ver >= sz) {
      vvs.push_back(v[ver]);
      vws.push_back(w[ver]);
      ver = (i - 1) / 2;
    }
    int ans = 0;
    for (int bit = 0; bit < (1 << vvs.size()); bit++) {
      int ws = 0;
      int vsum = 0;
      for (int i = 0; i < vvs.size(); i++) {
        if ((bit >> i) & 1) {
          ws += vvs[i];
          vsum += vws[i];
        }
      }
      if (ws > y)
        continue;
      cmax(ans, vsum + dp[ver][y - ws]);
    }
    cout << ans << "\\n";
  }
  return 0;
}