n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))


def solve(start):
    loop = []
    llen = 0
    lsum = 0
    reach = [0]*n

    now = start
    while True:
        nxt = P[now]-1
        loop.append(now)
        llen += 1
        lsum += C[now]
        reach[now] = 1
        if reach[nxt]:
            break
        now = nxt
    
    if lsum <= 0 or llen >= k:
        res = -10 ** 20
        s = 0
        now = start
        for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
        return res
    else:
        s = lsum * (k // llen)
        now = start
        for i in range(k % llen):
            s += C[now]
            now = P[now]-1
        j = loop.index(now)
        res = -10**20
        for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
        return res


def solver():
    ans = -10 ** 20
    for i in range(n):
        ans = max(ans, solve(i))
    return ans


print(solver()) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k P = list(map(int, input().split())) P list(map(int, input().split())) list map(int, input().split()) map int 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 def solve(start):
    loop = []
    llen = 0
    lsum = 0
    reach = [0]*n

    now = start
    while True:
        nxt = P[now]-1
        loop.append(now)
        llen += 1
        lsum += C[now]
        reach[now] = 1
        if reach[nxt]:
            break
        now = nxt
    
    if lsum <= 0 or llen >= k:
        res = -10 ** 20
        s = 0
        now = start
        for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
        return res
    else:
        s = lsum * (k // llen)
        now = start
        for i in range(k % llen):
            s += C[now]
            now = P[now]-1
        j = loop.index(now)
        res = -10**20
        for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
        return res solve loop = [] loop [] llen = 0 llen 0 lsum = 0 lsum 0 reach = [0]*n reach [0]*n [0] 0 n now = start now start while True:
        nxt = P[now]-1
        loop.append(now)
        llen += 1
        lsum += C[now]
        reach[now] = 1
        if reach[nxt]:
            break
        now = nxt
    
     True nxt = P[now]-1 nxt P[now]-1 P[now] P now 1 loop.append(now) loop.append loop append now llen += 1 llen 1 lsum += C[now] lsum C[now] C now reach[now] = 1 reach[now] reach now 1 if reach[nxt]:
            break
         reach[nxt] reach nxt break now = nxt now nxt if lsum <= 0 or llen >= k:
        res = -10 ** 20
        s = 0
        now = start
        for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
        return res
    else:
        s = lsum * (k // llen)
        now = start
        for i in range(k % llen):
            s += C[now]
            now = P[now]-1
        j = loop.index(now)
        res = -10**20
        for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
        return res lsum <= 0 or llen >= k lsum <= 0 lsum 0 llen >= k llen k res = -10 ** 20 res -10 ** 20 -10 20 s = 0 s 0 now = start now start for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
         i range(min(llen, k)) range min(llen, k) min llen k s += C[now] s C[now] C now res = max(res, s) res max(res, s) max res s now = P[now]-1 now P[now]-1 P[now] P now 1 return res res s = lsum * (k // llen) s lsum * (k // llen) lsum k // llen k llen now = start now start for i in range(k % llen):
            s += C[now]
            now = P[now]-1
         i range(k % llen) range k % llen k llen s += C[now] s C[now] C now now = P[now]-1 now P[now]-1 P[now] P now 1 j = loop.index(now) j loop.index(now) loop.index loop index now res = -10**20 res -10**20 -10 20 for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
         i range(llen) range llen j -= 1 j 1 res = max(res, s) res max(res, s) max res s s -= C[loop[j]] s C[loop[j]] C loop[j] loop j return res res start start def solver():
    ans = -10 ** 20
    for i in range(n):
        ans = max(ans, solve(i))
    return ans solver ans = -10 ** 20 ans -10 ** 20 -10 20 for i in range(n):
        ans = max(ans, solve(i))
     i range(n) range n ans = max(ans, solve(i)) ans max(ans, solve(i)) max ans solve(i) solve i return ans ans print(solver()) print solver() solver k = map(int, input().split()) map(int, input().split()) k def solve(start):
    loop = []
    llen = 0
    lsum = 0
    reach = [0]*n

    now = start
    while True:
        nxt = P[now]-1
        loop.append(now)
        llen += 1
        lsum += C[now]
        reach[now] = 1
        if reach[nxt]:
            break
        now = nxt
    
    if lsum <= 0 or llen >= k:
        res = -10 ** 20
        s = 0
        now = start
        for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
        return res
    else:
        s = lsum * (k // llen)
        now = start
        for i in range(k % llen):
            s += C[now]
            now = P[now]-1
        j = loop.index(now)
        res = -10**20
        for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
        return res def solve(start):
    loop = []
    llen = 0
    lsum = 0
    reach = [0]*n

    now = start
    while True:
        nxt = P[now]-1
        loop.append(now)
        llen += 1
        lsum += C[now]
        reach[now] = 1
        if reach[nxt]:
            break
        now = nxt
    
    if lsum <= 0 or llen >= k:
        res = -10 ** 20
        s = 0
        now = start
        for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
        return res
    else:
        s = lsum * (k // llen)
        now = start
        for i in range(k % llen):
            s += C[now]
            now = P[now]-1
        j = loop.index(now)
        res = -10**20
        for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
        return res solve C = list(map(int, input().split())) list(map(int, input().split())) C n, k = map(int, input().split()) map(int, input().split()) n P = list(map(int, input().split())) list(map(int, input().split())) P def solver():
    ans = -10 ** 20
    for i in range(n):
        ans = max(ans, solve(i))
    return ans def solver():
    ans = -10 ** 20
    for i in range(n):
        ans = max(ans, solve(i))
    return ans solver
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))


def solve(start):
    loop = []
    llen = 0
    lsum = 0
    reach = [0]*n

    now = start
    while True:
        nxt = P[now]-1
        loop.append(now)
        llen += 1
        lsum += C[now]
        reach[now] = 1
        if reach[nxt]:
            break
        now = nxt
    
    if lsum <= 0 or llen >= k:
        res = -10 ** 20
        s = 0
        now = start
        for i in range(min(llen, k)):
            s += C[now]
            res = max(res, s)
            now = P[now]-1
        return res
    else:
        s = lsum * (k // llen)
        now = start
        for i in range(k % llen):
            s += C[now]
            now = P[now]-1
        j = loop.index(now)
        res = -10**20
        for i in range(llen):
            j -= 1
            res = max(res, s)
            s -= C[loop[j]]
        return res


def solver():
    ans = -10 ** 20
    for i in range(n):
        ans = max(ans, solve(i))
    return ans


print(solver())
