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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod

const ll MAXN = (1 << 18);
const ll MAXL = 100001;
const ll MAXK = (1 << 9);

ll ruijou(ll n, ll k) {
  if (!k)
    return 1;
  ll a = ruijou(n, k / 2);
  a = (a * a) % mod;
  return k % 2 ? (a * n) % mod : a;
}
ll Sqrt(ll x) {
  if (x <= 2)
    return 1;
  ll kari = 1;
  while (kari * kari < x) {
    kari *= 2;
  }
  return kari;
}

ll dp[MAXK][MAXL];
ll N;
ll d[MAXN][2];

int main() {
  rep(i, MAXK) rep(j, MAXL) dp[i][j] = -INF;
  cin >> N;
  rep(i, N) rep(j, 2) cin >> d[i][j];
  ll K = Sqrt(N);
  // cout<<K<<endl;
  dp[0][0] = 0;
  dp[0][d[0][1]] = d[0][0];
  rep(i, MAXL - 1) dp[0][i + 1] = max(dp[0][i + 1], dp[0][i]);
  REP(i, 1, K) {
    rep(j, MAXL) {
      dp[i][j] = max(dp[i][j], dp[(i - 1) / 2][j]);
      if (j >= d[i][1])
        dp[i][j] = max(dp[i][j], dp[(i - 1) / 2][j - d[i][1]] + d[i][0]);
    }
  }
  vec ans;
  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll v, L;
    cin >> v >> L;
    v--;
    if (v < K)
      ans.pb(dp[v][L]);
    else {
      ll A = v;
      vec x;
      while (A >= K) {
        x.pb(A);
        A--;
        A /= 2;
      }
      ll M = x.size();
      ll kari = -INF;
      rep(j, (1 << M)) {
        ll value = 0, weigh = 0;
        rep(k, M) if (j & (1 << k)) {
          value += d[x[k]][0];
          weigh += d[x[k]][1];
        }
        // cout<<v<<':'<<A<<":"<<j<<' '<<value<<' '<<weigh<<' '<<L<<endl;
        if (weigh <= L) {
          // cout<<value+dp[A][L-weigh]<<endl;
          kari = max(kari, value + dp[A][L - weigh]);
        }
      }
      ans.pb(kari);
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());
#define ADD(a, b) a = (a + b) % mod

const ll MAXN = (1 << 18);
const ll MAXL = 100001;
const ll MAXK = (1 << 9);

ll ruijou(ll n, ll k) {
  if (!k)
    return 1;
  ll a = ruijou(n, k / 2);
  a = (a * a) % mod;
  return k % 2 ? (a * n) % mod : a;
}
ll Sqrt(ll x) {
  ll BIG = x + 10, SMALL = 0;
  while (BIG > SMALL + 1) {
    ll MID = (BIG + SMALL) / 2;
    if (MID * MID >= x)
      BIG = MID;
    else
      SMALL = MID;
  }
  return BIG;
}

ll dp[MAXK][MAXL];
ll N;
ll d[MAXN][2];

int main() {
  rep(i, MAXK) rep(j, MAXL) dp[i][j] = -INF;
  cin >> N;
  rep(i, N) rep(j, 2) cin >> d[i][j];
  ll K = Sqrt(N);
  // cout<<K<<endl;
  dp[0][0] = 0;
  dp[0][d[0][1]] = d[0][0];
  rep(i, MAXL - 1) dp[0][i + 1] = max(dp[0][i + 1], dp[0][i]);
  REP(i, 1, K) {
    rep(j, MAXL) {
      dp[i][j] = max(dp[i][j], dp[(i - 1) / 2][j]);
      if (j >= d[i][1])
        dp[i][j] = max(dp[i][j], dp[(i - 1) / 2][j - d[i][1]] + d[i][0]);
    }
  }
  vec ans;
  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll v, L;
    cin >> v >> L;
    v--;
    if (v < K)
      ans.pb(dp[v][L]);
    else {
      ll A = v;
      vec x;
      while (A >= K) {
        x.pb(A);
        A--;
        A /= 2;
      }
      ll M = x.size();
      ll kari = -INF;
      rep(j, (1 << M)) {
        ll value = 0, weigh = 0;
        rep(k, M) if (j & (1 << k)) {
          value += d[x[k]][0];
          weigh += d[x[k]][1];
        }
        // cout<<v<<':'<<A<<":"<<j<<' '<<value<<' '<<weigh<<' '<<L<<endl;
        if (weigh <= L) {
          // cout<<value+dp[A][L-weigh]<<endl;
          kari = max(kari, value + dp[A][L - weigh]);
        }
      }
      ans.pb(kari);
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}