def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    P = [p]
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
    front = P[:L]
    if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################


import sys
input = sys.stdin.readline
from itertools import accumulate

def lift(x): return P[x-1]

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
res = -float("inf")
for i in range(1,N+1):
    front, cycle, end, cnt = cycle_getter(K,i)
    S = sum(C[j-1] for j in front)
    T = sum(C[j-1] for j in cycle)
    if cycle+end:
        U = max(0, max(accumulate(C[j-1] for j in cycle+end)))
    else: U=0
    if front:
        R = max(accumulate(C[j-1] for j in front))
    else: R=0
    res = max(res, S+T*(cnt-1)+U, S+U, R)
print(res) def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    P = [p]
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
    front = P[:L]
    if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################ cycle_getter """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """ p = start p start front, cycle, end = [], [], [] front [] cycle [] end [] cnt = 0 cnt 0 visit = {p:0} visit {p:0} p p 0 L, R = N, -1 L N R -1 P = [p] P [p] p for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
     i range(1,N) range 1 N p = lift(p) p lift(p) lift p if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
         p in visit p visit """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """ L, R = visit[p], i L visit[p] visit p R i period = R-L period R-L R L break visit[p] = i visit[p] visit p i P.append(p) P.append P append p front = P[:L] front P[:L] P :L L if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
     L != N L N cycle, end = P[L:R], P[L:L+(N-L)%period] cycle P[L:R] P L:R L R end P[L:L+(N-L)%period] P L:L+(N-L)%period L L+(N-L)%period L (N-L)%period N-L N L period cnt = (N-L)//period cnt (N-L)//period N-L N L period return front, cycle, end, cnt return front, cycle, end, cnt front cycle end cnt N N start start import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline from itertools import accumulate def lift(x): return P[x-1] lift x x 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 res = -float("inf") res -float("inf") float("inf") float "inf" for i in range(1,N+1):
    front, cycle, end, cnt = cycle_getter(K,i)
    S = sum(C[j-1] for j in front)
    T = sum(C[j-1] for j in cycle)
    if cycle+end:
        U = max(0, max(accumulate(C[j-1] for j in cycle+end)))
    else: U=0
    if front:
        R = max(accumulate(C[j-1] for j in front))
    else: R=0
    res = max(res, S+T*(cnt-1)+U, S+U, R) i range(1,N+1) range 1 N+1 N 1 front, cycle, end, cnt = cycle_getter(K,i) front cycle_getter(K,i) cycle_getter K i cycle end cnt S = sum(C[j-1] for j in front) S sum(C[j-1] for j in front) sum C[j-1] C j-1 j 1 T = sum(C[j-1] for j in cycle) T sum(C[j-1] for j in cycle) sum C[j-1] C j-1 j 1 if cycle+end:
        U = max(0, max(accumulate(C[j-1] for j in cycle+end)))
    else: U=0
     cycle+end cycle end U = max(0, max(accumulate(C[j-1] for j in cycle+end))) U max(0, max(accumulate(C[j-1] for j in cycle+end))) max 0 max(accumulate(C[j-1] for j in cycle+end)) max accumulate(C[j-1] for j in cycle+end) accumulate C[j-1] C j-1 j 1 if front:
        R = max(accumulate(C[j-1] for j in front))
    else: R=0
     front R = max(accumulate(C[j-1] for j in front)) R max(accumulate(C[j-1] for j in front)) max accumulate(C[j-1] for j in front) accumulate C[j-1] C j-1 j 1 res = max(res, S+T*(cnt-1)+U, S+U, R) res max(res, S+T*(cnt-1)+U, S+U, R) max res S+T*(cnt-1)+U S+T*(cnt-1) S T*(cnt-1) T cnt-1 cnt 1 U S+U S U R print(res) print res U = max(0, max(accumulate(C[j-1] for j in cycle+end))) max(0, max(accumulate(C[j-1] for j in cycle+end))) U N, K = map(int, input().split()) map(int, input().split()) N T = sum(C[j-1] for j in cycle) sum(C[j-1] for j in cycle) T input = sys.stdin.readline sys.stdin.readline input front, cycle, end, cnt = cycle_getter(K,i) cycle_getter(K,i) front cycle, end, cnt = cycle_getter(K,i) cycle_getter(K,i) cycle res = -float("inf") -float("inf") res end, cnt = cycle_getter(K,i) cycle_getter(K,i) end K = map(int, input().split()) map(int, input().split()) K def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    P = [p]
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
    front = P[:L]
    if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################ def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    P = [p]
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
    front = P[:L]
    if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################ cycle_getter P = list(map(int, input().split())) list(map(int, input().split())) P def lift(x): return P[x-1] def lift(x): return P[x-1] lift C = list(map(int, input().split())) list(map(int, input().split())) C R = max(accumulate(C[j-1] for j in front)) max(accumulate(C[j-1] for j in front)) R res = max(res, S+T*(cnt-1)+U, S+U, R) max(res, S+T*(cnt-1)+U, S+U, R) res S = sum(C[j-1] for j in front) sum(C[j-1] for j in front) S cnt = cycle_getter(K,i) cycle_getter(K,i) cnt
def cycle_getter(N, start):
    """
    :param N: 移動回数
    :param start:　初期条件
    :return front: cycleまでの要素のリスト
            cycle: cycle内の要素のリスト
            end: cycle後の余った部分の要素のリスト
            cnt: cycle回数
    """
    p = start
    front, cycle, end = [], [], []
    cnt = 0
    visit = {p:0}
    L, R = N, -1
    P = [p]
    for i in range(1,N):
        p = lift(p)
        if p in visit:
            """
            (L, R) = (サイクルに入るまでに移動した回数, サイクルの終端に着くまでに移動した回数)
            [6,2,3,4,0,1,2] ⇒ (L, R) = (1, 6)
            """
            L, R = visit[p], i
            period = R-L
            break
        visit[p] = i
        P.append(p)
    front = P[:L]
    if L != N:
        cycle, end = P[L:R], P[L:L+(N-L)%period]
        cnt = (N-L)//period
    return front, cycle, end, cnt

################################################################################


import sys
input = sys.stdin.readline
from itertools import accumulate

def lift(x): return P[x-1]

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))
res = -float("inf")
for i in range(1,N+1):
    front, cycle, end, cnt = cycle_getter(K,i)
    S = sum(C[j-1] for j in front)
    T = sum(C[j-1] for j in cycle)
    if cycle+end:
        U = max(0, max(accumulate(C[j-1] for j in cycle+end)))
    else: U=0
    if front:
        R = max(accumulate(C[j-1] for j in front))
    else: R=0
    res = max(res, S+T*(cnt-1)+U, S+U, R)
print(res)