60pts WA求助
查看原帖
60pts WA求助
1126361
zzx0714楼主2023/10/12 21:21

求助,WA on #4#5

#include<bits/stdc++.h>
#define int long long
#define N 205
#define inf 0x7fffffff
using namespace std;
inline int read()
{
	int n=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0' && c<='9') n=(n<<3)+(n<<1)+(c^48),c=getchar();
	return n*f;
}
inline void write(int x)
{
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10^48);
	return;
}
int n,m,a[N],f[N][N];
vector<int>g[N];
inline void dfs(int u)
{
	f[u][1]=a[u];
	for(auto v:g[u])
	{
		dfs(v);
		for(int w=m;w>0;w--)
		{
			for(int sw=0;sw<w;sw++)
			{
				f[u][w]=max(f[u][w],f[v][sw]+f[u][w-sw]);
			}
		}
	}
	return;
}
signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    n=read(),m=read(),m++;
	int i,j;
	for(i=1;i<=n;i++,j++)
	{
		int v=read();
		a[i]=read();
		g[v].push_back(i);
	}
	dfs(0);
	write(f[0][m]);
	return 0;
}
2023/10/12 21:21
加载中...