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

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

const int INF = 1e9;

void enumerate(std::vector<std::pair<int, int>> &items,
               std::vector<std::pair<int, int>> &ps) {
  int L = items.size();
  REP(j, 1 << L) {
    int w = 0, v = 0;
    REP(k, L) {
      if (j >> k & 1)
        v += items[k].first, w += items[k].second;
    }
    ps.emplace_back(w, v);
  }
  sort(ALL(ps));
  int M = 1;
  REP(j, 1 << L) {
    if (ps[M - 1].second < ps[j].second) {
      ps[M++] = ps[j];
    }
  }
  ps.resize(M);
}

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  std::cin >> N;
  std::vector<int> V(N), W(N);
  REP(i, N) std::cin >> V[i] >> W[i];

  int n_perm = std::min(N, (1 << 10) - 1);
  std::vector<std::vector<std::pair<int, int>>> permed(n_perm);

  REP(i, n_perm) {
    std::vector<std::pair<int, int>> items;
    int v = i;
    while (true) {
      items.emplace_back(V[v], W[v]);
      if (v == 0) {
        break;
      }
      v = (v - 1) / 2;
    }

    std::vector<std::pair<int, int>> ps;
    enumerate(items, ps);
    permed[i] = ps;
  }
  /*
  for (auto ps : permed) {
    for (auto p : ps) std::cout << '(' << p.first << ',' << p.second << ')' << '
  '; std::cout << std::endl;
  }
  */

  int Q;
  std::cin >> Q;
  REP(_, Q) {
    int v, L;
    std::cin >> v >> L;
    v--;
    if (v < n_perm) {
      auto ans = std::lower_bound(ALL(permed[v]), std::make_pair(L, INF)) - 1;
      std::cout << ans->second << '\\n';
    } else {
      std::vector<std::pair<int, int>> items;
      while (v >= n_perm) {
        items.emplace_back(V[v], W[v]);
        v = (v - 1) / 2;
      }
      int M = items.size();
      int ans = 0;
      REP(i, 1 << M) {
        int vv = 0, w = 0;
        REP(j, M) if (i >> j & 1) vv += items[j].first, w += items[j].second;
        if (w <= L) {
          auto cand =
              std::lower_bound(ALL(permed[v]), std::make_pair(L - w, INF)) - 1;
          ans = std::max(vv + cand->second, ans);
        }
      }
      std::cout << ans << '\\n';
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

const int INF = 1e9;

void enumerate(std::vector<std::pair<int, int>> &items,
               std::vector<std::pair<int, int>> &ps) {
  int L = items.size();
  REP(j, 1 << L) {
    int w = 0, v = 0;
    REP(k, L) {
      if (j >> k & 1)
        v += items[k].first, w += items[k].second;
    }
    ps.emplace_back(w, v);
  }
  sort(ALL(ps));
  int M = 1;
  REP(j, 1 << L) {
    if (ps[M - 1].second < ps[j].second) {
      ps[M++] = ps[j];
    }
  }
  ps.resize(M);
}

int main() {
  // use scanf in CodeForces!
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);

  int N;
  std::cin >> N;
  std::vector<int> V(N), W(N);
  // REP(i, N) std::cin >> V[i] >> W[i];
  REP(i, N) scanf("%d %d", &V[i], &W[i]);

  int n_perm = std::min(N, (1 << 9) - 1);
  std::vector<std::vector<std::pair<int, int>>> permed(n_perm);

  REP(i, n_perm) {
    std::vector<std::pair<int, int>> items;
    int v = i;
    while (true) {
      items.emplace_back(V[v], W[v]);
      if (v == 0) {
        break;
      }
      v = (v - 1) / 2;
    }

    std::vector<std::pair<int, int>> ps;
    enumerate(items, ps);
    permed[i] = ps;
  }
  /*
  for (auto ps : permed) {
    for (auto p : ps) std::cout << '(' << p.first << ',' << p.second << ')' << '
  '; std::cout << std::endl;
  }
  */

  int Q;
  std::cin >> Q;
  REP(_, Q) {
    int v, L;
    std::cin >> v >> L;
    v--;
    if (v < n_perm) {
      auto ans = std::lower_bound(ALL(permed[v]), std::make_pair(L, INF)) - 1;
      // std::cout << ans->second << '\\n';
      printf("%d\\n", ans->second);
    } else {
      std::vector<std::pair<int, int>> items;
      while (v >= n_perm) {
        items.emplace_back(V[v], W[v]);
        v = (v - 1) / 2;
      }
      int M = items.size();
      int ans = 0;
      REP(i, 1 << M) {
        int vv = 0, w = 0;
        REP(j, M) if (i >> j & 1) vv += items[j].first, w += items[j].second;
        if (w <= L) {
          auto cand =
              std::lower_bound(ALL(permed[v]), std::make_pair(L - w, INF)) - 1;
          ans = std::max(vv + cand->second, ans);
        }
      }
      // std::cout << ans << '\\n';
      printf("%d\\n", ans);
    }
  }
  return 0;
}