Language: C++

#include<bits/stdc++.h>
#define ll long long
#define dbg1(x) cerr<<#x<<"="<<(x)<<" "
#define dbg2(x) cerr<<#x<<"="<<(x)<<"\n"
#define dbg3(x) cerr<<#x<<"\n"
using namespace std;
#define reg register
#define ll int64
inline ll read()
{
    ll 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<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
const int MN=55;
ll N,X,D,v[MN],p[MN],z[MN],id[MN],nm[MN];
std::vector<int>s[MN];
void dfs(int x)
{
	reg int i;p[x]=1;
	for(i=s[x].size()-1;~i;--i)
		dfs(s[x][i]),v[x]+=v[s[x][i]],p[x]+=p[s[x][i]];
}
ll f[MN*MN*MN];
bool cmp(const int&x,const int&y){return 1ll*p[x]*v[y]>1ll*p[y]*v[x];}
int main()
{
	N=read(),X=read(),D=read();
	reg int i,j,k;int lim=0;
	for(v[1]=read(),i=2;i<=N;++i)
		v[i]=read(),s[read()].push_back(i),z[i]=min(D,N);
	dfs(1);z[1]=N;
	for(i=1;i<=N;++i)lim+=z[i]*p[i];
	for(i=1;i<=lim+1;++i)f[i]=1e18;
	for(i=1;i<=N;++i)for(j=lim;j;--j)
	{
		int r=min(z[i],j/p[i]);
		for(k=1;k<=r;++k)
			f[j]=min(f[j],f[j-p[i]*k]+v[i]*k);
		f[j]=min(f[j],f[j+1]);
	}
	for(z[1]=1e14,i=2;i<=N;++i)z[i]=D-z[i];
	for(i=1;i<=N;++i)id[i]=i;
	std::sort(id+1,id+N+1,cmp);
	int ans=0;
	for(j=X,i=1;i<=N;++i)
	{
		nm[i]=min(z[id[i]],j/v[id[i]]);
		ans+=p[id[i]]*nm[i];
		j-=v[id[i]]*nm[i];
		if(!nm[i])break;
	}
	int tmp=ans;
	for(j=X-j,k=i,i=1;i<=lim;++i)
	{
		if(f[i]>X)break;
		int most=X-f[i];
		while(j>most)
		{
			while(!nm[k])--k;
			int n=(j-most+v[id[k]]-1)/v[id[k]];
			n=min((ll)n,nm[k]);nm[k]-=n;
			j-=v[id[k]]*n;tmp-=p[id[k]]*n;
		}
		ans=max(ans,tmp+i);
	}
	printf("%lld\n",ans);
	return 0;
}

Compiler output:
a.cc:8:9: warning: "ll" redefined
    8 | #define ll int64
      |         ^~
a.cc:2:9: note: this is the location of the previous definition
    2 | #define ll long long
      |         ^~
a.cc:8:12: error: 'int64' does not name a type; did you mean 'int64_t'?
    8 | #define ll int64
      |            ^~~~~
a.cc:9:8: note: in expansion of macro 'll'
    9 | inline ll read()
      |        ^~
a.cc:8:12: error: 'int64' does not name a type; did you mean 'int64_t'?
    8 | #define ll int64
      |            ^~~~~
a.cc:17:1: note: in expansion of macro 'll'
   17 | ll N,X,D,v[MN],p[MN],z[MN],id[MN],nm[MN];
      | ^~
a.cc: In function 'void dfs(int)':
a.cc:21:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         reg int i;p[x]=1;
      |                 ^
a.cc:21:19: error: 'p' was not declared in this scope
   21 |         reg int i;p[x]=1;
      |                   ^
a.cc:23:30: error: 'v' was not declared in this scope
   23 |                 dfs(s[x][i]),v[x]+=v[s[x][i]],p[x]+=p[s[x][i]];
      |                              ^
a.cc: At global scope:
a.cc:8:12: error: 'int64' does not name a type; did you mean 'int64_t'?
    8 | #define ll int64
      |            ^~~~~
a.cc:25:1: note: in expansion of macro 'll'
   25 | ll f[MN*MN*MN];
      | ^~
a.cc: In function 'bool cmp(const int&, const int&)':
a.cc:26:46: error: 'p' was not declared in this scope
   26 | bool cmp(const int&x,const int&y){return 1ll*p[x]*v[y]>1ll*p[y]*v[x];}
      |                                              ^
a.cc:26:51: error: 'v' was not declared in this scope
   26 | bool cmp(const int&x,const int&y){return 1ll*p[x]*v[y]>1ll*p[y]*v[x];}
      |                                                   ^
a.cc: In function 'int main()':
a.cc:29:9: error: 'N' was not declared in this scope
   29 |         N=read(),X=read(),D=read();
      |         ^
a.cc:29:15: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   29 |         N=read(),X=read(),D=read();
      |           ~~~~^~
In file included from /usr/include/x86_64-linux-gnu/bits/sigstksz.h:24,
                 from /usr/include/signal.h:328,
                 from /usr/include/c++/14/csignal:42,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:116,
                 from a.cc:1:
/usr/include/unistd.h:371:16: note: declared here
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc:29:18: error: 'X' was not declared in this scope
   29 |         N=read(),X=read(),D=read();
      |                  ^
a.cc:29:24: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   29 |         N=read(),X=read(),D=read();
      |                    ~~~~^~
/usr/include/unistd.h:371:16: note: declared here
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc:29:27: error: 'D' was not declared in this scope
   29 |         N=read(),X=read(),D=read();
      |                           ^
a.cc:29:33: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   29 |         N=read(),X=read(),D=read();
      |                             ~~~~^~
/usr/include/unistd.h:371:16: note: declared here
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc:30:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   30 |         reg int i,j,k;int lim=0;
      |                 ^
a.cc:30:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   30 |         reg int i,j,k;int lim=0;
      |                   ^
a.cc:30:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   30 |         reg int i,j,k;int lim=0;
      |                     ^
a.cc:31:13: error: 'v' was not declared in this scope
   31 |         for(v[1]=read(),i=2;i<=N;++i)
      |             ^
a.cc:31:22: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   31 |         for(v[1]=read(),i=2;i<=N;++i)
      |                  ~~~~^~
/usr/include/unistd.h:371:16: note: declared here
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc:32:26: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   32 |                 v[i]=read(),s[read()].push_back(i),z[i]=min(D,N);
      |                      ~~~~^~
/usr/include/unistd.h:371:16: note: declared here
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc:32:35: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   32 |                 v[i]=read(),s[read()].push_back(i),z[i]=min(D,N);
      |                               ~~~~^~
/usr/include/unistd.h:371:16: note: declared here
  371 | extern ssize_t read (int __fd, void *__buf, size_t __nbytes) __wur
      |                ^~~~
a.cc:32:52: error: 'z' was not declared in this scope
   32 |                 v[i]=read(),s[read()].push_back(i),z[i]=min(D,N);
      |                                                    ^
a.cc:33:16: error: 'z' was not declared in this scope
   33 |         dfs(1);z[1]=N;
      |                ^
a.cc:34:36: error: 'p' was not declared in this scope
   34 |         for(i=1;i<=N;++i)lim+=z[i]*p[i];
      |                                    ^
a.cc:35:30: error: 'f' was not declared in this scope
   35 |         for(i=1;i<=lim+1;++i)f[i]=1e18;
      |                              ^
a.cc:38:34: error: 'p' was not declared in this scope
   38 |                 int r=min(z[i],j/p[i]);
      |                                  ^
a.cc:40:25: error: 'f' was not declared in this scope
   40 |                         f[j]=min(f[j],f[j-p[i]*k]+v[i]*k);
      |                         ^
a.cc:40:51: error: 'v' was not declared in this scope
   40 |                         f[j]=min(f[j],f[j-p[i]*k]+v[i]*k);
      |                                                   ^
a.cc:41:17: error: 'f' was not declared in this scope
   41 |                 f[j]=min(f[j],f[j+1]);
      |                 ^
a.cc:44:26: error: 'id' was not declared in this scope; did you mean 'i'?
   44 |         for(i=1;i<=N;++i)id[i]=i;
      |                          ^~
      |                          i
a.cc:45:19: error: 'id' was not declared in this scope; did you mean 'i'?
   45 |         std::sort(id+1,id+N+1,cmp);
      |                   ^~
      |                   i
a.cc:49:17: error: 'nm' was not declared in this scope; did you mean 'tm'?
   49 |                 nm[i]=min(z[id[i]],j/v[id[i]]);
      |                 ^~
      |                 tm
a.cc:49:38: error: 'v' was not declared in this scope
   49 |                 nm[i]=min(z[id[i]],j/v[id[i]]);
      |                                      ^
a.cc:50:22: error: 'p' was not declared in this scope
   50 |                 ans+=p[id[i]]*nm[i];
      |                      ^
a.cc:57:20: error: 'f' was not declared in this scope
   57 |                 if(f[i]>X)break;
      |                    ^
a.cc:58:28: error: 'f' was not declared in this scope
   58 |                 int most=X-f[i];
      |                            ^
a.cc:61:32: error: 'nm' was not declared in this scope; did you mean 'tm'?
   61 |                         while(!nm[k])--k;
      |                                ^~
      |                                tm
a.cc:62:39: error: 'v' was not declared in this scope
   62 |                         int n=(j-most+v[id[k]]-1)/v[id[k]];
      |                                       ^
a.cc:8:12: error: 'int64' was not declared in this scope
    8 | #define ll int64
      |            ^~~~~
a.cc:63:32: note: in expansion of macro 'll'
   63 |                         n=min((ll)n,nm[k]);nm[k]-=n;
      |                                ^~
a.cc:63:37: error: 'nm' was not declared in this scope; did you mean 'n'?
   63 |                         n=min((ll)n,nm[k]);nm[k]-=n;
      |                                     ^~
      |                                     n
a.cc:64:44: error: 'p' was not declared in this scope
   64 |                         j-=v[id[k]]*n;tmp-=p[id[k]]*n;
      |                                            ^