Language: C++

int main() {
    int n, q;
    cin >> n;
    vector<tuple<int, int>> vw(n);
    REP(i, n) {
        int a, b;
        cin >> a >> b;
        vw[i] = make_tuple(a, b);
    }
    cin >> q;
    vector<tuple<int, int>> vl(q);
    REP(i, q) {
        int a, b;
        cin >> a >> b;
        vl[i] = make_tuple(a, b);
    }

    int former = min(512, n);
    vector<vector<int>> former_dp(former, vector<int>(100001, -1));
    vector<set<int>> former_set(former);

    former_dp[0][0] = 0;
    former_dp[0][get<1>(vw[0])] = get<0>(vw[0]);
    former_set[0].insert(0);
    former_set[0].insert(get<1>(vw[0]));

    FOR(i, 1, former) {
        int v, w;
        tie(v, w) = vw[i];
        auto& anc_dp = former_dp[(i - 1) / 2];
        auto& now_dp = former_dp[i];
        auto& anc_set = former_set[(i - 1) / 2];
        auto& now_set = former_set[i];
        for (auto j : anc_set) {
            now_dp[j] = anc_dp[j];
            now_set.insert(j);
        }
        for (auto j : anc_set) {
            if (w + j <= 100000) {
                now_dp[w + j] = max(now_dp[w + j], anc_dp[j] + v);
                now_set.insert(w + j);
            }
        }
    }

    int m;
    REP(i, former) {
        m = 0;
        auto& now_dp = former_dp[i];
        FOR(j, 1, 100001) {
            m = max(m, now_dp[j]);
            now_dp[j] = m;
        }
    }

    for (auto temp : vl) {
        int v, l;
        tie(v, l) = temp;
        if (v <= former) {
            cout << former_dp[v - 1][l] << endl;
            continue;
        }
        vector<tuple<int, int>> later;
        int vv = v;
        while (vv > former) {
            later.push_back(vw[vv - 1]);
            vv >>= 1;
        }
        auto& anc_dp = former_dp[vv - 1];
        int m = later.size();
        int ans = 0;
        int j, k, v_sum, w_sum, ii;
        REP(i, pow(2, m)) {
            v_sum, w_sum = 0, 0;
            REP(p, m) {
                tie(j, k) = later[p];
                if (i >> p & 1){
                    v_sum += j;
                    w_sum += k;
                }
            }
            if (w_sum > l) { continue; }
            ans = max(ans, v_sum + anc_dp[l - w_sum]);
        }
        cout << ans << endl;
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:3:5: error: 'cin' was not declared in this scope
    3 |     cin >> n;
      |     ^~~
a.cc:4:5: error: 'vector' was not declared in this scope
    4 |     vector<tuple<int, int>> vw(n);
      |     ^~~~~~
a.cc:4:12: error: 'tuple' was not declared in this scope
    4 |     vector<tuple<int, int>> vw(n);
      |            ^~~~~
a.cc:4:18: error: expected primary-expression before 'int'
    4 |     vector<tuple<int, int>> vw(n);
      |                  ^~~
a.cc:5:9: error: 'i' was not declared in this scope
    5 |     REP(i, n) {
      |         ^
a.cc:5:5: error: 'REP' was not declared in this scope
    5 |     REP(i, n) {
      |     ^~~
a.cc:11:18: error: expected primary-expression before 'int'
   11 |     vector<tuple<int, int>> vl(q);
      |                  ^~~
a.cc:18:18: error: 'min' was not declared in this scope; did you mean 'main'?
   18 |     int former = min(512, n);
      |                  ^~~
      |                  main
a.cc:19:19: error: expected primary-expression before 'int'
   19 |     vector<vector<int>> former_dp(former, vector<int>(100001, -1));
      |                   ^~~
a.cc:20:12: error: 'set' was not declared in this scope
   20 |     vector<set<int>> former_set(former);
      |            ^~~
a.cc:20:16: error: expected primary-expression before 'int'
   20 |     vector<set<int>> former_set(former);
      |                ^~~
a.cc:22:5: error: 'former_dp' was not declared in this scope; did you mean 'former'?
   22 |     former_dp[0][0] = 0;
      |     ^~~~~~~~~
      |     former
a.cc:23:18: error: 'get' was not declared in this scope
   23 |     former_dp[0][get<1>(vw[0])] = get<0>(vw[0]);
      |                  ^~~
a.cc:23:25: error: 'vw' was not declared in this scope
   23 |     former_dp[0][get<1>(vw[0])] = get<0>(vw[0]);
      |                         ^~
a.cc:24:5: error: 'former_set' was not declared in this scope; did you mean 'former'?
   24 |     former_set[0].insert(0);
      |     ^~~~~~~~~~
      |     former
a.cc:27:5: error: 'FOR' was not declared in this scope
   27 |     FOR(i, 1, former) {
      |     ^~~
a.cc:56:22: error: 'vl' was not declared in this scope
   56 |     for (auto temp : vl) {
      |                      ^~
a.cc:58:9: error: 'tie' was not declared in this scope
   58 |         tie(v, l) = temp;
      |         ^~~
a.cc:60:13: error: 'cout' was not declared in this scope
   60 |             cout << former_dp[v - 1][l] << endl;
      |             ^~~~
a.cc:60:44: error: 'endl' was not declared in this scope
   60 |             cout << former_dp[v - 1][l] << endl;
      |                                            ^~~~
a.cc:63:22: error: expected primary-expression before 'int'
   63 |         vector<tuple<int, int>> later;
      |                      ^~~
a.cc:66:13: error: 'later' was not declared in this scope
   66 |             later.push_back(vw[vv - 1]);
      |             ^~~~~
a.cc:70:17: error: 'later' was not declared in this scope
   70 |         int m = later.size();
      |                 ^~~~~
a.cc:73:16: error: 'pow' was not declared in this scope
   73 |         REP(i, pow(2, m)) {
      |                ^~~
a.cc:85:9: error: 'cout' was not declared in this scope
   85 |         cout << ans << endl;
      |         ^~~~
a.cc:85:24: error: 'endl' was not declared in this scope
   85 |         cout << ans << endl;
      |                        ^~~~