Language: C++

#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <stdlib.h>
#include <tuple>
#include <iomanip>
#include <iterator>
#include <stdio.h>
#include <fstream>
#include <time.h>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;

struct UnionFind {
    vector<int> parent;

    UnionFind(int N) : parent(N) {
        for (int i = 0; i < N; i++) {
            parent[i] = -1;
        }
    }

    int root(int i) {
        if (parent[i] < 0) {
            return i;
        }
        return (parent[i] = root(parent[i]));
    }

    bool unite(int from, int to) {
        int rx = root(from);
        int ry = root(to);
        if (rx != ry) {
            parent[ry] += parent[rx];
            parent[rx] = ry;

            return true;
        }
        else {
            return false;
        }
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int treeSize(int x) {
        

        return -parent[root(x)];
    }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
    if (pow == 1) {
        return base;
    }
    else if (pow == 0) {
        return 1;
    }

    if (pow % 2 == 0) {
        auto temp = modpow(base, pow / 2, mod);
        return (temp * temp) % mod;
    }
    else {
        return base * modpow(base, pow - 1, mod) % mod;
    }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
    auto fermatDiv = modpow(Y, mod - 2, mod);

    return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
    long long answer = 1;
    for (long long i = 0; i < right; i++) {
        answer = (answer * (left - i)) % mod;
        answer = moddiv(answer, (i + 1), mod);
    }

    return answer;
}

bool IsPrime(long long N) {
    if (N == 1) {
        return false;
    }
    for (long long i = 2; i * i <= N; i++) {
        if (N % i == 0) {
            return false;
        }
    }

    return true;
}



vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({ a, ex });
    }

    // 最後に残った数について
    if (N != 1) res.push_back({ N, 1 });
    return res;
}

long long euclid(long long a, long long b) {
    if (a > b) {
        long long temp = b;
        b = a;
        a = temp;
    }
    if (b % a == 0) {
        return a;
    }
    else {
        return euclid(a, b - a);
    }
}

int dp[131073][100000];
bool isAlreadyCalc[131073];
long long knapsack(int v[], int w[], long long maxW, int n, int indexList[]) {
    
    long long startIndex = 0;
    for (long long i = 0; i < n; i++) {
        if (!isAlreadyCalc[indexList[i]]) {
            startIndex = i;
        }
    }

    int answer[100000];
    for (long long i = startIndex; i < n; i++) {
        int index = indexList[i];
        if (index < 131073) {
            isAlreadyCalc[index] = true;
            for (long long j = 0; j <= maxW; j++) {
                if (j >= w[i]) {
                    answer[j] = dp[index][j] = max(dp[index / 2][j], dp[index / 2][j - w[index / 2]] + v[index / 2]);
                }
                else {
                    answer[j] = dp[index][j] = dp[index / 2][j];
                }
            }
        }
        else {
            for (long long j = 0; j <= maxW; j++) {
                if (j >= w[i]) {
                    answer[j] = max(dp[index / 2][j], dp[index / 2][j - w[index / 2]] + v[index / 2]);
                }
                else {
                    answer[j] = dp[index / 2][j];
                }
            }
        }
    }

    return answer[maxW];
}

int main() {
    /*long long N, K;
    cin >> N >> K;

    int lamps[200001];
    for (long long i = 0; i < N; i++) {
        long long temp;
        cin >> temp;
        lamps[i] = temp;
    }

    
    for (long long i = 0; i < K; i++) {
        int newLamps[200001];
        for (long long j = 0; j < N; j++) {
            long long min = (j - lamps[j]) > 0 ? j - lamps[j] : 0;
            long long max = (j + lamps[j]) < N ? j + lamps[j] : N - 1;

            for (long long k = min; k <= max; k++) {
                newLamps[k]++;
            }
            //cout << j << ":" << min << ":" << max << endl;
        }
        for (int k = 0; k < N; k++) {
            lamps[k] = newLamps[k];
        }
    }

    for (auto p : lamps) {
        cout << p << " ";
    }
    cout << endl;*/
    for (int i = 0; i < 131073; i++) {
        isAlreadyCalc[i] = false;
    }
    
    int treeV[262145];
    int treeW[262145];
    long long N;
    cin >> N;
    for (long long i = 1; i <= N; i++) {
        long long V, W;
        cin >> V >> W;
        treeV[i] = V;
        treeW[i] = W;
    }

    long long Q;
    cin >> Q;

    for (long long i = 0; i < Q; i++) {
        long long v, L;
        cin >> v >> L;
        int vs[100];
        int ws[100];
        int indexList[262145];
        int n = 0;
        while (v != 0) {
            vs[n] = treeV[v];
            ws[n] = treeW[v];
            indexList[n] = v;
            v /= 2;
            n++;
        }
        for (int j = 0; j < n / 2; j++) {
            int temp = indexList[j];
            indexList[j] = indexList[n - 1 - j];
            indexList[n - 1 - j] = temp;
            
        }
        cout << knapsack(vs, ws, L, n, indexList) << endl;
    }
    return 0;
}

Compiler output:
/tmp/ccXJdPiM.o: in function `knapsack(int*, int*, long long, int, int*)':
a.cc:(.text+0x3aa): relocation truncated to fit: R_X86_64_PC32 against symbol `isAlreadyCalc' defined in .bss section in /tmp/ccXJdPiM.o
a.cc:(.text+0x411): relocation truncated to fit: R_X86_64_PC32 against symbol `isAlreadyCalc' defined in .bss section in /tmp/ccXJdPiM.o
/tmp/ccXJdPiM.o: in function `main':
a.cc:(.text+0x792): relocation truncated to fit: R_X86_64_PC32 against symbol `isAlreadyCalc' defined in .bss section in /tmp/ccXJdPiM.o
collect2: error: ld returned 1 exit status