Language: C++

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert

using namespace std;

//-------------------------------------------------
//--Fast I/O (Chotto Hayai Nyuushutsuryoku)
//-------------------------------------------------
class FastIO
{
private:
    static const int IO_SIZE = 2e7;
    char rbuf[IO_SIZE], wbuf[IO_SIZE], *rp, *wp;
    char temp[19];
    inline bool minus(char c){return c=='-';}
    inline int cnum(char c){return c-'0';}
    inline int numc(int c){return c+'0';}
public:
    FastIO(){
        fread(rbuf,1,IO_SIZE,stdin);
        rp=rbuf; wp=wbuf;
    }
    ~FastIO(){fwrite(wbuf,1,wp-wbuf,stdout);}
    template<typename T=int>
    inline T readInt(){
        bool s; T ret=0;
        while(!isdigit(*rp) && !minus(*rp)) rp++;
        if (s=minus(*rp)) rp++;
        while(isdigit(*rp)) ret=ret*10+cnum(*rp++);
        return s?-ret:ret;
    }
    inline void readStr(string &s){
        string t="";
        while(isspace(*rp)) rp++;
        while(!isspace(*rp)) t+=*rp++;
        s.swap(t);
    }
    inline char getc(){
        while(isspace(*rp)) rp++;
        return *rp++;
    }
    inline void putc(int c){*wp++ = c;}
    inline void puts(const char *s){while(*s!='\0') putc(*s++);}
    inline void writeStr(const string &s){
        for(auto c:s) putc(c);
    }
    template<typename T>
    inline void writeInt(T x){
        if (x==0){putc('0'); return;}
        if (x<0) putc('-'), x=-x;
        char *tp=temp; T y=x;
        do{
            y=x/10;
            *tp++ = numc(x-y*10);
        }while(x=y);
        while(tp!=temp) putc(*(--tp));
    }
    FastIO& operator<<(const char a){putc(a);return *this;}
    FastIO& operator<<(const char *s){puts(s);return *this;}
    FastIO& operator<<(const string &a){writeStr(a);return *this;}
    FastIO& operator<<(const int a){writeInt(a);return *this;}
    FastIO& operator<<(const long long a){writeInt(a);return *this;}
    FastIO& operator>>(char &a){a=getc();return *this;}
    FastIO& operator>>(string &a){readStr(a);return *this;}
    FastIO& operator>>(int &a){a=readInt<int>();return *this;}
    FastIO& operator>>(long long &a){a=readInt<long long>();return *this;}
};
#define br '\n'
FastIO io;

const int INF = INT_MAX;

//-------------------------------------------------

int main(void)
{
    int N; io>>N;
    vi V(N),W(N);
    rep(i,N) io>>V[i]>>W[i];
    int Q; io>>Q;
    while(Q--){
        int t,L; io>>t>>L;
        vi v,w;
        while(t){
            v.pb(V[t-1]);
            w.pb(W[t-1]);
            t/=2;
        }
        int K = v.size();
        int K2 = K/2;
        vector<pi> ps;
        rep(i,1<<K2){
            int sw=0,sv=0;
            rep(j,K2){
                if (i>>j&1){
                    sw+=w[j];
                    sv+=v[j];
                }
            }
            ps.pb({sw,sv});
        }
        sort(all(ps));
        int m=1;
        FOR(i,1,(1<<K2)-1){
            if (ps[m-1].se < ps[i].se){
                ps[m++] = ps[i];
            }
        }
        int ans=0;
        rep(i,1<<(K-K2)){
            int sw=0,sv=0;
            rep(j,K-K2){
                if (i>>j&1){
                    sw+=w[K2+j];
                    sv+=v[K2+j];
                }
            }
            if (sw<=L){
                int tv = (lower_bound(ps.begin(),ps.begin()+m,mp(L-sw,INF))-1)->se;
                int tv = 0;
                chmax(ans,sv+tv);
            }
        }
        io<<ans<<"\n";
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:143:21: error: redeclaration of 'int tv'
  143 |                 int tv = 0;
      |                     ^~
a.cc:142:21: note: 'int tv' previously declared here
  142 |                 int tv = (lower_bound(ps.begin(),ps.begin()+m,mp(L-sw,INF))-1)->se;
      |                     ^~