Language: C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=300010,M=100010;
int n,m,q,ans=0;
int v[N],w[N],d[N],f[1030][M];
inline void dfs1(int x)
{
    d[x]=d[x/2]+1;
    for(int k=0;k<=1e5;k++)
        f[x][k]=f[x/2][k];
    for(int k=w[x];k<=1e5;k++)
        f[x][k]=max(f[x][k],f[x/2][k-w[x]]+v[x]);
    if(d[x]==m) return;
    dfs1(x<<1);
    dfs1(x<<1|1);
    return;
}
inline void dfs2(int x,int y,long long z)
{
    if(d[x]==m)
    {
        ans=max(ans,z+f[x][y]);
        return;
    }
    if(y>=w[x]) dfs2(x/2,y-w[x],z+v[x]);
    dfs2(x/2,y,z);
    return;
}
int main()
{
    scanf("%d",&n);
    m=log2(n)/2;
    if(m==0) m=1;
    for(int k=1;k<=n;k++)
        scanf("%d%d",&v[k],&w[k]);
    dfs1(1);
    scanf("%d",&q);
    while(q--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        ans=0;
        if(d[x]) printf("%d\n",f[x][y]);
        else dfs2(x,y,0ll),printf("%d\n",ans);
    }
    return 0;
}


Compiler output:
a.cc: In function 'void dfs2(int, int, long long int)':
a.cc:26:16: error: no matching function for call to 'max(int&, long long int)'
   26 |         ans=max(ans,z+f[x][y]);
      |             ~~~^~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:26:16: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   26 |         ans=max(ans,z+f[x][y]);
      |             ~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:26:16: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   26 |         ans=max(ans,z+f[x][y]);
      |             ~~~^~~~~~~~~~~~~~~