INF = float("inf")

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = -INF
for s in range(N):
    ### STEP1
    cum = []
    i = P[s] - 1 #s-> i
    cum.append(C[i])
    while i != s:
        i = P[i] - 1
        cum.append(cum[-1] + C[i])
    

    # STEP2
    if K <= len(cum):
        #1週未満しかできない
        score = max(cum[:K])
    
    elif cum[-1] <= 0:
        #一周以上できるが、ループしたら損する場合
        score = max(cum)
    else:
        # K//Length - 1回だけループし、最後は最適に止まる
        score1 = cum[-1] * (K//len(cum) - 1)
        score1 += max(cum)

        #ループをK//length回だけループする場合
        score2 = cum[-1]*(K//len(cum))
        resi = K % len(cum) #残り進める回数
        if resi != 0:
            #まだ進めるなら
            score2 += max(0, max(cum[:resi]))
        score = max(score1, score2)
    
    ans = max(ans, score)

print(ans) INF = float("inf") INF float("inf") float "inf" 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 ans = -INF ans -INF INF for s in range(N):
    ### STEP1
    cum = []
    i = P[s] - 1 #s-> i
    cum.append(C[i])
    while i != s:
        i = P[i] - 1
        cum.append(cum[-1] + C[i])
    

    # STEP2
    if K <= len(cum):
        #1週未満しかできない
        score = max(cum[:K])
    
    elif cum[-1] <= 0:
        #一周以上できるが、ループしたら損する場合
        score = max(cum)
    else:
        # K//Length - 1回だけループし、最後は最適に止まる
        score1 = cum[-1] * (K//len(cum) - 1)
        score1 += max(cum)

        #ループをK//length回だけループする場合
        score2 = cum[-1]*(K//len(cum))
        resi = K % len(cum) #残り進める回数
        if resi != 0:
            #まだ進めるなら
            score2 += max(0, max(cum[:resi]))
        score = max(score1, score2)
    
    ans = max(ans, score) s range(N) range N cum = [] cum [] i = P[s] - 1 i P[s] - 1 P[s] P s 1 cum.append(C[i]) cum.append cum append C[i] C i while i != s:
        i = P[i] - 1
        cum.append(cum[-1] + C[i])
    

    # STEP2
     i != s i s i = P[i] - 1 i P[i] - 1 P[i] P i 1 cum.append(cum[-1] + C[i]) cum.append cum append cum[-1] + C[i] cum[-1] cum -1 C[i] C i if K <= len(cum):
        #1週未満しかできない
        score = max(cum[:K])
    
    elif cum[-1] <= 0:
        #一周以上できるが、ループしたら損する場合
        score = max(cum)
    else:
        # K//Length - 1回だけループし、最後は最適に止まる
        score1 = cum[-1] * (K//len(cum) - 1)
        score1 += max(cum)

        #ループをK//length回だけループする場合
        score2 = cum[-1]*(K//len(cum))
        resi = K % len(cum) #残り進める回数
        if resi != 0:
            #まだ進めるなら
            score2 += max(0, max(cum[:resi]))
        score = max(score1, score2)
    
     K <= len(cum) K len(cum) len cum score = max(cum[:K]) score max(cum[:K]) max cum[:K] cum :K K elif cum[-1] <= 0:
        #一周以上できるが、ループしたら損する場合
        score = max(cum)
     cum[-1] <= 0 cum[-1] cum -1 0 score = max(cum) score max(cum) max cum score1 = cum[-1] * (K//len(cum) - 1) score1 cum[-1] * (K//len(cum) - 1) cum[-1] cum -1 K//len(cum) - 1 K//len(cum) K len(cum) len cum 1 score1 += max(cum) score1 max(cum) max cum score2 = cum[-1]*(K//len(cum)) score2 cum[-1]*(K//len(cum)) cum[-1] cum -1 K//len(cum) K len(cum) len cum resi = K % len(cum) resi K % len(cum) K len(cum) len cum if resi != 0:
            #まだ進めるなら
            score2 += max(0, max(cum[:resi]))
         resi != 0 resi 0 score2 += max(0, max(cum[:resi])) score2 max(0, max(cum[:resi])) max 0 max(cum[:resi]) max cum[:resi] cum :resi resi score = max(score1, score2) score max(score1, score2) max score1 score2 ans = max(ans, score) ans max(ans, score) max ans score print(ans) print ans ans = max(ans, score) max(ans, score) ans K = map(int, input().split()) map(int, input().split()) K P = list(map(int, input().split())) list(map(int, input().split())) P score1 = cum[-1] * (K//len(cum) - 1) cum[-1] * (K//len(cum) - 1) score1 score = max(cum[:K]) max(cum[:K]) score C = list(map(int, input().split())) list(map(int, input().split())) C score2 = cum[-1]*(K//len(cum)) cum[-1]*(K//len(cum)) score2 resi = K % len(cum) K % len(cum) resi score = max(cum) max(cum) score INF = float("inf") float("inf") INF cum = [] [] cum score2 += max(0, max(cum[:resi])) max(0, max(cum[:resi])) score2 N, K = map(int, input().split()) map(int, input().split()) N score = max(score1, score2) max(score1, score2) score i = P[s] - 1 P[s] - 1 i score1 += max(cum) max(cum) score1 ans = -INF -INF ans i = P[i] - 1 P[i] - 1 i
INF = float("inf")

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = -INF
for s in range(N):
    ### STEP1
    cum = []
    i = P[s] - 1 #s-> i
    cum.append(C[i])
    while i != s:
        i = P[i] - 1
        cum.append(cum[-1] + C[i])
    

    # STEP2
    if K <= len(cum):
        #1週未満しかできない
        score = max(cum[:K])
    
    elif cum[-1] <= 0:
        #一周以上できるが、ループしたら損する場合
        score = max(cum)
    else:
        # K//Length - 1回だけループし、最後は最適に止まる
        score1 = cum[-1] * (K//len(cum) - 1)
        score1 += max(cum)

        #ループをK//length回だけループする場合
        score2 = cum[-1]*(K//len(cum))
        resi = K % len(cum) #残り進める回数
        if resi != 0:
            #まだ進めるなら
            score2 += max(0, max(cum[:resi]))
        score = max(score1, score2)
    
    ans = max(ans, score)

print(ans)
