import sys
input = sys.stdin.readline

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

js = [None for _ in range(n)] # 周期とスコア増分

for s in range(n):
    if js[s]:
        continue
    m = s
    tmp = 0
    memo = [[] for _ in range(n)]
    i = 0
    while True:
        m = p[m] - 1
        tmp += c[m]
        memo[m].append((i, tmp))
        if len(memo[m]) > 1:
            js[m] = (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1])
        if len(memo[m]) > 2:
            break
        i += 1

ans = -100000000000
for s in range(n):
    m = s
    tmp = 0
    visited = [0] * n
    for i in range(k):
        m = p[m] - 1
        if visited[m]:
            break
        visited[m] = 1
        tmp += c[m]
        if js[m] and js[m][1] > 0:
            ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0]))
        else:
            ans = max(ans, tmp)
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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 None for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) None js = [None for _ in range(n)] js [None for _ in range(n)] for s in range(n):
    if js[s]:
        continue
    m = s
    tmp = 0
    memo = [[] for _ in range(n)]
    i = 0
    while True:
        m = p[m] - 1
        tmp += c[m]
        memo[m].append((i, tmp))
        if len(memo[m]) > 1:
            js[m] = (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1])
        if len(memo[m]) > 2:
            break
        i += 1 s range(n) range n if js[s]:
        continue
     js[s] js s continue m = s m s tmp = 0 tmp 0 [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] memo = [[] for _ in range(n)] memo [[] for _ in range(n)] i = 0 i 0 while True:
        m = p[m] - 1
        tmp += c[m]
        memo[m].append((i, tmp))
        if len(memo[m]) > 1:
            js[m] = (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1])
        if len(memo[m]) > 2:
            break
        i += 1 True m = p[m] - 1 m p[m] - 1 p[m] p m 1 tmp += c[m] tmp c[m] c m memo[m].append((i, tmp)) [m].append [m] memo m append (i, tmp) i tmp if len(memo[m]) > 1:
            js[m] = (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1])
         len(memo[m]) > 1 len(memo[m]) len memo[m] memo m 1 js[m] = (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1]) js[m] js m (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1]) memo[m][1][0] - memo[m][0][0] memo[m][1][0] [1] [m] memo m 1 0 memo[m][0][0] [0] [m] memo m 0 0 memo[m][1][1] - memo[m][0][1] memo[m][1][1] [1] [m] memo m 1 1 memo[m][0][1] [0] [m] memo m 0 1 if len(memo[m]) > 2:
            break
         len(memo[m]) > 2 len(memo[m]) len memo[m] memo m 2 break i += 1 i 1 ans = -100000000000 ans -100000000000 for s in range(n):
    m = s
    tmp = 0
    visited = [0] * n
    for i in range(k):
        m = p[m] - 1
        if visited[m]:
            break
        visited[m] = 1
        tmp += c[m]
        if js[m] and js[m][1] > 0:
            ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0]))
        else:
            ans = max(ans, tmp) s range(n) range n m = s m s tmp = 0 tmp 0 visited = [0] * n visited [0] * n [0] 0 n for i in range(k):
        m = p[m] - 1
        if visited[m]:
            break
        visited[m] = 1
        tmp += c[m]
        if js[m] and js[m][1] > 0:
            ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0]))
        else:
            ans = max(ans, tmp) i range(k) range k m = p[m] - 1 m p[m] - 1 p[m] p m 1 if visited[m]:
            break
         visited[m] visited m break visited[m] = 1 visited[m] visited m 1 tmp += c[m] tmp c[m] c m if js[m] and js[m][1] > 0:
            ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0]))
        else:
            ans = max(ans, tmp) js[m] and js[m][1] > 0 js[m] js m js[m][1] > 0 js[m][1] [m] js m 1 0 ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0])) ans max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0])) max ans tmp + js[m][1] * ((k - i - 1) // js[m][0]) tmp js[m][1] * ((k - i - 1) // js[m][0]) js[m][1] [m] js m 1 (k - i - 1) // js[m][0] k - i - 1 k - i k i 1 js[m][0] [m] js m 0 ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans p = list(map(int, input().split())) list(map(int, input().split())) p m = p[m] - 1 p[m] - 1 m m = s s m m = p[m] - 1 p[m] - 1 m js = [None for _ in range(n)] [None for _ in range(n)] js tmp += c[m] c[m] tmp tmp = 0 0 tmp ans = -100000000000 -100000000000 ans k = map(int, input().split()) map(int, input().split()) k n, k = map(int, input().split()) map(int, input().split()) n c = list(map(int, input().split())) list(map(int, input().split())) c m = s s m i += 1 1 i tmp += c[m] c[m] tmp visited = [0] * n [0] * n visited tmp = 0 0 tmp memo = [[] for _ in range(n)] [[] for _ in range(n)] memo i = 0 0 i ans = max(ans, tmp) max(ans, tmp) ans input = sys.stdin.readline sys.stdin.readline input ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0])) max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0])) ans
import sys
input = sys.stdin.readline

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

js = [None for _ in range(n)] # 周期とスコア増分

for s in range(n):
    if js[s]:
        continue
    m = s
    tmp = 0
    memo = [[] for _ in range(n)]
    i = 0
    while True:
        m = p[m] - 1
        tmp += c[m]
        memo[m].append((i, tmp))
        if len(memo[m]) > 1:
            js[m] = (memo[m][1][0] - memo[m][0][0], memo[m][1][1] - memo[m][0][1])
        if len(memo[m]) > 2:
            break
        i += 1

ans = -100000000000
for s in range(n):
    m = s
    tmp = 0
    visited = [0] * n
    for i in range(k):
        m = p[m] - 1
        if visited[m]:
            break
        visited[m] = 1
        tmp += c[m]
        if js[m] and js[m][1] > 0:
            ans = max(ans, tmp + js[m][1] * ((k - i - 1) // js[m][0]))
        else:
            ans = max(ans, tmp)
print(ans)
