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

#include <bits/stdc++.h>
#define bug1(x) cout << #x << " = " << x << endl
#define bug2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << endl
#define bug3(x, y, z)                                                          \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << endl
#define bug4(x, y, z, m)                                                       \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << " " << #m << " = " << m << endl

using namespace std;

class Debugger {
public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }

  template <typename T> void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};

const int MAX = 100001;

class TaskD {
private:
  Debugger debugger;

public:
  void solveOne(int it) {
    int n;
    cin >> n;
    vector<int> values(n), weight(n);
    for (int i = 0; i < n; i++) {
      cin >> values[i] >> weight[i];
    }
    int limit = min(1 << 9, n);
    vector<vector<int>> dp(limit, vector<int>(MAX, 0));
    for (int i = 0; i < MAX; i++) {
      dp[0][i] = i >= weight[0] ? values[0] : 0;
    }
    for (int u = 1; u < limit; u++) {
      int p = (u - 1) / 2;
      for (int w = 0; w < MAX; w++) {
        dp[u][w] = dp[p][w];
        if (w - weight[u] >= 0) {
          dp[u][w] = max(dp[u][w], dp[p][w - weight[u]] + values[u]);
        }
      }
    }

    int q;
    cin >> q;
    while (q--) {
      int u, maxWeight;
      cin >> u >> maxWeight;
      vector<int> nodes;
      while (u - 1 >= limit) {
        nodes.push_back(u - 1);
        u /= 2;
      }
      int ans = 0;
      int sz = nodes.size();
      for (int mask = 0; mask < (1 << sz); mask++) {
        int w = 0, v = 0;
        for (int i = 0; i < sz; i++) {
          if (mask & (1 << i)) {
            w += weight[nodes[i]];
            v += values[nodes[i]];
          }
        }
        if (w <= maxWeight) {
          ans = max(ans, dp[u - 1][maxWeight - w] + v);
        }
      }
      cout << ans << endl;
    }
  }

  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  TaskD solver;
  solver.solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define bug1(x) cout << #x << " = " << x << endl
#define bug2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << endl
#define bug3(x, y, z)                                                          \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << endl
#define bug4(x, y, z, m)                                                       \\
  cout << #x << " = " << x << " " << #y << " = " << y << " " << #z << " = "    \\
       << z << " " << #m << " = " << m << endl

using namespace std;

class Debugger {
public:
  template <typename T>
  void printVector(vector<T> const &vec, bool printSize = true) {
    if (printSize) {
      cout << vec.size() << endl;
    }
    for (auto &elem : vec) {
      cout << elem << " ";
    }
    cout << endl;
  }

  template <typename T> void printMatrix(vector<vector<T>> const &matrix) {
    cout << matrix.size() << " ";
    if (!matrix.empty()) {
      cout << matrix[0].size();
    }
    cout << endl;
    for (auto &vec : matrix) {
      printVector(vec, false);
    }
    cout << endl;
  }
};

const int MAXN = 10000005;

class TaskD {
private:
  Debugger debugger;

public:
  void solveOne(int it) {
    int n;
    // cin >> n;
    scanf("%d", &n);
    // n = 100000;
    vector<int> values(n), weight(n);
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &values[i], &weight[i]);
      // cin >> values[i] >> weight[i];
      // values[i] = 0;
      // weight[i] = 0;
    }
    vector<int> where(1 << 10);
    for (int i = 0; i < 10; i++) {
      where[1 << i] = i;
    }
    int q;
    // cin >> q;
    scanf("%d", &q);
    // q = 100000;
    while (q--) {
      int u, limit;
      // cin >> u >> limit;
      scanf("%d %d", &u, &limit);
      // u = n;
      // limit = 100000;
      vector<int> nodes;
      while (u) {
        nodes.push_back(u - 1);
        u /= 2;
      }
      int sz = nodes.size();
      int mid = sz / 2;
      vector<pair<int, int>> all(1 << mid);
      for (int mask = 0; mask < (1 << mid); mask++) {
        int w = 0, v = 0;
        int tmp = mask;
        while (tmp) {
          int last = tmp & -tmp;
          tmp -= last;
          w += weight[nodes[where[last]]];
          v += values[nodes[where[last]]];
        }
        all[mask] = {w, v};
      }
      sort(all.begin(), all.end());
      int len = 1 << mid;
      for (int i = 1; i < len; i++) {
        all[i].second = max(all[i].second, all[i - 1].second);
      }
      int ans = 0;
      int newMid = sz - mid;
      for (int mask = 0; mask < (1 << newMid); mask++) {
        int w = 0, v = 0;
        int tmp = mask;
        while (tmp) {
          int last = tmp & -tmp;
          tmp -= last;
          w += weight[nodes[where[last] + mid]];
          v += values[nodes[where[last] + mid]];
        }
        if (w > limit) {
          continue;
        }
        auto it =
            upper_bound(all.begin(), all.end(), make_pair(limit - w, (int)1e9));
        it--;
        ans = max(ans, (*it).second + v);
      }
      printf("%d\\n", ans);
      // cout << ans << endl;
    }
  }

  void solve() {
    int tc = 1;
    for (int it = 1; it <= tc; it++) {
      solveOne(it);
    }
  }
};

int main() {
  // ios_base::sync_with_stdio(false);
  TaskD solver;
  solver.solve();
  return 0;
}