N,K = map(int,input().split())
P = list(map(lambda x:int(x)-1,input().split()))
C = list(map(int,input().split()))

ans = -float('inf')
for i in range(N):
    visited = set()
    tmp = 0
    for k in range(K):
        if P[i] in visited: break
        tmp += C[P[i]]
        i = P[i]
        visited.add(i)
        ans = max(ans, tmp)
    if tmp < 0: continue
    l = len(visited)
    rem = K-l
    d,m = divmod(rem,l)
    if m==0 and d > 0:
        m = l
        d -= 1
    tmp *= (d+1)
    for k in range(m):
        tmp += C[P[i]]
        i = P[i]
        ans = max(ans, tmp)

print(ans) 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 ans = -float('inf') ans -float('inf') float('inf') float 'inf' for i in range(N):
    visited = set()
    tmp = 0
    for k in range(K):
        if P[i] in visited: break
        tmp += C[P[i]]
        i = P[i]
        visited.add(i)
        ans = max(ans, tmp)
    if tmp < 0: continue
    l = len(visited)
    rem = K-l
    d,m = divmod(rem,l)
    if m==0 and d > 0:
        m = l
        d -= 1
    tmp *= (d+1)
    for k in range(m):
        tmp += C[P[i]]
        i = P[i]
        ans = max(ans, tmp) i range(N) range N visited = set() visited set() set tmp = 0 tmp 0 for k in range(K):
        if P[i] in visited: break
        tmp += C[P[i]]
        i = P[i]
        visited.add(i)
        ans = max(ans, tmp)
     k range(K) range K if P[i] in visited: break
         P[i] in visited P[i] P i visited tmp += C[P[i]] tmp C[P[i]] C P[i] P i i = P[i] i P[i] P i visited.add(i) visited.add visited add i ans = max(ans, tmp) ans max(ans, tmp) max ans tmp if tmp < 0: continue
     tmp < 0 tmp 0 l = len(visited) l len(visited) len visited rem = K-l rem K-l K l d,m = divmod(rem,l) d divmod(rem,l) divmod rem l m if m==0 and d > 0:
        m = l
        d -= 1
     m==0 and d > 0 m==0 m 0 d > 0 d 0 m = l m l d -= 1 d 1 tmp *= (d+1) tmp d+1 d 1 for k in range(m):
        tmp += C[P[i]]
        i = P[i]
        ans = max(ans, tmp) k range(m) range m tmp += C[P[i]] tmp C[P[i]] C P[i] P i i = P[i] i P[i] P i ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans m = l l m i = P[i] P[i] i P = list(map(lambda x:int(x)-1,input().split())) list(map(lambda x:int(x)-1,input().split())) P m = divmod(rem,l) divmod(rem,l) m tmp *= (d+1) d+1 tmp C = list(map(int,input().split())) list(map(int,input().split())) C tmp += C[P[i]] C[P[i]] tmp tmp = 0 0 tmp d,m = divmod(rem,l) divmod(rem,l) d ans = max(ans, tmp) max(ans, tmp) ans i = P[i] P[i] i ans = -float('inf') -float('inf') ans K = map(int,input().split()) map(int,input().split()) K rem = K-l K-l rem N,K = map(int,input().split()) map(int,input().split()) N l = len(visited) len(visited) l tmp += C[P[i]] C[P[i]] tmp ans = max(ans, tmp) max(ans, tmp) ans visited = set() set() visited d -= 1 1 d
N,K = map(int,input().split())
P = list(map(lambda x:int(x)-1,input().split()))
C = list(map(int,input().split()))

ans = -float('inf')
for i in range(N):
    visited = set()
    tmp = 0
    for k in range(K):
        if P[i] in visited: break
        tmp += C[P[i]]
        i = P[i]
        visited.add(i)
        ans = max(ans, tmp)
    if tmp < 0: continue
    l = len(visited)
    rem = K-l
    d,m = divmod(rem,l)
    if m==0 and d > 0:
        m = l
        d -= 1
    tmp *= (d+1)
    for k in range(m):
        tmp += C[P[i]]
        i = P[i]
        ans = max(ans, tmp)

print(ans)