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

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[300010], dp[300010], b[300010];
map<int, ll> mp[2048];
int main() {
  int n;
  while (cin >> n) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", b + i, a + i);
    }
    mp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      dp[i] = dp[i / 2] + 1;
      int tmp = a[i], tmp1 = b[i];
      if (dp[i] <= 11) {
        mp[i] = mp[i / 2];
        for (auto it : mp[i / 2]) {
          if (it.fs + tmp <= 100000)
            mp[i][it.fs + tmp] = max(mp[i][it.fs + tmp], it.sc + tmp1);
        }
        ll tmp2 = 0, tmp3;
        for (auto it : mp[i]) {
          tmp3 = it.sc;
          mp[i][it.fs] = max(tmp3, tmp2);
          tmp2 = mp[i][it.fs];
        }
      }
    }
    int q;
    scanf("%d", &q);
    int x, l;
    vector<pair<int, int>> v;
    for (int i = 0; i < q; ++i) {
      scanf("%d%d", &x, &l);
      v.clear();
      while (dp[x] > 11) {
        v.push_back(mk(a[x], b[x]));
        x /= 2;
      }
      ans = 0;
      for (int i = 0; i < (1 << v.size()); ++i) {
        int tmp = 0;
        ll tmp1 = 0, tmp2;
        for (int j = 0; j < v.size(); ++j) {
          if (i & (1 << j)) {
            tmp += v[j].fs;
            tmp1 += v[j].sc;
            if (tmp > l) {
              break;
            }
          }
        }
        if (tmp > l) {
          continue;
        }
        tmp2 = (--mp[x].upper_bound(l - tmp))->sc;
        ans = max(ans, tmp2 + tmp1);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int a[300010], dp[300010], b[300010];
map<int, ll> mp[256];
int main() {
  int n;
  while (cin >> n) {
    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", b + i, a + i);
    }
    mp[0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      dp[i] = dp[i / 2] + 1;
      int tmp = a[i], tmp1 = b[i];
      if (dp[i] <= 8) {
        mp[i] = mp[i / 2];
        for (auto it : mp[i / 2]) {
          if (it.fs + tmp <= 100000)
            mp[i][it.fs + tmp] = max(mp[i][it.fs + tmp], it.sc + tmp1);
        }
        ll tmp2 = 0, tmp3;
        for (auto it : mp[i]) {
          tmp3 = it.sc;
          mp[i][it.fs] = max(tmp3, tmp2);
          tmp2 = mp[i][it.fs];
        }
      }
    }
    int q;
    scanf("%d", &q);
    int x, l;
    vector<pair<int, int>> v;
    for (int i = 0; i < q; ++i) {
      scanf("%d%d", &x, &l);
      v.clear();
      while (dp[x] > 8) {
        v.push_back(mk(a[x], b[x]));
        x /= 2;
      }
      ans = 0;
      for (int i = 0; i < (1 << v.size()); ++i) {
        int tmp = 0;
        ll tmp1 = 0, tmp2;
        for (int j = 0; j < v.size(); ++j) {
          if (i & (1 << j)) {
            tmp += v[j].fs;
            tmp1 += v[j].sc;
            if (tmp > l) {
              break;
            }
          }
        }
        if (tmp > l) {
          continue;
        }
        tmp2 = (--mp[x].upper_bound(l - tmp))->sc;
        ans = max(ans, tmp2 + tmp1);
      }
      printf("%lld\\n", ans);
    }
  }
  return 0;
}