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

I Tried Cordless Stick...

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

Post-quantum stocks soar in...

Post-Quantum Cryptography Stocks Surge Following Bitcoin Sell-Off Financial markets across...

I Tried Ergonomic Office...

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

ACR’s Radiology AI Accreditation...

As artificial intelligence continues to expand its footprint in...

Dancing, insults can’t build...

According to a report by The Punch, Senior Advocate...

I Tried Smart Security...

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

I Tried Cordless Stick Vacuums For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point in your routine when you realise enough is enough and my...

Post-quantum stocks soar in Hong Kong, mainland after security concerns spark bitcoin rout

Post-Quantum Cryptography Stocks Surge Following Bitcoin Sell-Off Financial markets across Hong Kong and mainland China experienced a dramatic sector rotation this week as investors poured...

I Tried Ergonomic Office Chairs For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.Upgrading my workspace routine changed everything, and it started with a simple realization that my...

ACR’s Radiology AI Accreditation Offers Health Systems a Benchmark for AI Inventory, Monitoring and Rollback

As artificial intelligence continues to expand its footprint in medical imaging, healthcare organizations face a profound operational shift. Deploying an imaging algorithm is no...

Dancing, insults can’t build Nigeria democracy – Okutepa

According to a report by The Punch, Senior Advocate of Nigeria (SAN) Jibrin Okutepa has stated that theatrical performances, dancing, and personal insults at...

I Tried Smart Security Cameras For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.After putting this through a proper 30-day real-world test, I finally understand why so many...

Chonky squirrel Stump wins 2026 Fat Squirrel Week

While Katmai National Park’s Fat Bear Week has long captured the internet's obsession with chubby wildlife preparing for hibernation, urban rodents are finally getting...

I Tried Noise Cancelling Earbuds For 30 Days Here Is the Honest Truth

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point in your routine when you realise enough is enough and the...

Is the Epicka Universal Travel Adapter Worth $20? Long-Term Durability Review

Long-Term Travel Endurance: Is the Epicka Adapter Worth $20? In the world of travel gear, budget accessories priced under $25 often carry an unfortunate stigma....