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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for (int i = (x), i##_len = (int)(n); i < i##_len; ++i)
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rreps(i, n) for (int i = (int)(n); i > 0; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\\n'

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;

constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1},
              dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

vector<int> V, W;
constexpr int MAXW = 100000;
vector<vector<int>> dp((1 << 9), vector<int>(MAXW, 0));

int dfs(int v, int w) {
  if (w < 0) {
    return -IINF;
  } else if (v < (1 << 9)) {
    return dp[v][w];
  }
  return max(dfs(v >> 1, w), dfs(v >> 1, w - W[v]) + V[v]);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int N;
  cin >> N;

  V.resize(N + 1);
  W.resize(N + 1);
  reps(i, N) { cin >> V[i] >> W[i]; }

  REP(i, 1, min(N + 1, 1 << 9)) {
    rep(j, MAXW + 1) {
      chmax(dp[i][j], dp[i >> 1][j]);
      if (j - 1 >= 0) {
        chmax(dp[i][j], dp[i][j - 1]);
      }
      if (j - W[i] >= 0) {
        chmax(dp[i][j], dp[i >> 1][j - W[i]] + V[i]);
      }
    }
  }

  int Q;
  cin >> Q;

  while (Q--) {
    int v, L;
    cin >> v >> L;
    cout << dfs(v, L) << '\\n';
  }

  cout << flush;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for (int i = (x), i##_len = (int)(n); i < i##_len; ++i)
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rreps(i, n) for (int i = (int)(n); i > 0; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\\n'

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;

constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1},
              dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

vector<int> V, W;
map<int, map<int, int>> dp;

int dfs(int v, int w) {
  if (w < 0) {
    return -IINF;
  } else if (v == 0) {
    return 0;
  }

  auto it = dp.find(v);
  if (it != dp.end()) {
    auto it2 = dp[v].find(w);
    if (it2 != dp[v].end()) {
      return dp[v][w];
    }
  }

  return dp[v][w] = max(dfs(v >> 1, w), dfs(v >> 1, w - W[v]) + V[v]);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int N;
  cin >> N;

  V.resize(N + 1);
  W.resize(N + 1);
  reps(i, N) { cin >> V[i] >> W[i]; }

  int Q;
  cin >> Q;

  while (Q--) {
    int v, L;
    cin >> v >> L;
    cout << dfs(v, L) << '\\n';
  }

  cout << flush;

  return 0;
}