Language: C++

#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 51;
const int   M   = N * N * N;

int nCh[N];
ll  sum[N];
int p[N];
int a[N];

ll  f[N][M];

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;
    int S;  cin >> S;
    int D;  cin >> D;

    for(int i = 1 ; i <= n ; ++i)   {
        nCh[i] = 1;
        cin >> sum[i];
        
        if (i > 1)  cin >> p[i];
    }
    for(int i = n ; i >= 2 ; --i)
        nCh[p[i]] += nCh[i],
        sum[p[i]] += sum[i];
    
    a[1] = 1e9; fill(a + 2,a + 1 + n,D);

    for(int i = 0 ; i <= n ; ++i)
    for(int w = 1 ; w <  M ; ++w)   f[i][w] = 1e18;

    for(int i = 0 ; i < n ; ++i)
    for(int w = 0 ; w < M ; ++w)    if (f[i][w] < 1e18) {
        int c = min(a[i + 1],50);

        for(int j = 0 ; j <= c ; ++j)   {
            int nv = nCh[i + 1] * j + w;
            ll  nw = sum[i + 1] * j + f[i][w]; 

            if (nv >= M)    break;
            if (f[i + 1][nv] > nw)
                f[i + 1][nv] = nw;
        }
    }
    int ans = 0;

    vector<int> order(n);

    iota(all(order),1);
    sort(all(order),[&](int x,int y)    {
        return  1ll * nCh[x] * sum[y] > 1ll * nCh[y] * sum[x]; 
    });

    for(int i = 1 ; i <= n ; ++i)   a[i] -= min(a[i],50);
    for(int w = 0 ; w <  M ; ++w)   if (f[n][w] <= S)   {
        int val = S - f[n][w];
        int nxt = w;

        for(int x : order)  {
            int c = min(a[x],(int)(val / sum[x]));

            val -= c * sum[x];
            nxt += c * nCh[x];
        }
        if (ans < nxt)
            ans = nxt;
    }
    cout << ans << endl;
}#include<bits/stdc++.h>

using namespace std;

#define ll  long long
#define ld  double

#define sz(x)   (int)x.size()
#define all(x)  x.begin(),x.end()

#define pb  emplace_back
#define X   first
#define Y   second

const int   N   = 51;
const int   M   = N * N * N;

int nCh[N];
ll  sum[N];
int p[N];
int a[N];

ll  f[N][M];

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n;  cin >> n;
    int S;  cin >> S;
    int D;  cin >> D;

    for(int i = 1 ; i <= n ; ++i)   {
        nCh[i] = 1;
        cin >> sum[i];
        
        if (i > 1)  cin >> p[i];
    }
    for(int i = n ; i >= 2 ; --i)
        nCh[p[i]] += nCh[i],
        sum[p[i]] += sum[i];
    
    a[1] = 1e9; fill(a + 2,a + 1 + n,D);

    for(int i = 0 ; i <= n ; ++i)
    for(int w = 1 ; w <  M ; ++w)   f[i][w] = 1e18;

    for(int i = 0 ; i < n ; ++i)
    for(int w = 0 ; w < M ; ++w)    if (f[i][w] < 1e18) {
        int c = min(a[i + 1],50);

        for(int j = 0 ; j <= c ; ++j)   {
            int nv = nCh[i + 1] * j + w;
            ll  nw = sum[i + 1] * j + f[i][w]; 

            if (nv >= M)    break;
            if (f[i + 1][nv] > nw)
                f[i + 1][nv] = nw;
        }
    }
    int ans = 0;

    vector<int> order(n);

    iota(all(order),1);
    sort(all(order),[&](int x,int y)    {
        return  1ll * nCh[x] * sum[y] > 1ll * nCh[y] * sum[x]; 
    });

    for(int i = 1 ; i <= n ; ++i)   a[i] -= min(a[i],50);
    for(int w = 0 ; w <  M ; ++w)   if (f[n][w] <= S)   {
        int val = S - f[n][w];
        int nxt = w;

        for(int x : order)  {
            int c = min(a[x],(int)(val / sum[x]));

            val -= c * sum[x];
            nxt += c * nCh[x];
        }
        if (ans < nxt)
            ans = nxt;
    }
    cout << ans << endl;
}

Compiler output:
a.cc:85:2: error: stray '#' in program
   85 | }#include<bits/stdc++.h>
      |  ^
a.cc:85:3: error: 'include' does not name a type
   85 | }#include<bits/stdc++.h>
      |   ^~~~~~~
a.cc:99:13: error: redefinition of 'const int N'
   99 | const int   N   = 51;
      |             ^
a.cc:15:13: note: 'const int N' previously defined here
   15 | const int   N   = 51;
      |             ^
a.cc:100:13: error: redefinition of 'const int M'
  100 | const int   M   = N * N * N;
      |             ^
a.cc:16:13: note: 'const int M' previously defined here
   16 | const int   M   = N * N * N;
      |             ^
a.cc:102:5: error: redefinition of 'int nCh [51]'
  102 | int nCh[N];
      |     ^~~
a.cc:18:5: note: 'int nCh [51]' previously declared here
   18 | int nCh[N];
      |     ^~~
a.cc:103:5: error: redefinition of 'long long int sum [51]'
  103 | ll  sum[N];
      |     ^~~
a.cc:19:5: note: 'long long int sum [51]' previously declared here
   19 | ll  sum[N];
      |     ^~~
a.cc:104:5: error: redefinition of 'int p [51]'
  104 | int p[N];
      |     ^
a.cc:20:5: note: 'int p [51]' previously declared here
   20 | int p[N];
      |     ^
a.cc:105:5: error: redefinition of 'int a [51]'
  105 | int a[N];
      |     ^
a.cc:21:5: note: 'int a [51]' previously declared here
   21 | int a[N];
      |     ^
a.cc:107:5: error: redefinition of 'long long int f [51][132651]'
  107 | ll  f[N][M];
      |     ^
a.cc:23:5: note: 'long long int f [51][132651]' previously declared here
   23 | ll  f[N][M];
      |     ^
a.cc:109:5: error: redefinition of 'int main()'
  109 | int main()  {
      |     ^~~~
a.cc:25:5: note: 'int main()' previously defined here
   25 | int main()  {
      |     ^~~~