Submission #3008823


Source Code Expand

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#define llint long long
#define inf (llint)1e18

using namespace std;
typedef pair<llint, llint> P;

struct Item
{
	int id;
	double w, v;
	Item(){}
	Item(int i, double a, double b){
		id = i, w = a, v = b;
	}
	bool operator<(const Item &obj)const{
		double rate = v / w, rate2 = obj.v / obj.w;
		return rate > rate2;
		
	}
};

llint n, x, d;
llint m[55];
vector<int> G[55];
llint W[55], V[55], N[55];
llint remN[55];
vector<llint> wvec, vvec;

llint dp[405][130005];
llint cost[130005];

P dfs(int v)
{
	P ret = make_pair(m[v], 1);
	for(int i = 0; i < G[v].size(); i++){
		P res = dfs(G[v][i]);
		ret.first += res.first;
		ret.second += res.second;
	}
	W[v] = ret.first;
	V[v] = ret.second;
	return ret;
}

void calcDP()
{
	for(int i = 1; i <= n; i++){
		llint useN = min(50LL, N[i]);
		remN[i] = N[i] - useN;
		for(int j = 0; j >= 0; j++){
			llint m = 1<<j;
			if(N[i] <= m) m = N[i], j = -2;
			wvec.push_back(W[i] * m);
			vvec.push_back(V[i] * m);
			N[i] -= m;
		}
	}
	llint num = wvec.size(), mx = n*n*n;
	
	for(int i = 0; i <= num; i++){
		for(int j = 0; j <= mx; j++){
			dp[i][j] = inf;
		}
	}
	dp[0][0] = 0;
	
	for(int i = 0; i < num; i++){
		for(int j = 0; j <= mx; j++){
			if(j + vvec[i] <= mx) dp[i+1][j+vvec[i]] = min(dp[i+1][j+vvec[i]], dp[i][j] + wvec[i]);
			dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
		}
	}
	
	for(int i = 0; i <= mx; i++) cost[i] = dp[num][i];
}

int main(void)
{
	cin >> n >> x >> d;
	cin >> m[1];
	int p;
	for(int i = 2; i <= n; i++){
		cin >> m[i] >> p;
		G[p].push_back(i);
	}
	dfs(1);
	
	N[1] = x;
	for(int i = 2; i <= n; i++) N[i] = d;
	calcDP();
	
	Item item[55];
	for(int i = 1; i <= n; i++){
		item[i] = Item(i, (double)W[i], (double)V[i]);
	}
	sort(item+1, item+n+1);
	
	llint ans = 0;
	for(int i = 0; i <= n*n*n; i++){
		if(cost[i] > x) continue;
		llint remX = x - cost[i], v = i;
		
		for(int j = 1; j <= n; j++){
			llint id = item[j].id;
			llint cap = remX / W[id];
			llint buy = min(cap, remN[id]);
			v += buy * V[id];
			remX -= buy * W[id];
		}
		ans = max(ans, v);
	}
	cout << ans << endl;
	
	return 0;
}

Submission Info

Submission Time
Task F - Sweet Alchemy
User leaf1415
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2257 Byte
Status RE
Exec Time 208 ms
Memory 411648 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 900
Status
AC × 2
WA × 1
AC × 14
WA × 8
MLE × 7
RE × 34
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 3 ms 8448 KB
a02 AC 4 ms 14592 KB
a03 WA 23 ms 112896 KB
b04 AC 2 ms 2304 KB
b05 RE 195 ms 411520 KB
b06 AC 47 ms 84096 KB
b07 AC 47 ms 84096 KB
b08 AC 144 ms 84096 KB
b09 AC 143 ms 84096 KB
b10 RE 196 ms 411520 KB
b11 RE 196 ms 411520 KB
b12 RE 196 ms 411520 KB
b13 RE 197 ms 411520 KB
b14 WA 13 ms 59648 KB
b15 WA 10 ms 47360 KB
b16 MLE 176 ms 360448 KB
b17 WA 17 ms 82176 KB
b18 WA 23 ms 110848 KB
b19 AC 7 ms 33024 KB
b20 MLE 79 ms 352640 KB
b21 RE 196 ms 411392 KB
b22 RE 181 ms 410240 KB
b23 AC 8 ms 33024 KB
b24 RE 196 ms 411520 KB
b25 AC 122 ms 233600 KB
b26 RE 196 ms 411520 KB
b27 RE 196 ms 411520 KB
b28 MLE 148 ms 282752 KB
b29 AC 95 ms 182400 KB
b30 MLE 174 ms 331904 KB
b31 MLE 200 ms 381056 KB
b32 RE 196 ms 411520 KB
b33 MLE 200 ms 381056 KB
b34 RE 195 ms 411392 KB
b35 WA 13 ms 59648 KB
b36 WA 20 ms 98560 KB
b37 AC 22 ms 106752 KB
b38 WA 48 ms 233728 KB
b39 RE 178 ms 409984 KB
b40 RE 194 ms 411392 KB
b41 AC 30 ms 149760 KB
b42 RE 181 ms 410240 KB
b43 AC 18 ms 88320 KB
b44 RE 196 ms 411648 KB
b45 RE 196 ms 411520 KB
b46 RE 195 ms 411520 KB
b47 RE 196 ms 411520 KB
b48 RE 198 ms 411520 KB
b49 RE 196 ms 411520 KB
b50 RE 195 ms 411520 KB
b51 RE 195 ms 411520 KB
b52 RE 197 ms 411520 KB
b53 RE 197 ms 411520 KB
b54 RE 177 ms 409984 KB
b55 RE 183 ms 410240 KB
b56 RE 195 ms 411392 KB
b57 RE 196 ms 411520 KB
b58 RE 195 ms 411520 KB
b59 RE 196 ms 411520 KB
b60 RE 195 ms 411520 KB
b61 RE 196 ms 411520 KB
b62 RE 196 ms 411520 KB
b63 MLE 208 ms 379008 KB