#### 结果最后居然是数组大小的问题 QvQ….

Tarjan 过程：

inline void tarjan(int u){
low[u]=dfn[u]=++num;
hep[++top]=u;vis[u]=1;
int to=edge[i].to;
if(!dfn[to])tarjan(to),low[u]=min(low[u],low[to]);
else if(vis[to])low[u]=min(low[u],dfn[to]);
}if(dfn[u]==low[u]){
++tot;vis[u]=0;
while(hep[top+1]!=u)
fa[hep[top]]=tot,vis[hep[top--]]=0;
}return;
}//Tarjan 模板


inline void dp(int x){
for(register int i=cost[x];i<=m;++i)f[x][i]=val[x];
for(register int i=H[x];i;i=G[i].nxt){
int v=G[i].to;dp(v);
for(register int j=m-cost[x];j>=0;--j)
for(register int k=0;k<=j;++k)
f[x][j+cost[x]]=max(f[x][j+cost[x]],
f[x][j+cost[x]-k]+f[v][k]);
}return;
}


for(register int i=1;i<=n;++i)
if(!dfn[i])tarjan(i);
for(register int i=1;i<=n;++i){
val[fa[i]]+=v[i];cost[fa[i]]+=w[i];
if(fa[i]!=fa[up[i]]&&up[i])
}int s=tot+1;
for(register int i=1;i<=tot;++i)


#### 代码：

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define Match
using namespace std;
const int N=3e2+10;
template <typename Tp> inline void read(Tp &x){
x=0;char ch=getchar();
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
}

struct Edge{int nxt,to;}edge[N<<1],G[N<<1];
int low[N],dfn[N],num,hep[N],top,vis[N],fa[N],up[N],use[N];

{G[++C].nxt=H[u],G[C].to=v,H[u]=C;}

inline void tarjan(int u){
low[u]=dfn[u]=++num;hep[++top]=u;vis[u]=1;
int to=edge[i].to;
if(!dfn[to])tarjan(to),low[u]=min(low[u],low[to]);
else if(vis[to])low[u]=min(low[u],dfn[to]);
}if(dfn[u]==low[u]){
++tot;vis[u]=0;
while(hep[top+1]!=u)
fa[hep[top]]=tot,vis[hep[top--]]=0;
}return;
}
inline void dp(int x){
for(register int i=cost[x];i<=m;++i)f[x][i]=val[x];
for(register int i=H[x];i;i=G[i].nxt){
int v=G[i].to;dp(v);
for(register int j=m-cost[x];j>=0;--j)
for(register int k=0;k<=j;++k)
f[x][j+cost[x]]=max(f[x][j+cost[x]],
f[x][j+cost[x]-k]+f[v][k]);
}return;
}
int main(){
ios::sync_with_stdio(false);
#ifndef Match
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
scanf("%d%d",&n,&m);
memset(f,-inf,sizeof(f));
for(register int i=1;i<=n;++i)scanf("%d",&w[i]);
for(register int i=1;i<=n;++i)scanf("%d",&v[i]);
for(register int x,i=1;i<=n;++i){
scanf("%d",&x);up[i]=x;
}
for(register int i=1;i<=n;++i)
if(!dfn[i])tarjan(i);
for(register int i=1;i<=n;++i){
val[fa[i]]+=v[i];cost[fa[i]]+=w[i];
if(fa[i]!=fa[up[i]]&&up[i])
}int s=tot+1;
for(register int i=1;i<=tot;++i)