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

/**
 *    author:  otera
 **/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

// #define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<int> v, w;

void solve() {
  int n;
  cin >> n;
  v.resize(n);
  w.resize(n);
  rep(i, n) { cin >> v[i] >> w[i]; }
  static int dp[520][100100];
  rep(i, 520) {
    rep(j, 100100) { dp[i][j] = 0; }
  }
  for (int j = w[0]; j < 100100; ++j) {
    dp[0][j] = v[0];
  }
  for (int i = 1; i < min(511, n); ++i) {
    int p = (i - 1) / 2;
    for (int j = 0; j < 100100; ++j) {
      chmax(dp[i][j], dp[p][j]);
      if (j + w[i] < 100100)
        chmax(dp[i][j + w[i]], dp[p][j] + v[i]);
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int e, l;
    cin >> e >> l;
    --e;
    if (e < 511) {
      cout << dp[e][l] << endl;
      continue;
    }
    vector<int> es;
    int now = e;
    while (now >= 511) {
      es.push_back(now);
      now = (now - 1) / 2;
    }
    int sz = (int)es.size();
    int ans = 0;
    for (int bit = 0; bit < (1 << sz); ++bit) {
      int sw = 0, sv = 0;
      rep(j, sz) {
        if (bit & (1 << j)) {
          sw += w[es[j]];
          sv += v[es[j]];
        }
      }
      if (sw <= l) {
        chmax(ans, dp[now][l - sw] + sv);
      }
    }
    cout << ans << endl;
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  otera
 **/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf = 1e9 + 7;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<int> v, w;

void solve() {
  int n;
  cin >> n;
  v.resize(n);
  w.resize(n);
  rep(i, n) { cin >> v[i] >> w[i]; }
  static int dp[530][100100];
  rep(i, 530) {
    rep(j, 100100) { dp[i][j] = 0; }
  }
  for (int j = w[0]; j < 100100; ++j) {
    dp[0][j] = v[0];
  }
  for (int i = 1; i < min(511LL, n); ++i) {
    int p = (i - 1) / 2;
    for (int j = 0; j < 100100; ++j) {
      chmax(dp[i][j], dp[p][j]);
      if (j + w[i] < 100100)
        chmax(dp[i][j + w[i]], dp[p][j] + v[i]);
    }
  }
  int q;
  cin >> q;
  rep(i, q) {
    int e, l;
    cin >> e >> l;
    --e;
    if (e < 511) {
      cout << dp[e][l] << endl;
      continue;
    }
    vector<int> es;
    int now = e;
    while (now >= 511) {
      es.push_back(now);
      now = (now - 1) / 2;
    }
    int sz = (int)es.size();
    int ans = 0;
    for (int bit = 0; bit < (1 << sz); ++bit) {
      int sw = 0, sv = 0;
      rep(j, sz) {
        if (bit & (1 << j)) {
          sw += w[es[j]];
          sv += v[es[j]];
        }
      }
      if (sw <= l) {
        chmax(ans, dp[now][l - sw] + sv);
      }
    }
    cout << ans << endl;
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout << fixed << setprecision(10);
  // int t; cin >> t; rep(i, t)solve();
  solve();
  return 0;
}