Language: C++

#include<bits/stdc++.h>

#define y1 dmytxdy
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef pair<ld,int> pdi;

template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}

int readint(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

ll n,X,m,ans,tot;
ll a[55],v[55],nxt[55],h[55],b[55],d[55][125005];
pdi p[55];

void addedge(int x,int y){v[++tot]=y; nxt[tot]=h[x]; h[x]=tot;}

void dfs(int u){
	b[u]=1;
	for(int p=h[u];p;p=nxt[p]) dfs(v[p]),a[u]+=a[v[p]],b[u]+=b[v[p]];
}

void calcd(){
	memset(d,0x3f,sizeof(d));
	d[0][0]=0;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=n*n*n;j++)
			for(int l=0;l<=min(n,m);l++)
				if(j>=l*b[i])
					chkmin(d[i][j],d[i-1][j-l*b[i]]+l*a[i]);
}

int solve(int pl,ll x,ll k,bool tp){
	ll ret=0;
	for(int i=0;i<=n*n*n;i++) if(d[n][i]<=k) chkmax(ret,i+(tp?min((k-d[n][i])/a[pl],max(0,m-n)):(k-d[n][i])/a[pl])*b[pl]);
	return ret;
}

int main(){
	n=readint(); X=readint(); m=readint();
	a[1]=readint();
	for(int i=2;i<=n;i++) a[i]=readint(),addedge(readint(),i);
	dfs(1);
	for(int i=1;i<=n;i++) p[i]=mp((ld)b[i]/a[i],i);
	sort(p+1,p+n+1);
	int pl=0;
	calcd();
	for(int i=1;i<=n;i++) if(p[i].se==1) pl=i;
	for(int i=pl;i<=n;i++){
		ll tmp=X,res=0,u=p[i].se;
		for(int j=i+1;j<=n;j++) tmp-=max(m-n,0ll)*a[p[j].se],res+=max(m-n,0ll)*b[p[j].se];
		if(tmp<0) continue;
		chkmax(ans,res+solve(u,min(n,m),tmp,i!=pl));
	}
	printf("%lld\n",ans);
	return 0;
}

Compiler output:
a.cc: In function 'int solve(int, ll, ll, bool)':
a.cc:50:92: error: no matching function for call to 'max(int, ll)'
   50 |         for(int i=0;i<=n*n*n;i++) if(d[n][i]<=k) chkmax(ret,i+(tp?min((k-d[n][i])/a[pl],max(0,m-n)):(k-d[n][i])/a[pl])*b[pl]);
      |                                                                                         ~~~^~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 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:50:92: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll' {aka 'long long int'})
   50 |         for(int i=0;i<=n*n*n;i++) if(d[n][i]<=k) chkmax(ret,i+(tp?min((k-d[n][i])/a[pl],max(0,m-n)):(k-d[n][i])/a[pl])*b[pl]);
      |                                                                                         ~~~^~~~~~~
/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:
/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:50:92: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   50 |         for(int i=0;i<=n*n*n;i++) if(d[n][i]<=k) chkmax(ret,i+(tp?min((k-d[n][i])/a[pl],max(0,m-n)):(k-d[n][i])/a[pl])*b[pl]);
      |                                                                                         ~~~^~~~~~~