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
#define int __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];
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];}
signed 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:10:8: note: in expansion of macro 'll'
   10 | inline ll read()
      |        ^~
a.cc:9:13: error: '__int64' does not name a type; did you mean '__int64_t'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:17:7: note: in expansion of macro 'int'
   17 | const int MN=55;
      |       ^~~
a.cc:8:12: error: '__int64' does not name a type; did you mean '__int64_t'?
    8 | #define ll __int64
      |            ^~~~~~~
a.cc:18:1: note: in expansion of macro 'll'
   18 | ll N,X,D,v[MN],p[MN],z[MN],id[MN],nm[MN];
      | ^~
a.cc:9:13: error: '__int64' was not declared in this scope; did you mean '__ynf64'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:19:8: note: in expansion of macro 'int'
   19 | vector<int>s[MN];
      |        ^~~
a.cc:19:11: error: template argument 1 is invalid
   19 | vector<int>s[MN];
      |           ^
a.cc:19:11: error: template argument 2 is invalid
a.cc:19:14: error: 'MN' was not declared in this scope
   19 | vector<int>s[MN];
      |              ^~
a.cc:20:6: error: variable or field 'dfs' declared void
   20 | void dfs(int x)
      |      ^~~
a.cc:9:13: error: '__int64' was not declared in this scope; did you mean '__ynf64'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:20:10: note: in expansion of macro 'int'
   20 | void dfs(int x)
      |          ^~~
a.cc:8:12: error: '__int64' does not name a type; did you mean '__int64_t'?
    8 | #define ll __int64
      |            ^~~~~~~
a.cc:26:1: note: in expansion of macro 'll'
   26 | ll f[MN*MN*MN];
      | ^~
a.cc:9:13: error: '__int64' does not name a type; did you mean '__int64_t'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:27:16: note: in expansion of macro 'int'
   27 | bool cmp(const int&x,const int&y){return 1ll*p[x]*v[y]>1ll*p[y]*v[x];}
      |                ^~~
a.cc:9:13: error: '__int64' does not name a type; did you mean '__int64_t'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:27:28: note: in expansion of macro 'int'
   27 | bool cmp(const int&x,const int&y){return 1ll*p[x]*v[y]>1ll*p[y]*v[x];}
      |                            ^~~
a.cc: In function 'bool cmp(const int&, const int&)':
a.cc:27:46: error: 'p' was not declared in this scope
   27 | bool cmp(const int&x,const int&y){return 1ll*p[x]*v[y]>1ll*p[y]*v[x];}
      |                                              ^
a.cc:27:51: error: 'v' was not declared in this scope
   27 | 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:30:9: error: 'N' was not declared in this scope
   30 |         N=read(),X=read(),D=read();
      |         ^
a.cc:30:15: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   30 |         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:30:18: error: 'X' was not declared in this scope
   30 |         N=read(),X=read(),D=read();
      |                  ^
a.cc:30:24: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   30 |         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:27: error: 'D' was not declared in this scope
   30 |         N=read(),X=read(),D=read();
      |                           ^
a.cc:30:33: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   30 |         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:9:13: error: '__int64' does not name a type; did you mean '__int64_t'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:31:13: note: in expansion of macro 'int'
   31 |         reg int i,j,k;int lim=0;
      |             ^~~
a.cc:9:13: error: '__int64' was not declared in this scope; did you mean '__ynf64'?
    9 | #define int __int64
      |             ^~~~~~~
a.cc:31:23: note: in expansion of macro 'int'
   31 |         reg int i,j,k;int lim=0;
      |                       ^~~
a.cc:32:13: error: 'v' was not declared in this scope
   32 |         for(v[1]=read(),i=2;i<=N;++i)
      |             ^
a.cc:32:22: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   32 |         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:25: error: 'i' was not declared in this scope
   32 |         for(v[1]=read(),i=2;i<=N;++i)
      |                         ^
a.cc:33:26: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   33 |                 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:33:29: error: 's' was not declared in this scope
   33 |                 v[i]=read(),s[read()].push_back(i),z[i]=min(D,N);
      |                             ^
