Language: C++

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
//typedef long long ll;
typedef long long int;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<int> &o) {
    int a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
//const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;

struct Item {
    ll v, w;
};


std::istream &operator>>(std::istream &in, Item &o) {
    cin >> o.v >> o.w;
    return in;
}

vector<int> create_candidates(int v) {
    vector<int> u;
    u.push_back(v);
    while (v > 0) {
        v = (v + 1) / 2 - 1;
        u.push_back(v);
    }
    return u;
}

void from_cache(ll u, ll l, vector<vector<ll>> &cache) {

    ll ans = cache[u][l];
    printf("%lld\n", ans);


}

void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
    auto index = create_candidates(u);

    int fm = [&] {
        int ans = 0;
        for (int i : index) if (i <= fe) cmax(ans, i);
        return ans;
    }();

    vector<int> overs;
    for (int i : index) if (i > fe) overs.push_back(i);

    ll ans = 0;

    int n = overs.size();
    rep(i, 1 << n) {
        ll w = 0, v = 0;
        rep(j, n) {
            if (!((i >> j) & 1)) continue;
            w += items[overs[j]].w;
            v += items[overs[j]].v;
            if (w > l) break;
        }
        if (w > l) continue;
        ll sub = l - w;
        cmax(ans, v + cache[fm][sub]);
    }

    printf("%lld\n", ans);

}

vector<vector<ll>> knapsack(vector<Item> &items, int fe) {
    vector<vector<ll>> dp(fe + 1, vector<ll>(1e5 + 1, -1));
    dp[0][0] = 0;
    auto add = [&](int i, ll j, ll v) {
        if (i > fe) return;
        if (j > 1e5) return;
        if (dp[i][j] == -1) dp[i][j] = v;
        else
            cmax(dp[i][j], v);
    };

    auto p = [](int i) {
        if (i == 0) return -1;
        return (i + 1) / 2 - 1;
    };

    auto get = [&](int i, ll j) -> ll {
        if (i == -1) {
            if (j == 0) return 0;
            return -1;
        }
        return dp[i][j];
    };

    rep(i, fe + 1) {
        Item item = items[i];
        rep(j, 1e5 + 1) {

            ll from = get(p(i), j);
            if (from == -1) continue;
            add(i, ll(j) + item.w, from + item.v);
            add(i, ll(j), from);
        }
    }

    rep(i, fe + 1) {
        rep(j, 1e5 + 1) {
            if (j == 0) continue;
            cmax(dp[i][j], dp[i][j - 1]);
        }
    }

    return dp;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
//    ifstream cin("C:\\Users\\riku\\Downloads\\in15.txt");
    int n;
    cin >> n;

    vector<Item> items(n);
    rep(i, n) {
        cin >> items[i].v >> items[i].w;
    }


//    int f = 1;
    int f = 8;
    int fe = (1 << (f + 1)) - 2;

//    cout << fe << endl;

//    vector<vector<P>> cache(fe + 1);
//
//    for (int i = 0; i <= fe && i < n; i++) {
//        auto index = create_candidates(i);
//        cache[i] = create_table(index, items);
//    }
//

    vector<vector<ll>> cache = knapsack(items, min(fe, n - 1));


    int q;
    cin >> q;
    rep(_, q) {
        int u, l;
        cin >> u >> l;
        u--;

        if (u <= fe) {
            from_cache(u, l, cache);
        } else {
            use_cache(u, l, cache, fe, items);
        }
    }

}


Compiler output:
a.cc:13:19: error: declaration does not declare anything [-fpermissive]
   13 | typedef long long int;
      |                   ^~~
a.cc:17:14: error: 'll' was not declared in this scope
   17 | typedef pair<ll, ll> P;
      |              ^~
a.cc:17:18: error: 'll' was not declared in this scope
   17 | typedef pair<ll, ll> P;
      |                  ^~
a.cc:17:20: error: template argument 1 is invalid
   17 | typedef pair<ll, ll> P;
      |                    ^
a.cc:17:20: error: template argument 2 is invalid
a.cc:18:7: error: 'll' does not name a type
   18 | const ll INF = 10e17;
      |       ^~
a.cc: In function 'std::istream& operator>>(std::istream&, std::queue<int>&)':
a.cc:35:5: error: 'll' was not declared in this scope
   35 |     ll a;
      |     ^~
