Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring> //memset(dp,0,sizeof(dp))
#include <functional>
#include <cctype>
#include <locale>
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ppb pop_back()
#define ALL(a) (a).begin(),(a).end()
#define int long long
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
//s[i]=tolower(s[i]); islower(s[i]); cout << tolower(s[i])はバグ

void dfs(const long n, const long W, const long value, const long weight, long &opt, const long index, const vector<long> &rsumw, const vector<long> &rminw, const vector<pair<long, long>> &v) {
  if (index == n) {
    opt = max(opt, value);
    return;
  }
  if (rsumw.at(index) <= W - weight) {
    long tvalue = value;
    for (long i = index; i < n; i++) {
      tvalue += v.at(i).first;
    }
    opt = max(opt, tvalue);
    return;
  }
  if (rminw.at(index) > W - weight) return;
  if (weight + v.at(index).second <= W) {
    opt = max(opt, value + v.at(index).first);
    long tweight = weight + v.at(index).second, tvalue = value + v.at(index).first;
    for (long i = index + 1; i < n; i++) {
      if (tweight + v.at(i).second <= W) {
        tweight += v.at(i).second, tvalue += v.at(i).first;
      } else {
        tvalue += v.at(i).first * (W - tweight) / v.at(i).second;
      break;
      }
    }
    if (opt < tvalue) {
      dfs(n, W, value + v.at(index).first, weight + v.at(index).second, opt, index + 1, rsumw, rminw, v);
    }
  }
  long tweight = weight, tvalue = value;
  for (long i = index + 1; i < n; i++) {
    if (tweight + v.at(i).second <= W) {
      tweight += v.at(i).second, tvalue += v.at(i).first;
    } else {
      tvalue += v.at(i).first * (W - tweight) / v.at(i).second;
    break;
    }
  }
  if (opt < tvalue) {
    dfs(n, W, value, weight, opt, index + 1, rsumw, rminw, v);
  }
}
long solve(const long n, const long W, vector<pair<long, long>> &v) {
  sort(v.begin(), v.end(), [&](pair<long, long> &a, pair<long, long> &b) {
    return (__int128)a.first * b.second > (__int128) a.second * b.first;
  });
  vector<long> rsumw(n + 1, 0);
  vector<long> rminw(n + 1, LONG_MAX);
  for (long i = n - 1; i >= 0; --i) {
    rsumw.at(i) = rsumw.at(i + 1) + v.at(i).second, rminw.at(i) = min(rminw.at(i + 1), v.at(i).second);
  }
  long opt = 0;
  dfs(n, W, 0, 0, opt, 0, rsumw, rminw, v);
  return opt;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int nn; cin >> nn;
    vector<int> v1(nn+1),ww(nn+1);
    rep(i,nn) cin >> v1[i+1] >> ww[i+1];
    int q; cin >> q;
    rep(query,q){
        int ve,W; cin >> ve >> W;
        int N=log2(ve)+1;
        vector<pair<long, long>> V(N);
        rep(i,N){
            V.at(i)={v1[ve],ww[ve]};
            ve/=2;
        }
        cout << solve(N, W, V) << "\n";
    }
}


Compiler output:
a.cc: In function 'long int solve(long int, long int, std::vector<std::pair<long int, long int> >&)':
a.cc:78:29: error: 'LONG_MAX' was not declared in this scope
   78 |   vector<long> rminw(n + 1, LONG_MAX);
      |                             ^~~~~~~~
a.cc:15:1: note: 'LONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   14 | #include <cctype>
  +++ |+#include <climits>
   15 | #include <locale>