Codeforces Round #419 (Div. 1), problem: (C) Karen and Supermarket Solution In C/C++

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=5010;
int n,m,c[N],d[N],fa[N];
int w[N],head[N],next[N];
void add(int f,int t){
static int cnt=0;
w[++cnt]=t;
next[cnt]=head[f];
head[f]=cnt;
}
int f[N][N],g[N][N],size[N];
//f[i][j],g[i][j]表示在子树i中买j件物品的最小代价
//其中g要求购买从根到i的物品
void dfs(int x){
for (int i=1;i<=n;i++) f[x][i]=g[x][i]=1e9+1;
g[x][0]=g[x][1]=c[x]-d[x];
f[x][1]=c[x];
size[x]=1;
for (int i=head[x];i;i=next[i]){
int v=w[i];
dfs(v);
for (int a=size[x];a>=0;a–)
for (int b=1;b<=size[v];b++){
f[x][a+b]=min(f[x][a+b],f[x][a]+f[v][b]);
g[x][a+b]=min(g[x][a+b],g[x][a]+min(f[v][b],g[v][b]));
}
size[x]+=size[v];
}
}
int main()
{
scanf(“%d%d”,&n,&m);
scanf(“%d%d”,&c[1],&d[1]);
for (int i=2;i<=n;i++){
scanf(“%d%d%d”,&c[i],&d[i],&fa[i]);
add(fa[i],i);
}
dfs(1);
int ans=0;
for (int i=1;i<=n;i++)
if (m>=f[1][i]||m>=g[1][i]) ans=i;
printf(“%d\n”,ans);
return 0;
}

Best Mix-In Ideas for...

Ultimate Mix-In Ideas & Tips for Ninja Creami The Mix-In...

Ninja Creami Outer Bowl...

How to Fix a Stuck Ninja Creami Bowl or...

Can You Put Frozen...

Can You Process Loose Frozen Fruit in the Ninja...

How to Spot and...

Keeping Your Ninja Creami 100% Mold-Free Because the Ninja Creami...

How to Make Sorbet...

Master Sorbet & Gelato in the Ninja Creami The Ninja...

Can You Use Dairy-Free...

Making Vegan & Dairy-Free Ice Cream in the Ninja...

Best Mix-In Ideas for the Ninja Creami: Cookies, Candy, Fruit, and Nuts

Ultimate Mix-In Ideas & Tips for Ninja Creami The Mix-In button is designed to fold your favorite toppings into processed ice cream without pulverizing them...

Ninja Creami Outer Bowl and Paddle Lock Stuck Issue: Easy Fixes

How to Fix a Stuck Ninja Creami Bowl or Paddle Lock If your outer bowl won't twist off the machine base or the red paddle...

Can You Put Frozen Whole Fruit Directly into the Ninja Creami?

Can You Process Loose Frozen Fruit in the Ninja Creami? No, you should never put loose, dry frozen fruit chunks directly into the Ninja Creami. 🛒...

How to Spot and Prevent Mold in the Ninja Creami Lid and Paddle Assembly

Keeping Your Ninja Creami 100% Mold-Free Because the Ninja Creami processes milk and sugar mixtures, moisture can collect in the spring-loaded paddle latch mechanism on...

How to Make Sorbet and Gelato in the Ninja Creami

Master Sorbet & Gelato in the Ninja Creami The Ninja Creami features dedicated Sorbet and Gelato one-touch buttons programmed for specific blade speeds and downward...

Can You Use Dairy-Free Milk in the Ninja Creami? (Almond, Oat, Coconut Guide)

Making Vegan & Dairy-Free Ice Cream in the Ninja Creami Yes! The Ninja Creami is a dream appliance for lactose-intolerant and vegan diets because micro-shaving...

Ninja Creami vs Traditional Churn Ice Cream Maker: What’s the Difference?

Ninja Creami vs Traditional Churners: Key Differences Traditional ice cream makers churn liquid while slowly freezing it. The Ninja Creami flips this concept upside down...

Best Extra Pint Containers and Lids for the Ninja Creami

Why You Need Extra Pints for Your Ninja Creami Because every Ninja Creami base requires 24 hours of freeze prep time, owning only 2 pints...

Is the Ninja Creami Dishwasher Safe? Cleaning and Maintenance Guide

Ninja Creami Dishwasher Safety & Cleaning Guide Proper cleaning ensures your Ninja Creami stays hygienic and prevents food residue build-up in the paddle lock mechanism....