Language: C++

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}







inline int grayCode(int i){
    return i ^ (i >> 1);
}

inline int invGrayCode(int j){
    for(int k(0); k < 5; ++k){// 2pow(5) = 32
        j = j ^ (j >> (1<<k));
    }
    return j;
}

std::unordered_map<int, int> twoPow{{1, 0}, {2, 1}, {4, 2}, {8, 3}, {16, 4}, {32, 5}, {64, 6}, {128, 7}, {256, 8}, {512, 9}, {1024, 10}, {2048, 11}, {4096, 12}, {8192, 13}, {16384, 14}, {32768, 15}, {65536, 16}, {131072, 17}, {262144, 18}, {524288, 19}};

int main(){
    int N; std::cin >> N;
    std::vector<int> V(N+1), W(N+1);
    for(int i(1); i <= N; ++i) std::cin >> V[i] >> W[i];
    int n(0);
    for(; n <= 18; ++n){
        if((1 << n) > N) break;
    }
    int Q, mxL; std::cin >> Q;
    std::vector<int> v(Q), L(Q);
    for(int i(0); i < Q; ++i){
        std::cin >> v[i] >> L[i];
        chmax(mxL, L[i]);
    }
    int n2(n / 2);
    std::vector<std::vector<long long>> dp((1 << n2), std::vector<long long>(mxL+1, 0));
    for(int i(1), i_len(1 << n2); i < i_len; ++i){
        for(int j(0); j <= mxL; ++j){
            dp[i][j] = dp[i/2][j];
            if(j >= W[i]) chmax(dp[i][j], dp[i/2][j-W[i]] + V[i]);
        }
    }
    for(int qq(0); qq < Q; ++qq){
        if(v[qq] < (1 << n2)){
            std::cout << dp[v[qq]][L] << "\n";
            continue;
        }
        std::vector<int> ww, vv;
        int vx(v[qq]);
        while(vx >= (1 << n2)){
            ww.push_back(W[vx]);
            vv.push_back(V[vx]);
            vx >>= 1;
        }
        int n1(ww.size());
        long long sw(0), sv(0);
        int prvj(0);
        // i = 0 の時の処理
        long long ans(0);
        if(sw <= L[qq]){
            chmax(ans, dp[vx][L[qq]-sw] + sv);
        }
        for(int i(1), i_len(1 << n1); i < i_len; ++i){
            int j(grayCode(i));
            int changebit(j ^ prvj);
            int changeNum(twoPow[changebit]);
            if(j & changebit){// changeBit が立った
                sw += ww[changeNum];
                sv += vv[changeNum];
            }else{// changeBit がなくなった
                sw -= ww[changeNum];
                sv -= vv[changeNum];
            }
            //何か処理
            if(sw <= L){
                chmax(ans, dp[vx][L-sw] + sv);
            }
            prvj = j;
        }
        std::cout << ans << "\n";
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:97:35: error: no match for 'operator[]' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::vector<long long int> >, std::vector<long long int> >::value_type' {aka 'std::vector<long long int>'} and 'std::vector<int>')
   97 |             std::cout << dp[v[qq]][L] << "\n";
      |                                   ^
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from a.cc:16:
/usr/include/c++/14/bits/stl_vector.h:1128:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = long long int; _Alloc = std::allocator<long long int>; reference = long long int&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1147:7: note: candidate: 'std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](size_type) const [with _Tp = long long int; _Alloc = std::allocator<long long int>; const_reference = const long long int&; size_type = long unsigned int]'
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1147:28: note:   no known conversion for argument 1 from 'std::vector<int>' to 'std::vector<long long int>::size_type' {aka 'long unsigned int'}
 1147 |       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:127:19: error: no match for 'operator<=' (operand types are 'long long int' and 'std::vector<int>')
  127 |             if(sw <= L){
      |                ~~ ^~ ~
      |                |     |
      |                |     std::vector<int>
      |                long long int
In file included from /usr/include/c++/14/regex:68,
                 from a.cc:24:
/usr/include/c++/14/bits/regex.h:1154:5: note: candidate: 'template<class _BiIter> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, const sub_match<_BiIter>&)'
 1154 |     operator<=(const sub_match<_BiIter>& __lhs, const sub_match<_BiIter>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1154:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/regex.h:1260:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<=(__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&, const sub_match<_BiIter>&)'
 1260 |     operator<=(const __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1260:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/regex.h:1353:5: note: candidate: 'template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, __sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)'
 1353 |     operator<=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1353:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/regex.h:1427:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const typename std::iterator_traits<_Iter>::value_type*, const sub_match<_BiIter>&)'
 1427 |     operator<=(typename iterator_traits<_Bi_iter>::value_type const* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1427:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   'std::vector<int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/regex.h:1521:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type*)'
 1521 |     operator<=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1521:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/regex.h:1599:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const typename std::iterator_traits<_Iter>::value_type&, const sub_match<_BiIter>&)'
 1599 |     operator<=(typename iterator_traits<_Bi_iter>::value_type const& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1599:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   'std::vector<int>' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/regex.h:1699:5: note: candidate: 'template<class _Bi_iter> bool std::__cxx11::operator<=(const sub_match<_BiIter>&, const typename std::iterator_traits<_Iter>::value_type&)'
 1699 |     operator<=(const sub_match<_Bi_iter>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/regex.h:1699:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::__cxx11::sub_match<_BiIter>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_pair.h:1064:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<=(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1064 |     operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1064:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::pair<_T1, _T2>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:469:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<=(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  469 |     operator<=(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:469:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/stl_iterator.h:513:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<=(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  513 |     operator<=(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:513:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::reverse_iterator<_Iterator>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/stl_iterator.h:1704:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1704 |     operator<=(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1704:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
/usr/include/c++/14/bits/stl_iterator.h:1767:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<=(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1767 |     operator<=(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1767:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::move_iterator<_IteratorL>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
In file included from a.cc:3:
/usr/include/c++/14/array:356:5: note: candidate: 'template<class _Tp, long unsigned int _Nm> bool std::operator<=(const array<_Tp, _Nm>&, const array<_Tp, _Nm>&)'
  356 |     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
      |     ^~~~~~~~
/usr/include/c++/14/array:356:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'const std::array<_Tp, _Nm>' and 'long long int'
  127 |             if(sw <= L){
      |                      ^
In file included from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from a.cc:4:
/usr/include/c++/14/string_view:717:5: note: candidate: 'template<class _CharT, class _Traits> constexpr bool std::operator<=(basic_string_view<_CharT, _Traits>, basic_string_view<_CharT, _Traits>)'
  717 |     operator<=(basic_string_view<_CharT, _Traits> __x,
      |     ^~~~~~~~
/usr/include/c++/14/string_view:717:5: note:   template argument deduction/substitution failed:
a.cc:127:22: note:   mismatched types 'st