import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int, input().split())
    P = list(map(lambda x : int(x) - 1, input().split()))
    C = list(map(int, input().split()))

    # doubling table
    nexts = [P]
    vals = [C]
    totals = [C]
    for _ in range(60):
        next, val, total = nexts[-1], vals[-1], totals[-1]
        nnext, nval, ntotal = [0] * n, [0] * n, [0] * n
        for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
        nexts.append(nnext)
        vals.append(nval)
        totals.append(ntotal)

    # answer
    ans = -INF
    for now in range(n):
        score = 0
        for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
    print(ans)
resolve() import sys sys INF = 1 << 60 INF 1 << 60 1 60 MOD = 10**9 + 7 MOD 10**9 + 7 10**9 10 9 7 sys.setrecursionlimit(2147483647) sys.setrecursionlimit sys setrecursionlimit 2147483647 input = lambda:sys.stdin.readline().rstrip() input lambda:sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip def resolve():
    n, k = map(int, input().split())
    P = list(map(lambda x : int(x) - 1, input().split()))
    C = list(map(int, input().split()))

    # doubling table
    nexts = [P]
    vals = [C]
    totals = [C]
    for _ in range(60):
        next, val, total = nexts[-1], vals[-1], totals[-1]
        nnext, nval, ntotal = [0] * n, [0] * n, [0] * n
        for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
        nexts.append(nnext)
        vals.append(nval)
        totals.append(ntotal)

    # answer
    ans = -INF
    for now in range(n):
        score = 0
        for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
    print(ans) resolve n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k P = list(map(lambda x : int(x) - 1, input().split())) P list(map(lambda x : int(x) - 1, input().split())) list map(lambda x : int(x) - 1, input().split()) map lambda x : int(x) - 1 int(x) - 1 int(x) int x 1 x input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split nexts = [P] nexts [P] P vals = [C] vals [C] C totals = [C] totals [C] C for _ in range(60):
        next, val, total = nexts[-1], vals[-1], totals[-1]
        nnext, nval, ntotal = [0] * n, [0] * n, [0] * n
        for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
        nexts.append(nnext)
        vals.append(nval)
        totals.append(ntotal)

    # answer
     _ range(60) range 60 next, val, total = nexts[-1], vals[-1], totals[-1] next nexts[-1] nexts -1 val vals[-1] vals -1 total totals[-1] totals -1 nnext, nval, ntotal = [0] * n, [0] * n, [0] * n nnext [0] * n [0] 0 n nval [0] * n [0] 0 n ntotal [0] * n [0] 0 n for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
         i range(n) range n nnext[i] = next[next[i]] nnext[i] nnext i next[next[i]] next next[i] next i nval[i] = val[i] + val[next[i]] nval[i] nval i val[i] + val[next[i]] val[i] val i val[next[i]] val next[i] next i ntotal[i] = max(total[i], val[i] + total[next[i]]) ntotal[i] ntotal i max(total[i], val[i] + total[next[i]]) max total[i] total i val[i] + total[next[i]] val[i] val i total[next[i]] total next[i] next i nexts.append(nnext) nexts.append nexts append nnext vals.append(nval) vals.append vals append nval totals.append(ntotal) totals.append totals append ntotal ans = -INF ans -INF INF for now in range(n):
        score = 0
        for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
     now range(n) range n score = 0 score 0 for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
     d range(59, -1, -1) range 59 -1 -1 if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
     k >> d & 1 k >> d k d 1 ans = max(ans, score + totals[d][now]) ans max(ans, score + totals[d][now]) max ans score + totals[d][now] score totals[d][now] [d] totals d now score += vals[d][now] score vals[d][now] [d] vals d now now = nexts[d][now] now nexts[d][now] [d] nexts d now print(ans) print ans resolve() resolve input = lambda:sys.stdin.readline().rstrip() lambda:sys.stdin.readline().rstrip() input def resolve():
    n, k = map(int, input().split())
    P = list(map(lambda x : int(x) - 1, input().split()))
    C = list(map(int, input().split()))

    # doubling table
    nexts = [P]
    vals = [C]
    totals = [C]
    for _ in range(60):
        next, val, total = nexts[-1], vals[-1], totals[-1]
        nnext, nval, ntotal = [0] * n, [0] * n, [0] * n
        for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
        nexts.append(nnext)
        vals.append(nval)
        totals.append(ntotal)

    # answer
    ans = -INF
    for now in range(n):
        score = 0
        for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
    print(ans) def resolve():
    n, k = map(int, input().split())
    P = list(map(lambda x : int(x) - 1, input().split()))
    C = list(map(int, input().split()))

    # doubling table
    nexts = [P]
    vals = [C]
    totals = [C]
    for _ in range(60):
        next, val, total = nexts[-1], vals[-1], totals[-1]
        nnext, nval, ntotal = [0] * n, [0] * n, [0] * n
        for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
        nexts.append(nnext)
        vals.append(nval)
        totals.append(ntotal)

    # answer
    ans = -INF
    for now in range(n):
        score = 0
        for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
    print(ans) resolve INF = 1 << 60 1 << 60 INF MOD = 10**9 + 7 10**9 + 7 MOD
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n, k = map(int, input().split())
    P = list(map(lambda x : int(x) - 1, input().split()))
    C = list(map(int, input().split()))

    # doubling table
    nexts = [P]
    vals = [C]
    totals = [C]
    for _ in range(60):
        next, val, total = nexts[-1], vals[-1], totals[-1]
        nnext, nval, ntotal = [0] * n, [0] * n, [0] * n
        for i in range(n):
            nnext[i] = next[next[i]]
            nval[i] = val[i] + val[next[i]]
            ntotal[i] = max(total[i], val[i] + total[next[i]])
        nexts.append(nnext)
        vals.append(nval)
        totals.append(ntotal)

    # answer
    ans = -INF
    for now in range(n):
        score = 0
        for d in range(59, -1, -1):
            if k >> d & 1:
                ans = max(ans, score + totals[d][now])
                score += vals[d][now]
                now = nexts[d][now]
    print(ans)
resolve()