Language: C++

/*
 * Code be weshen zyk :'(
 * I won't surrender quietly
 */
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma comment(linker, "/STACK:1024000000,1024000000")

#include <bits/stdc++.h>

#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

template<class T> using orderd_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define S second
#define F first
#define pb push_back

using ii = pair<int, int>;
using ll = long long;
using db = long double;

const int N = 50, K = 205, MOD = 998244353;
const ll MX = 1e15;
const int blkSz = 320;
int sz[N + 1];
int n, x, d;
ll m[N + 1], p[N + 1];
ll dp[N * N * N + 1];
int main() {
//#ifdef ONLINE_JUDGE
//	ios_base::sync_with_stdio(0);
//	cin.tie(0);
//#endif

	cin >> n >> x >> d;
	cin >> m[1];
	sz[1] = 1;
	for (int i = 2; i <= n; i++) {
		cin >> m[i] >> p[i];
		sz[i] = 1;
	}

	for (int i = n; i >= 1; --i) {
		sz[p[i]] += sz[i];
		m[p[i]] += m[i];
	}

	int ord[n + 1];
	iota(ord + 1, ord + n + 1, 1);

	sort(ord + 1, ord + n + 1, [](int i, int j) {
		///cost / doughnut
		///m[i] / sz[i] < m[j] / sz[j];
			return 1ll * m[i] * sz[j] < 1ll * m[j] * sz[i];
		});
	for (int i = 0; i <= n; i++)
		fill(dp[i], dp[i] + N * N * N + 1, x + 1);
	dp[0][0] = 0;
	int limit[n + 1];
	fill(limit + 1, limit + n + 1, d);
	limit[1] = (1 << 30);
	for (int i = 1; i <= n; i++) {
		for (int bag = N * N * N; bag >= 0; --bag)
			for (int take = 0;
					bag - take * sz[i] >= 0 && take <= min(N, limit[i]); take++)
				dp[bag] = min(dp[bag], dp[bag - take * sz[i]] + m[i] * take);
	}
	int ans = 0;
	for (int take = 0; take <= N * N * N; take++)
		if (dp[take] <= x) {
			int pLimit[n + 1];
			int tmp = take;

			for (int i = 1; i < n; i++) {
				if (limit[i] >= 50)
					pLimit[i] = limit[i] - 50;
				else
					pLimit[i] = 0;
			}

			int lft = x - dp[take];
			int cur = take;
			for (int j = 1; j <= n; j++) {
				int howMuch = lft / m[ord[j]];
				howMuch = min(howMuch, pLimit[ord[j]]);
				cur += howMuch * sz[ord[j]];
				lft = lft - m[ord[j]] * howMuch;
			}
			ans = max(ans, cur);
		}

	cout << ans << '\n';

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:66:14: error: invalid types 'll {aka long long int}[int]' for array subscript
   66 |         dp[0][0] = 0;
      |              ^
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:10:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of 'typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = long long int; _Tp = int; typename __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type = void]':
/usr/include/c++/14/bits/stl_algobase.h:998:21:   required from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = long long int; _Tp = int]'
  998 |     { std::__fill_a1(__first, __last, __value); }
      |       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1029:20:   required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = long long int; _Tp = int]'
 1029 |       std::__fill_a(__first, __last, __value);
      |       ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:65:7:   required from here
   65 |                 fill(dp[i], dp[i] + N * N * N + 1, x + 1);
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:952:9: error: invalid type argument of unary '*' (have 'long long int')
  952 |         *__first = __tmp;
      |         ^~~~~~~~