Problem p02648 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

map<ll, ll> m;
vector<vector<ll>> qq;
ll NN;
ll c = 0;
vector<ll> VV;
vector<ll> WW;

void dfs(ll n) {
  if (n > NN) {
    return;
  }
  m.insert({n, c});
  c++;
  dfs(n * 2);
  dfs(n * 2 + 1);
}

ll dp[20][100010];
ll cur = 0;
ll ans[(size_t)1e5];
ll cur2 = 0;

void dfs2(const ll n, const ll depth) {
  if (n > NN) {
    return;
  }
  const ll kTh = 0;
  if (depth <= kTh) {
    REP(w, 100001) {
      if (w >= WW[n - 1]) {
        dp[depth][w] =
            max(dp[depth - 1][w - WW[n - 1]] + VV[n - 1], dp[depth - 1][w]);
      } else {
        dp[depth][w] = dp[depth - 1][w];
      }
    }
  }
  while (cur < qq.size() && qq[cur][1] == n) {
    ll z = qq[cur][2];
    if (depth <= kTh) {
      ans[qq[cur][3]] = dp[depth][z];
    } else {
      vector<pair<ll, ll>> v;
      v.reserve(pow(2, depth - kTh));
      v.emplace_back(0, 0);
      ll nn = n;
      ll ddepth = depth;
      while (ddepth > kTh) {
        ll vs = v.size();
        REP(j, vs) {
          v.emplace_back(v[j].first + VV[nn - 1], v[j].second + WW[nn - 1]);
        }
        ddepth--;
        nn /= 2;
      }
      ll m = 0;
      REP(i, v.size()) {
        if (z >= v[i].second) {
          m = max(m, v[i].first + dp[ddepth][z - v[i].second]);
        }
      }
      ans[qq[cur][3]] = m;
    }
    cur++;
  }
  dfs2(n * 2, depth + 1);
  dfs2(n * 2 + 1, depth + 1);
}

void solve(long long N, std::vector<long long> V, std::vector<long long> W,
           long long Q, std::vector<long long> v, std::vector<long long> L) {
  VV.swap(V);
  WW.swap(W);
  NN = N;
  dfs(1);
  qq.resize(Q);
  REP(i, Q) { qq[i] = {m[v[i]], v[i], L[i], i}; }
  sort(ALL(qq));
  dfs2(1, 1);
  REP(i, Q) { cout << ans[i] << endl; }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> V(N);
  std::vector<long long> W(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> v(Q);
  std::vector<long long> L(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &v[i]);
    scanf("%lld", &L[i]);
  }
  solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdint.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

map<ll, ll> m;
vector<vector<ll>> qq;
ll NN;
ll c = 0;
vector<ll> VV;
vector<ll> WW;

void dfs(ll n) {
  if (n > NN) {
    return;
  }
  m.insert({n, c});
  c++;
  dfs(n * 2);
  dfs(n * 2 + 1);
}

ll dp[20][100010];
ll cur = 0;
ll ans[(size_t)1e5];
ll cur2 = 0;

void dfs2(const ll n, const ll depth) {
  if (n > NN) {
    return;
  }
  const ll kTh = 9;
  if (depth <= kTh) {
    REP(w, 100001) {
      if (w >= WW[n - 1]) {
        dp[depth][w] =
            max(dp[depth - 1][w - WW[n - 1]] + VV[n - 1], dp[depth - 1][w]);
      } else {
        dp[depth][w] = dp[depth - 1][w];
      }
    }
  }
  while (cur < qq.size() && qq[cur][1] == n) {
    ll z = qq[cur][2];
    if (depth <= kTh) {
      ans[qq[cur][3]] = dp[depth][z];
    } else {
      vector<pair<ll, ll>> v;
      v.reserve(pow(2, depth - kTh));
      v.emplace_back(0, 0);
      ll nn = n;
      ll ddepth = depth;
      while (ddepth > kTh) {
        ll vs = v.size();
        REP(j, vs) {
          v.emplace_back(v[j].first + VV[nn - 1], v[j].second + WW[nn - 1]);
        }
        ddepth--;
        nn /= 2;
      }
      ll m = 0;
      REP(i, v.size()) {
        if (z >= v[i].second) {
          m = max(m, v[i].first + dp[ddepth][z - v[i].second]);
        }
      }
      ans[qq[cur][3]] = m;
    }
    cur++;
  }
  dfs2(n * 2, depth + 1);
  dfs2(n * 2 + 1, depth + 1);
}

void solve(long long N, std::vector<long long> V, std::vector<long long> W,
           long long Q, std::vector<long long> v, std::vector<long long> L) {
  VV.swap(V);
  WW.swap(W);
  NN = N;
  dfs(1);
  qq.resize(Q);
  REP(i, Q) { qq[i] = {m[v[i]], v[i], L[i], i}; }
  sort(ALL(qq));
  dfs2(1, 1);
  REP(i, Q) { cout << ans[i] << endl; }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> V(N);
  std::vector<long long> W(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &V[i]);
    scanf("%lld", &W[i]);
  }
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> v(Q);
  std::vector<long long> L(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &v[i]);
    scanf("%lld", &L[i]);
  }
  solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L));
  return 0;
}