#include<bits/stdc++.h>
using namespace std;constint M =510;constint N =510;int n, m;int h[N], e[N<<1], ne[N<<1], idx;int v[N<<1], w[N<<1];int f[N][M];int dp[N][M];int dfn[N], low[N];int timestamp;int stk[N], in_stk[N];int top;int scc_cnt;int id[N];int d[N];int ww[N], vv[N];int st[N];voidadd(int a,int b){
e[idx]= b;
ne[idx]= h[a];
h[a]= idx ++;}voidtarjan(int u){
dfn[u]= low[u]=++ timestamp;
stk[++ top]= u, in_stk[u]= true;for(int i = h[u]; i !=-1; i = ne[i]){int j = e[i];if(!dfn[j]){tarjan(j);
low[u]=min(low[u], low[j]);}elseif(in_stk[j]){
low[u]=min(low[u], dfn[j]);}}if(dfn[u]== low[u]){int y;++ scc_cnt;do{
y = stk[top --];
in_stk[y]= false;
id[y]= scc_cnt;
ww[scc_cnt]+= w[y];
vv[scc_cnt]+= v[y];}while(y != u);}}voiddfs(int u){for(int i = h[u]; i !=-1; i = ne[i]){int son = e[i];dfs(son);for(int j = m - vv[u]; j >=0; j --){for(int k =0; k <= j; k ++)
f[u][j]=max(f[u][j], f[u][j-k]+ f[son][k]);}}for(int i = m; i >= vv[u]; i --){
f[u][i]= f[u][i-vv[u]]+ ww[u];}for(int i =0; i < vv[u]; i ++){
f[u][i]=0;}}intmain(){
cin >> n >> m;memset(h,-1,sizeof(h));for(int i =1; i <= n; i ++){
cin >> v[i];}for(int i =1; i <= n; i ++){
cin >> w[i];}for(int i =1; i <= n; i ++){
cin >> d[i];if(d[i]!=0){add(d[i], i);}}for(int i =1; i <= n; i ++){if(!dfn[i])tarjan(i);}memset(h,-1,sizeof(h));int idx =0;for(int i =1; i <= n; i ++){if(id[i]!= id[d[i]])//对于单个孤立的点来所就是id!=0直接建立一个0->id的边{add(id[d[i]], id[i]);
st[id[i]]=1;//建立了一个id[d[i]]->id[i]的边说明id[i]有父亲节点了}}for(int i =1; i <= scc_cnt; i ++){if(!st[i]){add(0, i);}}dfs(0);
cout<< f[0][m]<< endl;return0;}