#include <bits/stdc++.h>
#define II int
#define IL inline
#define R register
#define I 510
using namespace std;

IL void of(R II &a) {
R char c=getchar (); R II w=1, p=0;
while (c<'0' || c>'9') { if(c=='-') w=-1; c=getchar (); }
while (c>='0' && c<='9') { p=p*10+c-'0'; c=getchar (); }
a=w*p;
}

/* -------------------- Peipei -------------------- */

II n,V,root,_tot,all,_top,bit;
II head[I], f[I][I], w[I], v[I], in[I], belong[I], kl[I], VV[I];
II DFN[I], LOW[I], Stack[I], cant[I], vis[I], W[I], had[I][I];

struct owo { II to,up; } aa[I], edge[I];
IL void add(R II x,R II y) {
aa[++_tot]=(owo) {y,head[x]};
head[x]=_tot;
}

IL void Tarjan(R II x) {
DFN[x]=LOW[x]=++all;
Stack[++_top]=x;
vis[x]=1;
for(R II i=head[x],go;i;i=aa[i].up)
{
go=aa[i].to;
if(!DFN[go]) {
Tarjan(go);
LOW[x]=min(LOW[x],LOW[go]);
} else if(vis[go]) LOW[x]=min(LOW[x],DFN[go]);
}

if(DFN[x]==LOW[x]) {
bit++;
do {
R II o=Stack[_top--];
vis[o]=0;
belong[o]=bit;
W[bit]+=w[o];
VV[bit]+=v[o];
} while (Stack[_top+1]!=x) ;
}
}

IL void dfs(R II x) {
for(R II i=VV[x];i<=V;i++) f[x][i]=W[x];
for(R II i=head[x],go;i;i=aa[i].up)
{
go=aa[i].to;
dfs(go);
for(R II j=V-VV[x];j>=0;j--)
for(R II k=j;k>=0;k--)
f[x][j+VV[x]]=max(f[x][j+VV[x]],f[x][j+VV[x]-k]+f[go][k]);
}
}

int main()
{
of(n); of(V);
for(R II i=1;i<=n;i++) of(v[i]);
for(R II i=1;i<=n;i++) of(w[i]);
for(R II i=1,x;i<=n;i++)
{
of(x);
if(!x) continue ;
add(x,i); in[i]++;
}

for(R II i=1;i<=n;i++) if(!DFN[i]) Tarjan(i);
for(R II i=1;i<=_tot;i++) edge[i]=aa[i];
for(R II i=1;i<=n;i++) kl[i]=head[i], head[i]=0;
for(R II i=1;i<=n;i++) had[i][i]=1, in[i]=0;
_tot=0;

for(R II x=1;x<=n;x++)
{
for(R II i=kl[x],go;i;i=edge[i].up)
{
go=edge[i].to;
if(!had[belong[x]][belong[go]]) {
add(belong[x],belong[go]);
had[belong[x]][belong[go]]=1;
in[belong[go]]++;
}
}
}

for(R II i=1;i<=bit;i++) if(!in[i]) add(0,i);
dfs(0);
printf("%d\n",f[0][V]);
}


#### P`eipei

Why Be King When Can Be God.

tql%%%

Orz

QvQ