a.cc:36:11: error: 'a' was not declared in this scope
   36 |     in >> a;
      |           ^
a.cc: At global scope:
a.cc:48:24: error: 'll' was not declared in this scope
   48 | typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
      |                        ^~
a.cc:48:35: error: 'll' was not declared in this scope
   48 | typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
      |                                   ^~
a.cc:48:37: error: template argument 1 is invalid
   48 | typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
      |                                     ^
a.cc:48:37: error: template argument 2 is invalid
a.cc:48:48: error: 'll' was not declared in this scope
   48 | typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
      |                                                ^~
a.cc:48:50: error: template argument 1 is invalid
   48 | typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
      |                                                  ^
a.cc:48:52: error: template argument 1 is invalid
   48 | typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
      |                                                    ^
a.cc:48:52: error: template argument 2 is invalid
a.cc:48:52: error: template argument 3 is invalid
a.cc:51:5: error: 'll' does not name a type
   51 |     ll v, w;
      |     ^~
a.cc: In function 'std::istream& operator>>(std::istream&, Item&)':
a.cc:56:14: error: 'struct Item' has no member named 'v'
   56 |     cin >> o.v >> o.w;
      |              ^
a.cc:56:21: error: 'struct Item' has no member named 'w'
   56 |     cin >> o.v >> o.w;
      |                     ^
