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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

ll d[(1 << 9) + 1][100001];

ll pow(ll a, ll b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    ll d = pow(a, b / 2);
    return (d * d) % MOD;
  } else {
    ll d = pow(a, b - 1);
    return (a * d) % MOD;
  }
}

int main() {
  ll n, q;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 0; i < (1 << 9); i++) {
    for (int j = 0; j < 100001; j++) {
      d[i][j] = 0;
    }
  }

  ll half = min(((ll)1 << 9L), n + 1);
  for (int i = 1; i < half; i++) {
    for (int j = 0; j < 100001; j++) {
      d[i][j] = max(d[i][j], d[i / 2][j]);
      if (j - w[i] >= 0) {
        d[i][j] = max(d[i][j], d[i / 2][j - w[i]] + v[i]);
      }
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    ll p, l;
    cin >> p >> l;
    if (p < half) {
      cout << d[p][l] << endl;
    } else {
      vector<ll> items;
      ll tmp = p;
      while (tmp >= half) {
        items.push_back(tmp);
        tmp /= 2;
      }
      ll ans = d[tmp][l];
      ll cnt = items.size();
      for (int j = 0; j < (1 << cnt); j++) {
        ll tmp_v = 0, tmp_w = 0;
        for (int k = 0; k < cnt; k++) {
          if (((j >> k) & 1) == 1) {
            tmp_v += v[items[k]];
            tmp_w += w[items[k]];
          }
        }
        if (l - tmp_w >= 0) {
          ans = max(ans, d[tmp][l - tmp_w] + tmp_v);
        }
      }

      cout << ans << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

ll d[(1 << 9) + 1][100001];

ll pow(ll a, ll b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    ll d = pow(a, b / 2);
    return (d * d) % MOD;
  } else {
    ll d = pow(a, b - 1);
    return (a * d) % MOD;
  }
}

int main() {
  ll n, q;
  cin >> n;
  vector<ll> v(n + 1), w(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 0; i < (1 << 9); i++) {
    for (int j = 0; j < 100001; j++) {
      d[i][j] = 0;
    }
  }

  ll half = min(((ll)1 << 9L), n + 1);
  for (int i = 1; i < half; i++) {
    for (int j = 0; j < 100001; j++) {
      d[i][j] = max(d[i][j], d[i / 2][j]);
      if (j - w[i] >= 0) {
        d[i][j] = max(d[i][j], d[i / 2][j - w[i]] + v[i]);
      }
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    ll p, l;
    cin >> p >> l;
    if (p < half) {
      cout << d[p][l] << endl;
    } else {
      ll ans = 0;
      ll tmp = p - half + 1;
      ll cnt = 0;
      while (tmp > 0) {
        cnt++;
        tmp /= 2;
      }
      for (int j = 0; j < (1 << cnt); j++) {
        ll tmp_v = 0, tmp_w = 0;
        for (int k = 0; k < cnt; k++) {
          if ((j >> k) & 1) {
            tmp_v += v[pow(2L, k) + half - 1];
            tmp_w += w[pow(2L, k) + half - 1];
          }
        }
        if (l - tmp_w > 0) {
          ans = max(ans, d[half - 1][l - tmp_w] + tmp_v);
        }
      }

      cout << ans << endl;
    }
  }

  return 0;
}