a.cc:33:35: error: too few arguments to function 'ssize_t read(int, void*, size_t)'
   33 |                 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:33:52: error: 'z' was not declared in this scope
   33 |                 v[i]=read(),s[read()].push_back(i),z[i]=min(D,N);
      |                                                    ^
a.cc:34:9: error: 'dfs' was not declared in this scope; did you mean 'ffs'?
   34 |         dfs(1);z[1]=N;
      |         ^~~
      |         ffs
a.cc:34:16: error: 'z' was not declared in this scope
   34 |         dfs(1);z[1]=N;
      |                ^
a.cc:35:13: error: 'i' was not declared in this scope
   35 |         for(i=1;i<=N;++i)lim+=z[i]*p[i];
      |             ^
a.cc:35:26: error: 'lim' was not declared in this scope
   35 |         for(i=1;i<=N;++i)lim+=z[i]*p[i];
      |                          ^~~
a.cc:35:36: error: 'p' was not declared in this scope
   35 |         for(i=1;i<=N;++i)lim+=z[i]*p[i];
      |                                    ^
a.cc:36:13: error: 'i' was not declared in this scope
   36 |         for(i=1;i<=lim+1;++i)f[i]=1e18;
      |             ^
a.cc:36:20: error: 'lim' was not declared in this scope
   36 |         for(i=1;i<=lim+1;++i)f[i]=1e18;
      |                    ^~~
a.cc:36:30: error: 'f' was not declared in this scope
   36 |         for(i=1;i<=lim+1;++i)f[i]=1e18;
      |                              ^
a.cc:37:13: error: 'i' was not declared in this scope
   37 |         for(i=1;i<=N;++i)for(j=lim;j;--j)
      |             ^
a.cc:37:30: error: 'j' was not declared in this scope; did you mean 'jn'?
   37 |         for(i=1;i<=N;++i)for(j=lim;j;--j)
      |                              ^
      |                              jn
a.cc:37:32: error: 'lim' was not declared in this scope
   37 |         for(i=1;i<=N;++i)for(j=lim;j;--j)
      |                                ^~~
a.cc:39:21: error: expected ';' before 'r'
   39 |                 int r=min(z[i],j/p[i]);
      |                     ^
a.cc:40:21: error: 'k' was not declared in this scope
   40 |                 for(k=1;k<=r;++k)
      |                     ^
a.cc:40:28: error: 'r' was not declared in this scope
   40 |                 for(k=1;k<=r;++k)
      |                            ^
a.cc:41:25: error: 'f' was not declared in this scope
   41 |                         f[j]=min(f[j],f[j-p[i]*k]+v[i]*k);
      |                         ^
a.cc:41:43: error: 'p' was not declared in this scope
   41 |                         f[j]=min(f[j],f[j-p[i]*k]+v[i]*k);
      |                                           ^
a.cc:41:51: error: 'v' was not declared in this scope
   41 |                         f[j]=min(f[j],f[j-p[i]*k]+v[i]*k);
      |                                                   ^
a.cc:42:17: error: 'f' was not declared in this scope
   42 |                 f[j]=min(f[j],f[j+1]);
      |                 ^
a.cc:44:23: error: 'i' was not declared in this scope
   44 |         for(z[1]=1e14,i=2;i<=N;++i)z[i]=D-z[i];
      |                       ^
a.cc:45:13: error: 'i' was not declared in this scope
   45 |         for(i=1;i<=N;++i)id[i]=i;
      |             ^
a.cc:45:26: error: 'id' was not declared in this scope
   45 |         for(i=1;i<=N;++i)id[i]=i;
      |                          ^~
a.cc:46:19: error: 'id' was not declared in this scope
   46 |         std::sort(id+1,id+N+1,cmp);
      |                   ^~
a.cc:47:13: error: expected ';' before 'ans'
   47 |         int ans=0;
      |             ^~~
a.cc:48:13: error: 'j' was not declared in this scope; did you mean 'jn'?
   48 |         for(j=X,i=1;i<=N;++i)
      |             ^
      |             jn
a.cc:48:17: error: 'i' was not declared in this scope
   48 |         for(j=X,i=1;i<=N;++i)
      |                 ^
a.cc:50:17: error: 'nm' was not declared in this scope; did you mean 'tm'?
   50 |                 nm[i]=min(z[id[i]],j