import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n, k = nm()
    p = nl()
    _c = nl()
    c = [0]*n
    for i in range(n):
        p[i] -= 1
        c[i] = _c[p[i]]
    m = 31
    MIN = - (1 << 63)
    vertex = list()
    score = list()
    vertex.append(p)
    score.append(c)
    for a in range(1, m+1):
        p_ath = [0] * n
        c_ath = [0] * n
        for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
        vertex.append(p_ath)
        score.append(c_ath)
    
    prv = [[MIN, 0] for _ in range(n)]
    nxt = [[MIN, MIN] for _ in range(n)] 
    for b in range(m, -1, -1):
        for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
        prv, nxt = nxt, prv
    ans = max(max(x) for x in prv)
    if ans == 0:
        ans = max(c)
    print(ans)    
    return

solve() import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline readall = sys.stdin.read readall sys.stdin.read sys.stdin sys stdin read ns = lambda: readline().rstrip() ns lambda: readline().rstrip() readline().rstrip() ().rstrip () readline rstrip ni = lambda: int(readline().rstrip()) ni lambda: int(readline().rstrip()) int(readline().rstrip()) int readline().rstrip() ().rstrip () readline rstrip nm = lambda: map(int, readline().split()) nm lambda: map(int, readline().split()) map(int, readline().split()) map int readline().split() ().split () readline split nl = lambda: list(map(int, readline().split())) nl lambda: list(map(int, readline().split())) list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split prn = lambda x: print(*x, sep='\n') prn lambda x: print(*x, sep='\n') print(*x, sep='\n') print *x x sep='\n' sep '\n' x def solve():
    n, k = nm()
    p = nl()
    _c = nl()
    c = [0]*n
    for i in range(n):
        p[i] -= 1
        c[i] = _c[p[i]]
    m = 31
    MIN = - (1 << 63)
    vertex = list()
    score = list()
    vertex.append(p)
    score.append(c)
    for a in range(1, m+1):
        p_ath = [0] * n
        c_ath = [0] * n
        for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
        vertex.append(p_ath)
        score.append(c_ath)
    
    prv = [[MIN, 0] for _ in range(n)]
    nxt = [[MIN, MIN] for _ in range(n)] 
    for b in range(m, -1, -1):
        for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
        prv, nxt = nxt, prv
    ans = max(max(x) for x in prv)
    if ans == 0:
        ans = max(c)
    print(ans)    
    return solve n, k = nm() n nm() nm k p = nl() p nl() nl _c = nl() _c nl() nl c = [0]*n c [0]*n [0] 0 n for i in range(n):
        p[i] -= 1
        c[i] = _c[p[i]]
     i range(n) range n p[i] -= 1 p[i] p i 1 c[i] = _c[p[i]] c[i] c i _c[p[i]] _c p[i] p i m = 31 m 31 MIN = - (1 << 63) MIN - (1 << 63) 1 << 63 1 63 vertex = list() vertex list() list score = list() score list() list vertex.append(p) vertex.append vertex append p score.append(c) score.append score append c for a in range(1, m+1):
        p_ath = [0] * n
        c_ath = [0] * n
        for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
        vertex.append(p_ath)
        score.append(c_ath)
    
     a range(1, m+1) range 1 m+1 m 1 p_ath = [0] * n p_ath [0] * n [0] 0 n c_ath = [0] * n c_ath [0] * n [0] 0 n for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
         i range(n) range n p_ath[i] = vertex[a-1][vertex[a-1][i]] p_ath[i] p_ath i vertex[a-1][vertex[a-1][i]] [a-1] vertex a-1 a 1 vertex[a-1][i] [a-1] vertex a-1 a 1 i c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]] c_ath[i] c_ath i score[a-1][i] + score[a-1][vertex[a-1][i]] score[a-1][i] [a-1] score a-1 a 1 i score[a-1][vertex[a-1][i]] [a-1] score a-1 a 1 vertex[a-1][i] [a-1] vertex a-1 a 1 i vertex.append(p_ath) vertex.append vertex append p_ath score.append(c_ath) score.append score append c_ath [MIN, 0] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [MIN, 0] MIN 0 prv = [[MIN, 0] for _ in range(n)] prv [[MIN, 0] for _ in range(n)] [MIN, MIN] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [MIN, MIN] MIN MIN nxt = [[MIN, MIN] for _ in range(n)] nxt [[MIN, MIN] for _ in range(n)] for b in range(m, -1, -1):
        for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
        prv, nxt = nxt, prv
     b range(m, -1, -1) range m -1 -1 for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
         i range(n) range n if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
         (k >> b) & 1 k >> b k b 1 nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) max nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 prv[i][0] + score[b][i] prv[i][0] [i] prv i 0 score[b][i] [b] score b i nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i]) nxt[vertex[b][i]][1] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 1 max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i]) max nxt[vertex[b][i]][1] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 1 prv[i][1] + score[b][i] prv[i][1] [i] prv i 1 score[b][i] [b] score b i nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1]) nxt[i][0] [i] nxt i 0 max(nxt[i][0], prv[i][0], prv[i][1]) max nxt[i][0] [i] nxt i 0 prv[i][0] [i] prv i 0 prv[i][1] [i] prv i 1 nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i]) max nxt[vertex[b][i]][0] [vertex[b][i]] nxt vertex[b][i] [b] vertex b i 0 prv[i][0] + score[b][i] prv[i][0] [i] prv i 0 score[b][i] [b] score b i nxt[i][0] = max(nxt[i][0], prv[i][0]) nxt[i][0] [i] nxt i 0 max(nxt[i][0], prv[i][0]) max nxt[i][0] [i] nxt i 0 prv[i][0] [i] prv i 0 nxt[i][1] = max(nxt[i][1], prv[i][1]) nxt[i][1] [i] nxt i 1 max(nxt[i][1], prv[i][1]) max nxt[i][1] [i] nxt i 1 prv[i][1] [i] prv i 1 prv, nxt = nxt, prv prv nxt nxt prv ans = max(max(x) for x in prv) ans max(max(x) for x in prv) max max(x) max x if ans == 0:
        ans = max(c)
     ans == 0 ans 0 ans = max(c) ans max(c) max c print(ans) print ans return solve() solve nl = lambda: list(map(int, readline().split())) lambda: list(map(int, readline().split())) nl nm = lambda: map(int, readline().split()) lambda: map(int, readline().split()) nm ni = lambda: int(readline().rstrip()) lambda: int(readline().rstrip()) ni ns = lambda: readline().rstrip() lambda: readline().rstrip() ns readall = sys.stdin.read sys.stdin.read readall prn = lambda x: print(*x, sep='\n') lambda x: print(*x, sep='\n') prn readline = sys.stdin.readline sys.stdin.readline readline def solve():
    n, k = nm()
    p = nl()
    _c = nl()
    c = [0]*n
    for i in range(n):
        p[i] -= 1
        c[i] = _c[p[i]]
    m = 31
    MIN = - (1 << 63)
    vertex = list()
    score = list()
    vertex.append(p)
    score.append(c)
    for a in range(1, m+1):
        p_ath = [0] * n
        c_ath = [0] * n
        for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
        vertex.append(p_ath)
        score.append(c_ath)
    
    prv = [[MIN, 0] for _ in range(n)]
    nxt = [[MIN, MIN] for _ in range(n)] 
    for b in range(m, -1, -1):
        for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
        prv, nxt = nxt, prv
    ans = max(max(x) for x in prv)
    if ans == 0:
        ans = max(c)
    print(ans)    
    return def solve():
    n, k = nm()
    p = nl()
    _c = nl()
    c = [0]*n
    for i in range(n):
        p[i] -= 1
        c[i] = _c[p[i]]
    m = 31
    MIN = - (1 << 63)
    vertex = list()
    score = list()
    vertex.append(p)
    score.append(c)
    for a in range(1, m+1):
        p_ath = [0] * n
        c_ath = [0] * n
        for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
        vertex.append(p_ath)
        score.append(c_ath)
    
    prv = [[MIN, 0] for _ in range(n)]
    nxt = [[MIN, MIN] for _ in range(n)] 
    for b in range(m, -1, -1):
        for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
        prv, nxt = nxt, prv
    ans = max(max(x) for x in prv)
    if ans == 0:
        ans = max(c)
    print(ans)    
    return solve
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n, k = nm()
    p = nl()
    _c = nl()
    c = [0]*n
    for i in range(n):
        p[i] -= 1
        c[i] = _c[p[i]]
    m = 31
    MIN = - (1 << 63)
    vertex = list()
    score = list()
    vertex.append(p)
    score.append(c)
    for a in range(1, m+1):
        p_ath = [0] * n
        c_ath = [0] * n
        for i in range(n):
            p_ath[i] = vertex[a-1][vertex[a-1][i]]
            c_ath[i] = score[a-1][i] + score[a-1][vertex[a-1][i]]
        vertex.append(p_ath)
        score.append(c_ath)
    
    prv = [[MIN, 0] for _ in range(n)]
    nxt = [[MIN, MIN] for _ in range(n)] 
    for b in range(m, -1, -1):
        for i in range(n):
            if (k >> b) & 1:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[vertex[b][i]][1] = max(nxt[vertex[b][i]][1], prv[i][1] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0], prv[i][1])
            else:
                nxt[vertex[b][i]][0] = max(nxt[vertex[b][i]][0], prv[i][0] + score[b][i])
                nxt[i][0] = max(nxt[i][0], prv[i][0])
                nxt[i][1] = max(nxt[i][1], prv[i][1])
        prv, nxt = nxt, prv
    ans = max(max(x) for x in prv)
    if ans == 0:
        ans = max(c)
    print(ans)    
    return

solve()