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

7 Reasons This Viral...

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

Jaishankar meets senior Japanese...

In a significant step forward for Indo-Pacific diplomacy, External...

Why Viral Kitchen Gadgets...

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

How does a raccoon...

Raccoons are ubiquitous across both rural woodlands and bustling...

We Tested Viral Kitchen...

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

Brad Pitt and Ines...

Hollywood royalty made a stylish statement in Los Angeles...

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...

Jaishankar meets senior Japanese leaders; discusses deeper strategic, economic ties

In a significant step forward for Indo-Pacific diplomacy, External Affairs Minister S. Jaishankar met with senior Japanese leaders in Tokyo to deliberate on enhancing...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

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

How does a raccoon act with rabies?

Raccoons are ubiquitous across both rural woodlands and bustling suburban neighborhoods. While these masked foragers are generally cautious and nocturnal, their close proximity to...

We Tested Viral Kitchen Gadgets Here Is What You Must Know

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Brad Pitt and Ines De Ramon walk ‘The Further Mis-Adventures of Cliff Booth’ premiere red carpet hand in hand

Hollywood royalty made a stylish statement in Los Angeles as Oscar-winning actor Brad Pitt arrived hand in hand with his partner, jewelry designer Inés...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.Most products in this category disappoint within weeks. Cheap materials, vague instructions, and results that...

US and rivals spar over next UN boss as race lurches towards deadline

According to a report by Al Jazeera, diplomatic tensions among global powers have intensified as the United Nations nears the deadline to select its...

Why Viral Kitchen Gadgets Is Currently Breaking the Internet

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