n,k = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
g = [[0]*(n) for _ in range(n)]
A = [n]*n
# for i in range(n):
#     tmp = 0
#     idx = i
#     cnt = 0
#     set_ =set()
#     while cnt<n:
#         if C[idx] not in set_:
#             tmp += C[idx]
#             set_.add(C[idx])
#             g[i][cnt] = tmp
#             idx = P[idx]-1
#             cnt += 1
#         else:
#             p = len(set_)
#             A[i] = p
#             break
ans = -float('inf')

for i in range(n):
    S = []
    idx = P[i]-1
    S.append(C[idx])
    while idx != i:
        idx = P[idx]-1
        S.append(S[-1] +C[idx])
    v,w = k//len(S),k%len(S)
    if k<=len(S):
        val = max(S[:k])
    elif S[-1]<=0:
        val = max(S)
    else:
        val1 = S[-1] *(v-1)
        val1 += max(S)
        val2 = S[-1]*v
        if w!=0:
            val2 += max(0,max(S[:w]))
        val = max(val1,val2)
    ans = max(ans,val)
# for i in range(n):
#     v,w = k//A[i],k%A[i]
#     if A[i]<k:
#         if g[i][A[i]-1]<=0:
#             val = max(g[i][:A[i]])
#         else:
#             val1 = (v-1)*g[i][A[i]-1]
#             val1 += max(g[i][:A[i]])
#             val2 = v*g[i][A[i]-1]
#             if w!=0:
#                 val2 += max(0,max(g[i][:w]))
#             val = max(val1,val2)
#     else:
#         val = max(g[i][:k])
#     ans = max(ans,val)
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 [0]*(n) for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [0]*(n) [0] 0 n g = [[0]*(n) for _ in range(n)] g [[0]*(n) for _ in range(n)] A = [n]*n A [n]*n [n] n n ans = -float('inf') ans -float('inf') float('inf') float 'inf' for i in range(n):
    S = []
    idx = P[i]-1
    S.append(C[idx])
    while idx != i:
        idx = P[idx]-1
        S.append(S[-1] +C[idx])
    v,w = k//len(S),k%len(S)
    if k<=len(S):
        val = max(S[:k])
    elif S[-1]<=0:
        val = max(S)
    else:
        val1 = S[-1] *(v-1)
        val1 += max(S)
        val2 = S[-1]*v
        if w!=0:
            val2 += max(0,max(S[:w]))
        val = max(val1,val2)
    ans = max(ans,val)
# for i in range(n):
#     v,w = k//A[i],k%A[i]
#     if A[i]<k:
#         if g[i][A[i]-1]<=0:
#             val = max(g[i][:A[i]])
#         else:
#             val1 = (v-1)*g[i][A[i]-1]
#             val1 += max(g[i][:A[i]])
#             val2 = v*g[i][A[i]-1]
#             if w!=0:
#                 val2 += max(0,max(g[i][:w]))
#             val = max(val1,val2)
#     else:
#         val = max(g[i][:k])
#     ans = max(ans,val) i range(n) range n S = [] S [] idx = P[i]-1 idx P[i]-1 P[i] P i 1 S.append(C[idx]) S.append S append C[idx] C idx while idx != i:
        idx = P[idx]-1
        S.append(S[-1] +C[idx])
     idx != i idx i idx = P[idx]-1 idx P[idx]-1 P[idx] P idx 1 S.append(S[-1] +C[idx]) S.append S append S[-1] +C[idx] S[-1] S -1 C[idx] C idx v,w = k//len(S),k%len(S) v k//len(S) k len(S) len S w k%len(S) k len(S) len S if k<=len(S):
        val = max(S[:k])
    elif S[-1]<=0:
        val = max(S)
    else:
        val1 = S[-1] *(v-1)
        val1 += max(S)
        val2 = S[-1]*v
        if w!=0:
            val2 += max(0,max(S[:w]))
        val = max(val1,val2)
     k<=len(S) k len(S) len S val = max(S[:k]) val max(S[:k]) max S[:k] S :k k elif S[-1]<=0:
        val = max(S)
     S[-1]<=0 S[-1] S -1 0 val = max(S) val max(S) max S val1 = S[-1] *(v-1) val1 S[-1] *(v-1) S[-1] S -1 v-1 v 1 val1 += max(S) val1 max(S) max S val2 = S[-1]*v val2 S[-1]*v S[-1] S -1 v if w!=0:
            val2 += max(0,max(S[:w]))
         w!=0 w 0 val2 += max(0,max(S[:w])) val2 max(0,max(S[:w])) max 0 max(S[:w]) max S[:w] S :w w val = max(val1,val2) val max(val1,val2) max val1 val2 ans = max(ans,val) ans max(ans,val) max ans val print(ans) print ans C = list(map(int,input().split())) list(map(int,input().split())) C val2 = S[-1]*v S[-1]*v val2 S = [] [] S v,w = k//len(S),k%len(S) k//len(S) v A = [n]*n [n]*n A val1 = S[-1] *(v-1) S[-1] *(v-1) val1 val1 += max(S) max(S) val1 ans = max(ans,val) max(ans,val) ans k = map(int,input().split()) map(int,input().split()) k idx = P[i]-1 P[i]-1 idx P = list(map(int,input().split())) list(map(int,input().split())) P n,k = map(int,input().split()) map(int,input().split()) n val = max(S[:k]) max(S[:k]) val w = k//len(S),k%len(S) k%len(S) w idx = P[idx]-1 P[idx]-1 idx val2 += max(0,max(S[:w])) max(0,max(S[:w])) val2 val = max(S) max(S) val val = max(val1,val2) max(val1,val2) val g = [[0]*(n) for _ in range(n)] [[0]*(n) for _ in range(n)] g ans = -float('inf') -float('inf') ans
n,k = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
g = [[0]*(n) for _ in range(n)]
A = [n]*n
# for i in range(n):
#     tmp = 0
#     idx = i
#     cnt = 0
#     set_ =set()
#     while cnt<n:
#         if C[idx] not in set_:
#             tmp += C[idx]
#             set_.add(C[idx])
#             g[i][cnt] = tmp
#             idx = P[idx]-1
#             cnt += 1
#         else:
#             p = len(set_)
#             A[i] = p
#             break
ans = -float('inf')

for i in range(n):
    S = []
    idx = P[i]-1
    S.append(C[idx])
    while idx != i:
        idx = P[idx]-1
        S.append(S[-1] +C[idx])
    v,w = k//len(S),k%len(S)
    if k<=len(S):
        val = max(S[:k])
    elif S[-1]<=0:
        val = max(S)
    else:
        val1 = S[-1] *(v-1)
        val1 += max(S)
        val2 = S[-1]*v
        if w!=0:
            val2 += max(0,max(S[:w]))
        val = max(val1,val2)
    ans = max(ans,val)
# for i in range(n):
#     v,w = k//A[i],k%A[i]
#     if A[i]<k:
#         if g[i][A[i]-1]<=0:
#             val = max(g[i][:A[i]])
#         else:
#             val1 = (v-1)*g[i][A[i]-1]
#             val1 += max(g[i][:A[i]])
#             val2 = v*g[i][A[i]-1]
#             if w!=0:
#                 val2 += max(0,max(g[i][:w]))
#             val = max(val1,val2)
#     else:
#         val = max(g[i][:k])
#     ans = max(ans,val)
print(ans)