n, k = map(int, input().split())
p = list(map(int, input().split(" ")))
for i in range(n):
    p[i] -= 1
c = list(map(int, input().split(" ")))
ans = -10000000000
for start in range(n):
    now = start
    pt = 0
    cycle = 0
    looplen = 0

    while 1:
        now = p[now]
        pt += c[now]
        looplen += 1
        if now == start:
            break

    cycle = pt
    pt = 0
    for i in range(looplen):
        now = p[now]
        pt += c[now]
        aftermove = k - i-1
        if aftermove < 0:
            continue

        loop = aftermove//looplen
        ans = max(ans, pt)
        ans = max(ans, pt+loop*cycle)
print(ans) 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 " " for i in range(n):
    p[i] -= 1 i range(n) range n p[i] -= 1 p[i] p i 1 c = list(map(int, input().split(" "))) c list(map(int, input().split(" "))) list map(int, input().split(" ")) map int input().split(" ") ().split () input split " " ans = -10000000000 ans -10000000000 for start in range(n):
    now = start
    pt = 0
    cycle = 0
    looplen = 0

    while 1:
        now = p[now]
        pt += c[now]
        looplen += 1
        if now == start:
            break

    cycle = pt
    pt = 0
    for i in range(looplen):
        now = p[now]
        pt += c[now]
        aftermove = k - i-1
        if aftermove < 0:
            continue

        loop = aftermove//looplen
        ans = max(ans, pt)
        ans = max(ans, pt+loop*cycle) start range(n) range n now = start now start pt = 0 pt 0 cycle = 0 cycle 0 looplen = 0 looplen 0 while 1:
        now = p[now]
        pt += c[now]
        looplen += 1
        if now == start:
            break

     1 now = p[now] now p[now] p now pt += c[now] pt c[now] c now looplen += 1 looplen 1 if now == start:
            break

     now == start now start break cycle = pt cycle pt pt = 0 pt 0 for i in range(looplen):
        now = p[now]
        pt += c[now]
        aftermove = k - i-1
        if aftermove < 0:
            continue

        loop = aftermove//looplen
        ans = max(ans, pt)
        ans = max(ans, pt+loop*cycle) i range(looplen) range looplen now = p[now] now p[now] p now pt += c[now] pt c[now] c now aftermove = k - i-1 aftermove k - i-1 k - i k i 1 if aftermove < 0:
            continue

         aftermove < 0 aftermove 0 continue loop = aftermove//looplen loop aftermove//looplen aftermove looplen ans = max(ans, pt) ans max(ans, pt) max ans pt ans = max(ans, pt+loop*cycle) ans max(ans, pt+loop*cycle) max ans pt+loop*cycle pt loop*cycle loop cycle print(ans) print ans pt += c[now] c[now] pt n, k = map(int, input().split()) map(int, input().split()) n pt = 0 0 pt now = start start now k = map(int, input().split()) map(int, input().split()) k c = list(map(int, input().split(" "))) list(map(int, input().split(" "))) c ans = -10000000000 -10000000000 ans looplen = 0 0 looplen p = list(map(int, input().split(" "))) list(map(int, input().split(" "))) p looplen += 1 1 looplen pt = 0 0 pt cycle = 0 0 cycle pt += c[now] c[now] pt now = p[now] p[now] now now = p[now] p[now] now ans = max(ans, pt) max(ans, pt) ans loop = aftermove//looplen aftermove//looplen loop cycle = pt pt cycle aftermove = k - i-1 k - i-1 aftermove ans = max(ans, pt+loop*cycle) max(ans, pt+loop*cycle) ans
n, k = map(int, input().split())
p = list(map(int, input().split(" ")))
for i in range(n):
    p[i] -= 1
c = list(map(int, input().split(" ")))
ans = -10000000000
for start in range(n):
    now = start
    pt = 0
    cycle = 0
    looplen = 0

    while 1:
        now = p[now]
        pt += c[now]
        looplen += 1
        if now == start:
            break

    cycle = pt
    pt = 0
    for i in range(looplen):
        now = p[now]
        pt += c[now]
        aftermove = k - i-1
        if aftermove < 0:
            continue

        loop = aftermove//looplen
        ans = max(ans, pt)
        ans = max(ans, pt+loop*cycle)
print(ans)