a.cc: At global scope:
a.cc:70:6: error: variable or field 'from_cache' declared void
   70 | void from_cache(ll u, ll l, vector<vector<ll>> &cache) {
      |      ^~~~~~~~~~
a.cc:70:17: error: 'll' was not declared in this scope
   70 | void from_cache(ll u, ll l, vector<vector<ll>> &cache) {
      |                 ^~
a.cc:70:23: error: 'll' was not declared in this scope
   70 | void from_cache(ll u, ll l, vector<vector<ll>> &cache) {
      |                       ^~
a.cc:70:43: error: 'll' was not declared in this scope
   70 | void from_cache(ll u, ll l, vector<vector<ll>> &cache) {
      |                                           ^~
a.cc:70:43: error: template argument 1 is invalid
a.cc:70:43: error: template argument 2 is invalid
a.cc:70:45: error: template argument 1 is invalid
   70 | void from_cache(ll u, ll l, vector<vector<ll>> &cache) {
      |                                             ^~
a.cc:70:45: error: template argument 2 is invalid
a.cc:70:49: error: 'cache' was not declared in this scope
   70 | void from_cache(ll u, ll l, vector<vector<ll>> &cache) {
      |                                                 ^~~~~
a.cc:78:6: error: variable or field 'use_cache' declared void
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |      ^~~~~~~~~
a.cc:78:16: error: 'll' was not declared in this scope
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                ^~
a.cc:78:22: error: 'll' was not declared in this scope
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                      ^~
a.cc:78:42: error: 'll' was not declared in this scope
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                                          ^~
a.cc:78:42: error: template argument 1 is invalid
a.cc:78:42: error: template argument 2 is invalid
a.cc:78:44: error: template argument 1 is invalid
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                                            ^~
a.cc:78:44: error: template argument 2 is invalid
a.cc:78:48: error: 'cache' was not declared in this scope
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                                                ^~~~~
a.cc:78:55: error: expected primary-expression before 'int'
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                                                       ^~~
a.cc:78:76: error: expected primary-expression before '&' token
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                                                                            ^
a.cc:78:77: error: 'items' was not declared in this scope
   78 | void use_cache(ll u, ll l, vector<vector<ll>> &cache, int fe, vector<Item> &items) {
      |                                                                             ^~~~~
a.cc:110:15: error: 'll' was not declared in this scope
  110 | vector<vector<ll>> knapsack(vector<Item> &items, int fe) {
      |               ^~
a.cc:110:15: error: template argument 1 is invalid
a.cc:110:15: error: template argument 2 is invalid
a.cc:110:17: error: template argument 1 is invalid
  110 | vector<vector<ll>> knapsack(vector<Item> &items, int fe) {
      |                 ^~
a.cc:110:17: error: template argument 2 is invalid
a.cc: In function 'int knapsack(std::vector<Item>&, int)':
a.cc:111:19: error: 'll' was not declared in this scope
  111 |     vector<vector<ll>> dp(fe + 1, vector<ll>(1e5 + 1, -1));
      |                   ^~
a.cc:111:19: error: template argument 1 is invalid
a.cc:111:19: error: template argument 2 is invalid
a.cc:111:21: error: template argument 1 is invalid
  111 |     vector<vector<ll>> dp(fe + 1, vector<ll>(1e5 + 1, -1));
      |                     ^~
a.cc:111:21: error: template argument 2 is invalid
a.cc:111:44: error: template argument 2 is invalid
  111 |     vector<vector<ll>> dp(fe + 1, vector<ll>(1e5 + 1, -1));
      |                                            ^
a.cc:111:58: error: expression list treated as compound expression in initializer [-fpermissive]
  111 |     vector<vector<ll>> dp(fe + 1, vector<ll>(1e5 + 1, -1));
      |                                                          ^
a.cc:112:7: error: invalid types 'int[int]' for array subscript
  112 |     dp[0][0] = 0;
      |       ^
a.cc:113:27: error: 'll' is not a type
  113 |     auto add = [&](int i, ll j, ll v) {
      |                           ^~
a.cc:113:33: error: 'll' is not a type
  113 |     auto add = [&](int i, ll j, ll v) {
      |                                 ^~
a.cc: In lambda function:
a.cc:116:15: error: invalid types 'int[int]' for array subscript
  116 |         if (dp[i][j] == -1) dp[i][j] = v;
      |               ^
a.cc:116:31: error: invalid types 'int[int]' for array subscript
  116 |         if (dp[i][j] == -1) dp[i][j] = v;
      |                               ^
a.cc:118:20: error: invalid types 'int[int]' for array subscript
  118 |             cmax(dp[i][j], v);
      |                    ^
a.cc:20:20: note: in definition of macro 'cmax'
   20 | #define cmax(x, y) x = max(x, y)
      |                    ^
a.cc:118:20: error: invalid types 'int[int]' for array subscript
  118 |             cmax(dp[i][j], v);
      |                    ^
a.cc:20:28: note: in definition of macro 'cmax'
   20 | #define cmax(x, y) x = max(x, y)
      |                            ^
a.cc: In function 'int knapsack(std::vector<Item>&, int)':
a.cc:126:27: error: 'll' is not a type
  126 |     auto get = [&](int i, ll j) -> ll {
      |                           ^~
a.cc:126:36: error: 'll' does not name a type
  126 |     auto get = [&](int i, ll j) -> ll {
      |                                    ^~
a.cc:138:15: error: expected ';' before 'from'
  138 |             ll from = get(p(i), j);
      |               ^~~~~
      |               ;
a.cc:139:17: error: 'from' was not declared in this scope; did you mean 'fromfp'?
  139 |             if (from == -1) continue;
      |                 ^~~~
      |                 fromfp
a.cc:140:33: error: 'struct Item' has no member named 'w'
  140 |             add(i, ll(j) + item.w, from + item.v);
      |                                 ^
a.cc:140:36: error: 'from' was not declared in this scope; did you mean 'fromfp'?
  140 |             add(i, ll(j) + item.w, from + item.v);
      |                                    ^~~~
      |                                    fromfp
a.cc:140:48: error: 'struct Item' has no member named 'v'
  140 |             add(i, ll(j) + item.w, from + item.v);
      |                                                ^
a.cc:148:20: error: invalid types 'int[int]' for array subscript
  148 |             cmax(dp[i][j], dp[i][j - 1]);
      |                    ^
a.cc:20:20: note: in definition of macro 'cmax'
   20 | #define cmax(x, y) x = max(x, y)
      |                    ^
a.cc:148:20: error: invalid types 'int[int]' for array subscript
  148 |             cmax(dp[i][j], dp[i][j - 1]);
      |                    ^
a.cc:20:28: note: in definition of macro 'cmax'
   20 | #define cmax(x, y) x = max(x, y)
      |                            ^
a.cc:148:30: error: invalid types 'int[int]' for array subscript
  148 |             cmax(dp[i][j], dp[i][j - 1]);
      |                              ^
a.cc:20:31: note: in definition of macro 'cmax'
   20 | #define cmax(x, y) x = max(x, y)
      |                               ^
a.cc: In function 'int main()':
a.cc:164:25: