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

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPS(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000000000 // 14
#define N 1000000
#define L 100100

ll M = 512;
vector<vector<ll>> dp(M, vector<ll>(L, 0));
vector<ll> val(N);
vector<ll> wei(N);

int main() {
  ll n, v, l, q;
  cin >> n;
  REP(i, n) cin >> val[i + 1] >> wei[i + 1];
  cin >> q;
  FOR(i, 1, min(M - 1, n)) {
    REP(j, L) {
      if (j - wei[i] >= 0)
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - wei[i]] + val[i]);
      else
        dp[i][j] = dp[i / 2][j];
      if (j >= 1)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
    }
  }
  REP(i, q) {
    cin >> v >> l;
    if (v < M) {
      cout << dp[v][l] << endl;
    } else {
      ll ans = 0;
      vector<ll> jelv;
      vector<ll> jelw;
      while (v >= M) {
        jelv.pb(val[v]);
        jelw.pb(wei[v]);
        v /= 2;
      }
      ll len = jelv.size();
      REP(j, (1ll << len)) {
        ll twei = 0;
        ll tval = 0;
        REP(p, len) {
          if ((j >> p) % 2) {
            tval += jelv[p];
            twei += jelw[p];
            if (twei > l) {
              break;
            }
          }
        }
        if (twei <= l)
          ans = max(ans, dp[v][l - twei] + tval);
      }
      cout << ans << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long double ld;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPS(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000000000 // 14
#define N 1000000
#define L 110000

ll M = 512;
vector<vector<ll>> dp(M + 10, vector<ll>(L, 0));
vector<ll> val(N);
vector<ll> wei(N);

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  ll n, v, l, q;
  cin >> n;
  REP(i, n) cin >> val[i + 1] >> wei[i + 1];
  cin >> q;
  FOR(i, 1, min(M - 1, n)) {
    REP(j, L) {
      if (j - wei[i] >= 0)
        dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - wei[i]] + val[i]);
      else
        dp[i][j] = dp[i / 2][j];
    }
  }
  REP(i, q) {
    cin >> v >> l;
    if (v < M) {
      ll ans = 0;
      REP(j, l + 1) ans = max(ans, dp[v][j]);
      cout << ans << endl;
    } else {
      ll ans = 0;
      vector<ll> jelv;
      vector<ll> jelw;
      while (v >= M) {
        jelv.pb(val[v]);
        jelw.pb(wei[v]);
        v /= 2;
      }
      ll len = jelv.size();
      REP(j, (1ll << len)) REP(k, l + 1) {
        ll tval = dp[v][k];
        ll twei = k;
        REP(p, len) {
          if ((j >> p) % 2) {
            tval += jelv[p];
            twei += jelw[p];
          }
        }
        if (twei <= l) {
          ans = max(ans, tval);
        }
      }
      cout << ans << endl;
    }
  }
}