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

#include <bits/stdc++.h>
using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;
vector<vector<int>> v;
vector<vector<int>> dp;

int f(int x, int y) {
  if (dp[x][y] != -1)
    return dp[x][y];
  else if (x == 0) {
    if (y >= v[0][0])
      dp[x][y] = v[0][1];
    else
      dp[x][y] = 0;
  } else if (y < v[x][0])
    dp[x][y] = f((x - 1) / 2, y);
  else {
    dp[x][y] = max(f((x - 1) / 2, y), f((x - 1) / 2, y - v[x][0]) + v[x][1]);
  }
  return dp[x][y];
}

int main() {
  int N;
  cin >> N;
  v.resize(N);
  for (int i = 0; i < N; i++)
    v[i].resize(2);
  for (int i = 0; i < N; i++)
    cin >> v[i][1] >> v[i][0];
  int M = 0, Q;
  cin >> Q;
  vector<vector<int>> q(Q, vector<int>(2));
  for (int i = 0; i < Q; i++) {
    cin >> q[i][0] >> q[i][1];
    q[i][0]--;
    chmax(M, q[i][1]);
  }
  dp.resize(min(N, (1 << 10)));
  for (auto &x : dp)
    x.assign(M + 1, -1);
  for (int i = 0; i < (int)dp.size(); i++) {
    for (int j = 0; j < M + 1; j++)
      f(i, j);
  }
  for (int i = 0; i < Q; i++) {
    int p = q[i][0];
    int L = q[i][1];
    if (p < (int)dp.size()) {
      printf("%d\\n", dp[p][L]);
    } else {
      int ans = 0;
      int w = p;
      vector<int> t;
      while (w >= (int)dp.size()) {
        t.push_back(w);
        w = (w - 1) / 2;
      }
      int cnt = t.size();
      for (int i = 0; i < (1 << cnt); i++) {
        int a = 0, b = L;
        for (int j = 0; j < cnt; j++) {
          if (i & (1 << j)) {
            a += v[t[j]][1];
            b -= v[t[j]][0];
            if (b < 0)
              break;
          }
        }
        if (b < 0)
          continue;
        chmax(ans, a + dp[w][b]);
      }
      printf("%d\\n", ans);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;
vector<vector<int>> v;
vector<vector<int>> dp;

int f(int x, int y) {
  if (dp[x][y] != -1)
    return dp[x][y];
  else if (x == 0) {
    if (y >= v[0][0])
      dp[x][y] = v[0][1];
    else
      dp[x][y] = 0;
  } else if (y < v[x][0])
    dp[x][y] = f((x - 1) / 2, y);
  else {
    dp[x][y] = max(f((x - 1) / 2, y), f((x - 1) / 2, y - v[x][0]) + v[x][1]);
  }
  return dp[x][y];
}

int main() {
  int N;
  cin >> N;
  v.resize(N);
  for (int i = 0; i < N; i++)
    v[i].resize(2);
  for (int i = 0; i < N; i++)
    cin >> v[i][1] >> v[i][0];
  int M = 0, Q;
  cin >> Q;
  vector<vector<int>> q(Q, vector<int>(2));
  for (int i = 0; i < Q; i++) {
    cin >> q[i][0] >> q[i][1];
    q[i][0]--;
    chmax(M, q[i][1]);
  }
  dp.resize(min(N, (1 << 10)));
  for (auto &x : dp)
    x.assign(M + 1, -1);
  for (int i = 0; i < Q; i++) {
    int p = q[i][0];
    int L = q[i][1];
    if (p < (int)dp.size()) {
      printf("%d\\n", f(p, L));
    } else {
      int ans = 0;
      int w = p;
      vector<int> t;
      while (w >= (int)dp.size()) {
        t.push_back(w);
        w = (w - 1) / 2;
      }
      int cnt = t.size();
      for (int i = 0; i < (1 << cnt); i++) {
        int a = 0, b = 0;
        for (int j = 0; j < cnt; j++) {
          if (i & (1 << j)) {
            a += v[t[j]][1];
            b += v[t[j]][0];
            if (b > L)
              break;
          }
        }
        if (b > L)
          continue;
        chmax(ans, a + f(w, L - b));
      }
      printf("%d\\n", ans);
    }
  }
}