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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(), (a).end()
#define int long long
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ

int dp[1025][100001];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<P> v(n + 1);
  rep(i, n) { cin >> v[i + 1].fi >> v[i + 1].se; }
  int x = 0;
  if (n != 1)
    x += log2(n / 2);
  chmin(x, 10);
  for (int i = 1; i < (1 << x); i++) {
    rep(j, 100001) {
      chmax(dp[i][j], dp[i / 2][j]);
      if (j - v[i].se >= 0)
        chmax(dp[i][j], dp[i / 2][j - v[i].se] + v[i].fi);
    }
  }
  int q;
  cin >> q;
  rep(query, q) {
    int V, L;
    cin >> V >> L;
    if (V < (1 << x)) {
      cout << dp[V][L] << '\\n';
    } else {
      vector<P> vec;
      while (V >= (1 << x)) {
        vec.emplace_back(v[V]);
        V /= 2;
      }
      int ans = 0, sz = vec.size();
      rep(i, 1 << sz) {
        int resW = 0, resV = 0;
        rep(j, sz) {
          if (i & (1 << j)) {
            resV += vec[j].fi;
            resW += vec[j].se;
          }
        }
        if (L - resW < 0)
          continue;
        chmax(ans, resV + dp[V][L - resW]);
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <iomanip>
#include <iostream>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(), (a).end()
#define int long long
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
// s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ

int dp[520][100001];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<P> v(n + 1);
  rep(i, n) { cin >> v[i + 1].fi >> v[i + 1].se; }
  int x = 0;
  if (n != 1)
    x += log2(n / 2);
  chmin(x, 9);
  for (int i = 1; i < (1 << x); i++) {
    rep(j, 100001) {
      chmax(dp[i][j], dp[i / 2][j]);
      if (j - v[i].se >= 0)
        chmax(dp[i][j], dp[i / 2][j - v[i].se] + v[i].fi);
    }
  }
  int q;
  cin >> q;
  rep(query, q) {
    int V, L;
    cin >> V >> L;
    if (V < (1 << x)) {
      cout << dp[V][L] << '\\n';
    } else {
      vector<P> vec;
      while (V >= (1 << x)) {
        vec.emplace_back(v[V]);
        V /= 2;
      }
      int ans = 0, sz = vec.size();
      rep(i, 1 << sz) {
        int resW = 0, resV = 0;
        rep(j, sz) {
          if (i & (1 << j)) {
            resV += vec[j].fi;
            resW += vec[j].se;
          }
        }
        if (L - resW < 0)
          continue;
        chmax(ans, resV + dp[V][L - resW]);
      }
      cout << ans << '\\n';
    }
  }
  return 0;
}