#include<cstdio>
#include<vector>
using namespace std;
#define INF 1000000000
#define N 100010
#define M 110
int w[N], si[N], loli[N], dp[N][M];
vector<int> g[N];
int dfs(int u, int p=0){
static int cnt;
if(!p){
cnt = 0;
}
int size = 1;
for(int v: g[u]){
if(v==p) continue;
size += dfs(v, u);
}
si[++cnt] = u;
loli[cnt] = cnt-size;
return size;
}
int main(){
int n, K;
while(scanf("%d%d", &n, &K), n){
for(int i=1; i<=n; i++){
scanf("%d", w+i);
}
for(int i=2; i<=n; i++){
int v;
scanf("%d", &v); v++;
g[i].push_back(v);
g[v].push_back(i);
}
dfs(1);
dp[0][0] = 0;
fill(dp[0]+1, dp[0]+K+1, -INF);
for(int i=1; i<=n; i++){
dp[i][0] = 0;
for(int j=1; j<=K; j++){
dp[i][j] = max(dp[i-1][j], w[si[i]]+dp[loli[i]][j-1]);
}
}
int ans = dp[n][1];
for(int i=2; i<=K; i++){
ans = max(ans, dp[n][i]);
}
printf("%d\n", ans);
for(int i=1; i<=n; i++){
g[i].clear();
}
}
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTx2ZWN0b3I+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgSU5GIDEwMDAwMDAwMDAKCiNkZWZpbmUgTiAxMDAwMTAKI2RlZmluZSBNIDExMAppbnQgd1tOXSwgc2lbTl0sIGxvbGlbTl0sIGRwW05dW01dOwp2ZWN0b3I8aW50PiBnW05dOwoKaW50IGRmcyhpbnQgdSwgaW50IHA9MCl7CiAgICBzdGF0aWMgaW50IGNudDsKICAgIGlmKCFwKXsKICAgICAgICBjbnQgPSAwOwogICAgfQogICAgaW50IHNpemUgPSAxOwogICAgZm9yKGludCB2OiBnW3VdKXsKICAgICAgICBpZih2PT1wKSBjb250aW51ZTsKICAgICAgICBzaXplICs9IGRmcyh2LCB1KTsKICAgIH0KICAgIHNpWysrY250XSA9IHU7CiAgICBsb2xpW2NudF0gPSBjbnQtc2l6ZTsKICAgIHJldHVybiBzaXplOwp9CgppbnQgbWFpbigpewogICAgaW50IG4sIEs7CiAgICB3aGlsZShzY2FuZigiJWQlZCIsICZuLCAmSyksIG4pewogICAgICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspewogICAgICAgICAgICBzY2FuZigiJWQiLCB3K2kpOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGk9MjsgaTw9bjsgaSsrKXsKICAgICAgICAgICAgaW50IHY7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsICZ2KTsgdisrOwogICAgICAgICAgICBnW2ldLnB1c2hfYmFjayh2KTsKICAgICAgICAgICAgZ1t2XS5wdXNoX2JhY2soaSk7CiAgICAgICAgfQogICAgICAgIGRmcygxKTsKICAgICAgICBkcFswXVswXSA9IDA7CiAgICAgICAgZmlsbChkcFswXSsxLCBkcFswXStLKzEsIC1JTkYpOwogICAgICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspewogICAgICAgICAgICBkcFtpXVswXSA9IDA7CiAgICAgICAgICAgIGZvcihpbnQgaj0xOyBqPD1LOyBqKyspewogICAgICAgICAgICAgICAgZHBbaV1bal0gPSBtYXgoZHBbaS0xXVtqXSwgd1tzaVtpXV0rZHBbbG9saVtpXV1bai0xXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW50IGFucyA9IGRwW25dWzFdOwogICAgICAgIGZvcihpbnQgaT0yOyBpPD1LOyBpKyspewogICAgICAgICAgICBhbnMgPSBtYXgoYW5zLCBkcFtuXVtpXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiJWRcbiIsIGFucyk7CiAgICAgICAgZm9yKGludCBpPTE7IGk8PW47IGkrKyl7CiAgICAgICAgICAgIGdbaV0uY2xlYXIoKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gMDsKfQo=
NyAzCjYgMyA0IC0xIDEgLTUgMQowIDEgMSAwIDAgNQoyIDEKLTEgMQowCjMgMwoxIDIgMwowIDAKOCA4CjEgMiAzIDQgNSA2IDcgOAowIDEgMiAzIDQgNSA2CjQgNAotMjcgLTQ1IC02NyAtOTgKMiAwIDIKMCAwCg==
7 3
6 3 4 -1 1 -5 1
0 1 1 0 0 5
2 1
-1 1
0
3 3
1 2 3
0 0
8 8
1 2 3 4 5 6 7 8
0 1 2 3 4 5 6
4 4
-27 -45 -67 -98
2 0 2
0 0