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;
}

Advertisement

spot_img

Stock market rebound in...

Indian benchmark equity indices witnessed a decisive rebound during...

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

Gen Z is spending...

A dramatic transformation is sweeping through the elite corners...

I Tried Viral Kitchen...

📢 As an Amazon Associate, I earn from qualifying...

Live-Action Can You Kiss...

Fans of romantic manga adaptations have an exciting new...

7 Reasons This Viral...

📢 As an Amazon Associate, I earn from qualifying...

Stock market rebound in early trade: Sensex jumps over 400 points, Nifty holds above 22,390

Indian benchmark equity indices witnessed a decisive rebound during early trade, bringing substantial relief to investors following consecutive sessions of choppy price action. The...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Gen Z is spending more on art than ever before. It’s a key piece of the Great Wealth Transfer, UBS says

A dramatic transformation is sweeping through the elite corners of the global art market. Known largely for digital culture and disruptive social trends, Generation...

I Tried Viral Kitchen Gadgets For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Live-Action Can You Kiss Me First? Series’ Teaser Reveals Theme Song

Fans of romantic manga adaptations have an exciting new television premiere to look forward to, as the upcoming live-action series based on Yamayade's romance...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Technology Innovation Institute Completes Asteroid-Landing Probe for Landmark Emirates Mission to the Asteroid Belt

In a historic milestone for Middle Eastern space exploration, the Technology Innovation Institute (TII) has officially completed its state-of-the-art asteroid-landing probe. Designed and constructed...

We Tested Viral Kitchen Gadgets Here Is What You Must Know

After putting the Viral Kitchen Gadgets through a proper 30-day real-world test, I can tell you exactly what works, what does not, and who...

L&T wins ₹10,000-15,000 crore power transmission orders in India, Middle East

Indian infrastructure and engineering conglomerate Larsen & Toubro (L&T) has achieved another major milestone, securing multiple mega orders within its Power Transmission & Distribution...