# string
# a, b, c = input().split()
# str_list = list(input().split())

# number
# a, b, c = map(int, input().split())
# num_list = list(map(int, input().split()))

# lows
# str_list = [input() for _ in range(n)]

# many inputs
# num_list = []
# for i in range(n): num_list.append(list(map(int, input().split())))

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

for i in range(n):
    p[i] -= 1

ans = int(-1e14)
for i in range(n):
    tmp = []
    cycle = 0
    index = i
    while True:
        index = p[index]
        tmp.append(c[index])
        cycle += c[index]
        if i == index:
            break
    
    l = len(tmp)
    t = 0
    for j in range(l):
        t += tmp[j]
        if j >= k:
            break
        cur = t
        if cycle > 0:
            e = int((k-j-1) / l)
            cur += e * cycle
        ans = max(cur, ans)
        
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 c = list(map(int, input().split())) c 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 ans = int(-1e14) ans int(-1e14) int -1e14 for i in range(n):
    tmp = []
    cycle = 0
    index = i
    while True:
        index = p[index]
        tmp.append(c[index])
        cycle += c[index]
        if i == index:
            break
    
    l = len(tmp)
    t = 0
    for j in range(l):
        t += tmp[j]
        if j >= k:
            break
        cur = t
        if cycle > 0:
            e = int((k-j-1) / l)
            cur += e * cycle
        ans = max(cur, ans)
         i range(n) range n tmp = [] tmp [] cycle = 0 cycle 0 index = i index i while True:
        index = p[index]
        tmp.append(c[index])
        cycle += c[index]
        if i == index:
            break
    
     True index = p[index] index p[index] p index tmp.append(c[index]) tmp.append tmp append c[index] c index cycle += c[index] cycle c[index] c index if i == index:
            break
    
     i == index i index break l = len(tmp) l len(tmp) len tmp t = 0 t 0 for j in range(l):
        t += tmp[j]
        if j >= k:
            break
        cur = t
        if cycle > 0:
            e = int((k-j-1) / l)
            cur += e * cycle
        ans = max(cur, ans)
         j range(l) range l t += tmp[j] t tmp[j] tmp j if j >= k:
            break
         j >= k j k break cur = t cur t if cycle > 0:
            e = int((k-j-1) / l)
            cur += e * cycle
         cycle > 0 cycle 0 e = int((k-j-1) / l) e int((k-j-1) / l) int (k-j-1) / l k-j-1 k-j k j 1 l cur += e * cycle cur e * cycle e cycle ans = max(cur, ans) ans max(cur, ans) max cur ans print(ans) print ans e = int((k-j-1) / l) int((k-j-1) / l) e cycle = 0 0 cycle ans = max(cur, ans) max(cur, ans) ans t += tmp[j] tmp[j] t ans = int(-1e14) int(-1e14) ans n, k = map(int, input().split()) map(int, input().split()) n t = 0 0 t index = i i index l = len(tmp) len(tmp) l cur = t t cur c = list(map(int, input().split())) list(map(int, input().split())) c p = list(map(int, input().split())) list(map(int, input().split())) p cycle += c[index] c[index] cycle cur += e * cycle e * cycle cur k = map(int, input().split()) map(int, input().split()) k tmp = [] [] tmp index = p[index] p[index] index
# string
# a, b, c = input().split()
# str_list = list(input().split())

# number
# a, b, c = map(int, input().split())
# num_list = list(map(int, input().split()))

# lows
# str_list = [input() for _ in range(n)]

# many inputs
# num_list = []
# for i in range(n): num_list.append(list(map(int, input().split())))

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

for i in range(n):
    p[i] -= 1

ans = int(-1e14)
for i in range(n):
    tmp = []
    cycle = 0
    index = i
    while True:
        index = p[index]
        tmp.append(c[index])
        cycle += c[index]
        if i == index:
            break
    
    l = len(tmp)
    t = 0
    for j in range(l):
        t += tmp[j]
        if j >= k:
            break
        cur = t
        if cycle > 0:
            e = int((k-j-1) / l)
            cur += e * cycle
        ans = max(cur, ans)
        
print(ans)

