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

// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

#include "boost/multiprecision/cpp_dec_float.hpp"
#include "boost/multiprecision/cpp_int.hpp"
namespace mp = boost::multiprecision;
typedef mp::cpp_int LL;
typedef mp::number<mp::cpp_dec_float<1024>>
    DD; // 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)

typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define rrep(i, r, l) for (ll i = r; i >= l; i--)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define endl "\\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void mod_print(ll k) {
  ll P = 1000;
  rep(y, 1, P) {
    ll x = mod(y * k);
    if (x + P >= i_7) {
      x -= i_7;
    }
    if (abs(x) <= P) {
      cout << x << "/" << y << endl;
      return;
    }
  }
  cout << "nun" << endl;
}
#define all(c) c.begin(), c.end()
//////////////////////////

ll sqr(ll n) {
  ll c = 0;
  while (n > 0) {
    n /= 2;
    c++;
  }
  if (c <= 6)
    return 1;
  return 1ll << ((c + 1) / 2);
}
vector<l_l> conv(vector<l_l> v) {
  vector<l_l> res;
  sort(all(v));
  ll mx = -inf;
  for (auto x : v) {
    if (chmax(mx, x.se)) {
      res.pb(x);
    }
  }
  return res;
}

int main() {
  fastio ll n;
  cin >> n;
  ll v[n + 1], w[n + 1];
  rep(i, 1, n) cin >> v[i] >> w[i];
  ll s = sqr(n);
  ll t = s * 2 - 1;

  vector<l_l> dp[t + 1];
  dp[0].pb({0, 0});
  rep(i, 1, t) {
    ll pos = i;
    ll pre = pos / 2;
    for (auto x : dp[pre]) {
      dp[pos].pb(x);
      dp[pos].pb({x.fi + w[pos], x.se + v[pos]}); // おもさ、かち
    }
    dp[pos] = conv(dp[pos]);
  }
  /*rep(i,1,t){
      cout<<i<<":"<<endl;
      for(auto x:dp[i]){
          cout<<x.fi<<" "<<x.se<<endl;
      }
  }*/
  ll q;
  cin >> q;
  while (q--) {
    ll pos, l;
    cin >> pos >> l;
    vector<l_l> res;
    res.pb({0, 0});
    while (pos > t) {
      ll sz = res.size();
      rep(i, 0, sz - 1) {
        l_l x = res[i];
        res.pb({x.fi + w[pos], x.se + v[pos]});
      }
      pos /= 2;
    }

    ll ans = 0;
    for (auto x : res) {
      if (x.fi > l)
        continue;
      ll nok = l - x.fi;
      // ll p=POSU(dp[pos],l_l{nok,0})-1;
      ll p = upper_bound(dp[pos].begin(), dp[pos].end(), (l_l{nok, inf})) -
             dp[pos].begin() - 1;
      if (p >= 0)
        x.se += dp[pos][p].se;
      chmax(ans, x.se);
    }
    cout << ans << endl;
  }

  /*while(q--){
      ll pos,l;cin>>pos>>l;
      ll dp[l+1];
      dp[0]=0;
      rep(i,1,l)dp[i]=-inf*4;
      while(pos>=1){
          ll nx[l+1];
          rep(i,0,l)nx[i]=dp[i];
          rep(i,0,l){
              if(i+w[pos]<=l)chmax(nx[i+w[pos]],dp[i]+v[pos]);
          }
          rep(i,0,l)dp[i]=nx[i];
          pos=pos/2;
      }
      ll mx=0;rep(j,0,l)chmax(mx,dp[j]);
      cout<<mx<<endl;
  }*/

  return 0;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

#include "boost/multiprecision/cpp_dec_float.hpp"
#include "boost/multiprecision/cpp_int.hpp"
namespace mp = boost::multiprecision;
typedef mp::cpp_int LL;
typedef mp::number<mp::cpp_dec_float<1024>>
    DD; // 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)

typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
typedef pair<dd, dd> d_d;
ll inf = (ll)1E16;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define rrep(i, r, l) for (ll i = r; i >= l; i--)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define fi first
#define se second
#define endl "\\n"
#define SORT(v) sort(v.begin(), v.end())
#define ERASE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define POSL(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define POSU(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void mod_print(ll k) {
  ll P = 1000;
  rep(y, 1, P) {
    ll x = mod(y * k);
    if (x + P >= i_7) {
      x -= i_7;
    }
    if (abs(x) <= P) {
      cout << x << "/" << y << endl;
      return;
    }
  }
  cout << "nun" << endl;
}
#define all(c) c.begin(), c.end()
//////////////////////////

int main() {
  fastio ll n;
  cin >> n;
  ll v[n + 1], w[n + 1];
  rep(i, 1, n) cin >> v[i] >> w[i];
  ll q;
  cin >> q;
  while (q--) {
    ll pos, l;
    cin >> pos >> l;
    ll dp[l + 1];
    memset(dp, 0, sizeof(dp));

    while (pos >= 1) {

      ll nx[l + 1];
      rep(i, 0, l) nx[i] = dp[i];
      rep(i, 0, l) {
        if (i + w[pos] <= l)
          chmax(nx[i + w[pos]], dp[i] + v[pos]);
      }
      rep(i, 0, l) dp[i] = nx[i];
      pos = pos / 2;
    }
    ll mx = 0;
    rep(j, 0, l) chmax(mx, dp[j]);
    cout << mx << endl;
  }

  return 0;
}