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

// #pragma warning(disable:4996)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>
// #include <stdio.h>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int N, Q;
VI V, W;
VVLL DP2;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // cout << 1e5 << "\\n";
  // for (int n = 0; n < 1e5; n++) {
  //	cout << "1 1\\n";
  // }
  // cout << 1 << "\\n";
  // cout << 1e5 - 1 << " " << 30 << "\\n";
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (int n = 1; n <= N; n++) {
    cin >> V[n] >> W[n];
  }
  cin >> Q;
  vector<PI> querys(Q);
  int maxl = 0;
  for (int q = 0; q < Q; q++) {
    cin >> querys[q].first >> querys[q].second;
    maxl = max(maxl, querys[q].second);
  }
  DP2.resize(1024, VLL(maxl + 1, -1));
  DP2[1][0] = 0;
  DP2[1][W[1]] = V[1];
  for (int n = 2; n <= min(N, (1 << 10) - 1); n++) {
    VI vs(1, n);
    while (vs.back() > 1) {
      vs.push_back(vs.back() >> 1);
    }
    for (int code = 0; code < (1 << vs.size()); code++) {
      LL w = 0, v = 0;
      for (int t = 0; t < vs.size(); t++) {
        if ((code & (1 << t)) == 0)
          continue;
        w += W[vs[t]];
        v += V[vs[t]];
      }
      if (w > maxl)
        continue;
      DP2[n][w] = max(DP2[n][w], v);
    }
  }
  for (int n = 1; n <= min(N, (1 << 10) - 1); n++) {
    for (int w = 1; w <= maxl; w++) {
      DP2[n][w] = max(DP2[n][w], DP2[n][w - 1]);
    }
  }
  for (int q = 0; q < Q; q++) {
    int n, L;
    n = querys[q].first;
    L = querys[q].second;
    if (n < (1 << 10)) {
      std::cout << DP2[n][L] << "\\n";
    } else {
      VI vs;
      vs.push_back(n);
      while (vs.back() >= (1 << 10)) {
        vs.push_back(vs.back() >> 1);
      }
      LL ans = 0;
      for (int code = 0; code < (1 << vs.size()); code++) {
        LL w = 0, v = 0;
        for (int t = 0; t < vs.size(); t++) {
          if ((code & (1 << t)) == 0)
            continue;
          w += W[vs[t]];
          v += V[vs[t]];
        }
        if (w > L)
          continue;
        int p = (vs.back() >> 1);
        v += DP2[p][L - w];
        ans = max(ans, v);
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

// #pragma warning(disable:4996)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
// #include <boost/multiprecision/cpp_int.hpp>
#include <bitset>
#include <complex>
#include <cstdio>
#include <list>
// #include <stdio.h>

//< in.txt > out.txt
using namespace std;
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
typedef long long LL;
typedef long double LD;
// typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

int N, Q;
VI V, W;
VVLL DP2;

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  // cout << 1e5 << "\\n";
  // for (int n = 0; n < 1e5; n++) {
  //	cout << "1 1\\n";
  // }
  // cout << 1 << "\\n";
  // cout << 1e5 - 1 << " " << 30 << "\\n";
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (int n = 1; n <= N; n++) {
    cin >> V[n] >> W[n];
  }
  cin >> Q;
  vector<PI> querys(Q);
  int maxl = 0;
  for (int q = 0; q < Q; q++) {
    cin >> querys[q].first >> querys[q].second;
    maxl = max(maxl, querys[q].second);
  }
  DP2.resize(1024, VLL(maxl + 1, -1));
  DP2[1][0] = 0;
  DP2[1][W[1]] = V[1];
  for (int n = 2; n <= min(N, (1 << 10) - 1); n++) {
    int p = n >> 1;
    for (int w = 0; w <= maxl; w++) {
      DP2[n][w] = DP2[p][w];
      if (w >= W[n]) {
        if (DP2[p][w - W[n]] != -1) {
          DP2[n][w] = max(DP2[n][w], DP2[p][w - W[n]] + V[n]);
        }
      }
    }
  }
  for (int n = 1; n <= min(N, (1 << 10) - 1); n++) {
    for (int w = 1; w <= maxl; w++) {
      DP2[n][w] = max(DP2[n][w], DP2[n][w - 1]);
    }
  }
  for (int q = 0; q < Q; q++) {
    int n, L;
    n = querys[q].first;
    L = querys[q].second;
    if (n < (1 << 10)) {
      std::cout << DP2[n][L] << "\\n";
    } else {
      VI vs;
      vs.push_back(n);
      while (vs.back() >= (1 << 10)) {
        vs.push_back(vs.back() >> 1);
      }
      LL ans = 0;
      for (int code = 0; code < (1 << vs.size()); code++) {
        LL w = 0, v = 0;
        for (int t = 0; t < vs.size(); t++) {
          if ((code & (1 << t)) == 0)
            continue;
          w += W[vs[t]];
          v += V[vs[t]];
        }
        if (w > L)
          continue;
        int p = (vs.back() >> 1);
        v += DP2[p][L - w];
        ans = max(ans, v);
      }
      cout << ans << "\\n";
    }
  }
  return 0;
}