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

#include <bits/stdc++.h>
using namespace std;
#define int long long

vector<int> V(0), W(0);
int b;
vector<vector<int>> dp(0, vector<int>(0, 0));

int solve(int v, int L) {
  if (v < b) {
    return dp[v][L];
  } else if (W[v] <= L) {
    return max(solve(v / 2, L), solve(v / 2, L - W[v]) + V[v]);
  } else {
    return solve(v / 2, L);
  }
}

signed main() {
  int N, i, j;
  scanf("%lld", &N);
  V.assign(N + 1, 0);
  W.assign(N + 1, 0);
  //	vector<int> V(N + 1), W(N + 1);
  for (i = 1; i <= N; i++) {
    scanf("%lld%lld", &V[i], &W[i]);
  }
  b = min(N + 1, 1ll << 10);
  int Lmax = 100000;
  dp.assign(b, vector<int>(Lmax + 1, 0));
  for (i = 1; i < b; i++) {
    for (j = 0; j <= Lmax; j++) {
      dp[i][j] = dp[i / 2][j];
      if (j >= W[i]) {
        dp[i][j] = max(dp[i][j], dp[i / 2][j - W[i]] + V[i]);
      }
      //			printf("dp[%lld][%lld] = %lld\\n", i, j,
      //dp[i][j]);
    }
  }

  int Q;
  scanf("%lld", &Q);
  while (Q--) {
    int v, L;
    scanf("%lld%lld", &v, &L);
    printf("%lld\\n", solve(v, L));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define all(v) begin(v), end(v)
#define pb(a) push_back(a)
#define fr first
#define sc second
#define INF 2000000000

#define X real()
#define Y imag()
#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).X, (b).X) && EQ((a).Y, (b).Y))
#define LE(n, m) ((n) < (m) + EPS)
#define LEQ(n, m) ((n) <= (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define GEQ(n, m) ((n) + EPS >= (m))

typedef vector<int> VI;
typedef vector<VI> MAT;
typedef pair<int, int> pii;
typedef long long ll;

typedef complex<double> P;
typedef pair<P, P> L;
typedef pair<P, double> C;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};
int const MOD = 1000000007;

namespace std {
bool operator<(const P &a, const P &b) {
  return a.X != b.X ? a.X < b.X : a.Y < b.Y;
}
} // namespace std

int N, W;
// int v[210], w[210];
int VMAX, WMAX;
// int dp[200010];

int solve1(vector<int> &v, vector<int> &w) {
  VMAX = -1, WMAX = -1;
  //   cin >> N >> W;
  rep(i, 0, N) {
    //       cin >> v[i] >> w[i];
    VMAX = max(VMAX, v[i]);
    WMAX = max(WMAX, w[i]);
  }

  // Dataset 1: 半分全列挙
  int ans = 0;
  vector<pii> v1, v2;
  int range1 = N / 2, range2 = N - range1;
  for (int i = 0; i < (1 << range1); i++) {
    int VSUM = 0, WSUM = 0;
    rep(j, 0, range1) {
      if (i >> j & 1) {
        VSUM += v[j];
        WSUM += w[j];
      }
    }
    if (WSUM > W)
      continue;
    v1.pb(pii(WSUM, VSUM));
  }

  for (int i = 0; i < (1 << range2); i++) {
    int VSUM = 0, WSUM = 0;
    rep(j, 0, range2) {
      if (i >> j & 1) {
        int idx = j + range1;
        VSUM += v[idx];
        WSUM += w[idx];
      }
    }
    if (WSUM > W)
      continue;
    v2.pb(pii(WSUM, VSUM));
  }

  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  int t = 0, ma = 0;
  repr(i, v1.size() - 1, 0) {
    while (t < v2.size() && v1[i].fr + v2[t].fr <= W) {
      ma = max(ma, v2[t++].sc);
    }
    ans = max(ans, ma + v1[i].sc);
  }
  return ans;
}

signed main() {
  int NN, i;
  scanf("%lld", &NN);
  vector<int> VV(NN + 1), WW(NN + 1);
  for (i = 1; i <= NN; i++) {
    scanf("%lld%lld", &VV[i], &WW[i]);
  }
  int Q;
  scanf("%lld", &Q);
  while (Q--) {
    int v, L;
    scanf("%lld%lld", &v, &L);
    vector<int> vsub(0), wsub(0);
    for (v = v; v > 0; v /= 2) {
      vsub.push_back(VV[v]);
      wsub.push_back(WW[v]);
    }
    N = vsub.size();
    W = L;
    //		solve1(vsub, wsub);
    printf("%lld\\n", solve1(vsub, wsub));
  }
  return 0;
}