Submission #3025248


Source Code Expand

#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO
#define ALL(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;


/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/

const int MAXN = 55;

int n;
ll x, d;
ll m[MAXN];
int p[MAXN];
int sz[MAXN];

const int MX = 150000;
array<ll, MX> dp;
int lst[MAXN];

int main() {
#ifdef FASTIO
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
	cin >> n >> x >> d;
	cin >> m[0];
	for (int i = 1; i < n; ++i) {
		cin >> m[i] >> p[i];
		--p[i];
	}
	for (int i = n - 1; i >= 0; --i) {
		sz[i] += 1;
		if (i != 0) {
			m[p[i]] += m[i];
			sz[p[i]] += sz[i];
		}
	}
	vector<array<ll, 3>> vv;
	vv.push_back({m[0], sz[0], x});
	for (int i = 1; i < n; ++i)
		vv.push_back({m[i], sz[i], d});
	assert(n <= 50);
	for (int i = 0; i < vv.size(); ++i)
		assert(vv[i][1] <= 50);
	sort(vv.begin(), vv.end(), [] (array<ll, 3> a, array<ll, 3> b) {
			return a[0] * b[1] < a[1] * b[0];
			});

	ll add = 0;
	for (int i = 0; i < vv.size(); ++i) {
		if (vv[i][2] <= n)
			continue;
		ll lf = x;
		for (int j = 0; j < i; ++j) {
			lf -= vv[j][0] * vv[j][2];
			if (lf < 0)
				break;
		}
		for (int j = i + 1; j < vv.size() && lf >= 0; ++j) {
			int k = 0;
			while ((k + 1) * vv[j][0] / vv[i][0] * vv[i][1] < (k + 1) * vv[j][1])
				++k;
			lf -= vv[j][0] * k;
		}
		if (lf >= 0) {
			ll go = min(vv[i][2] - n, lf / vv[i][0]);
			add += vv[i][1] * go;
			x -= vv[i][0] * go;
			lf -= vv[i][0] * go;
			vv[i][2] -= go;
			if (vv[i][2] > n)
				break;
		}
		else {
			break;
		}
	}
	dp.fill(x + 1);
	dp[0] = 0;
	for (int i = 0; i < vv.size(); ++i) {
		int n = vv[i][1];
		ll cs = vv[i][0];
		ll av = vv[i][2];
		for (int j = 0; j < n; ++j)
			lst[j] = j;
		auto dp2 = dp;
		for (int i = 0; i < MX; ++i) {
			int g = i % n;
			if (lst[g] <= i)
				lst[g] = i + n;
			int k = (lst[g] - i) / n;
			for (; lst[g] < MX && k <= av; lst[g] += n, ++k) {
				if (dp2[lst[g]] > dp[i] + cs * k)
					dp2[lst[g]] = dp[i] + cs * k;
				else
					break;
			}
		}
		dp = dp2;
	}
	ll ans = add;
	for (int i = 0; i < MX; ++i)
		if (dp[i] <= x)
			ans = max(ans, add + i);
	cout << ans << "\n";
	return 0;
}


Submission Info

Submission Time
Task F - Sweet Alchemy
User LHiC
Language C++14 (GCC 5.4.1)
Score 900
Code Size 2578 Byte
Status AC
Exec Time 65 ms
Memory 2560 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 3
AC × 63
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56, b57, b58, b59, b60, b61, b62, b63
Case Name Status Exec Time Memory
a01 AC 6 ms 2560 KB
a02 AC 6 ms 2560 KB
a03 AC 8 ms 2560 KB
b04 AC 5 ms 2560 KB
b05 AC 65 ms 2560 KB
b06 AC 63 ms 2560 KB
b07 AC 63 ms 2560 KB
b08 AC 65 ms 2560 KB
b09 AC 65 ms 2560 KB
b10 AC 65 ms 2560 KB
b11 AC 65 ms 2560 KB
b12 AC 65 ms 2560 KB
b13 AC 65 ms 2560 KB
b14 AC 8 ms 2560 KB
b15 AC 7 ms 2560 KB
b16 AC 62 ms 2560 KB
b17 AC 13 ms 2560 KB
b18 AC 19 ms 2560 KB
b19 AC 6 ms 2560 KB
b20 AC 29 ms 2560 KB
b21 AC 64 ms 2560 KB
b22 AC 45 ms 2560 KB
b23 AC 5 ms 2560 KB
b24 AC 65 ms 2560 KB
b25 AC 65 ms 2560 KB
b26 AC 65 ms 2560 KB
b27 AC 65 ms 2560 KB
b28 AC 65 ms 2560 KB
b29 AC 65 ms 2560 KB
b30 AC 65 ms 2560 KB
b31 AC 65 ms 2560 KB
b32 AC 65 ms 2560 KB
b33 AC 65 ms 2560 KB
b34 AC 64 ms 2560 KB
b35 AC 6 ms 2560 KB
b36 AC 9 ms 2560 KB
b37 AC 10 ms 2560 KB
b38 AC 19 ms 2560 KB
b39 AC 29 ms 2560 KB
b40 AC 63 ms 2560 KB
b41 AC 12 ms 2560 KB
b42 AC 45 ms 2560 KB
b43 AC 7 ms 2560 KB
b44 AC 65 ms 2560 KB
b45 AC 65 ms 2560 KB
b46 AC 65 ms 2560 KB
b47 AC 65 ms 2560 KB
b48 AC 65 ms 2560 KB
b49 AC 65 ms 2560 KB
b50 AC 65 ms 2560 KB
b51 AC 65 ms 2560 KB
b52 AC 65 ms 2560 KB
b53 AC 65 ms 2560 KB
b54 AC 32 ms 2560 KB
b55 AC 46 ms 2560 KB
b56 AC 64 ms 2560 KB
b57 AC 65 ms 2560 KB
b58 AC 65 ms 2560 KB
b59 AC 65 ms 2560 KB
b60 AC 65 ms 2560 KB
b61 AC 65 ms 2560 KB
b62 AC 65 ms 2560 KB
b63 AC 65 ms 2560 KB