N, KK = map(int, input().split())
P = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))

cnt = [1] * (N+1)
retsu = []
for i in range(1,N+1):
    idx = i
    r = []
    for _ in range(N):
        if cnt[idx] == 0:
            break
        r.append(C[P[idx]])
        cnt[idx] = 0
        idx = P[idx]
    if r:
        retsu.append(r)

ans = -1 * (10 ** 10)
for z in retsu:
    K = KK
    s = sum(z)
    l = len(z)
    if K <= l:
        plus = 0
    elif s <= 0:
        plus = 0
        K = l
    else:
        plus = s * (K // l)
        if K % l == 0:
            plus -= s
    K %= l
    if K == 0:
        K = l
    
    moto = z + z
    point = [[0] * l for _ in range(l+K-1)]
    kari = -1 * (10 ** 10)
    for i in range(l+K-1):
        for j in range(l):
            if i == j:
                point[i][j] = moto[i]
            elif i - j >= K or i < j:
                continue
            else:
                point[i][j] = moto[i] + point[i-1][j]
            kari = max(kari, point[i][j])
    ans = max(ans, plus + kari)
print(ans) N, KK = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split KK P = [0] + list(map(int, input().split())) P [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split C = [0] + list(map(int, input().split())) C [0] + list(map(int, input().split())) [0] 0 list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split cnt = [1] * (N+1) cnt [1] * (N+1) [1] 1 N+1 N 1 retsu = [] retsu [] for i in range(1,N+1):
    idx = i
    r = []
    for _ in range(N):
        if cnt[idx] == 0:
            break
        r.append(C[P[idx]])
        cnt[idx] = 0
        idx = P[idx]
    if r:
        retsu.append(r) i range(1,N+1) range 1 N+1 N 1 idx = i idx i r = [] r [] for _ in range(N):
        if cnt[idx] == 0:
            break
        r.append(C[P[idx]])
        cnt[idx] = 0
        idx = P[idx]
     _ range(N) range N if cnt[idx] == 0:
            break
         cnt[idx] == 0 cnt[idx] cnt idx 0 break r.append(C[P[idx]]) r.append r append C[P[idx]] C P[idx] P idx cnt[idx] = 0 cnt[idx] cnt idx 0 idx = P[idx] idx P[idx] P idx if r:
        retsu.append(r) r retsu.append(r) retsu.append retsu append r ans = -1 * (10 ** 10) ans -1 * (10 ** 10) -1 10 ** 10 10 10 for z in retsu:
    K = KK
    s = sum(z)
    l = len(z)
    if K <= l:
        plus = 0
    elif s <= 0:
        plus = 0
        K = l
    else:
        plus = s * (K // l)
        if K % l == 0:
            plus -= s
    K %= l
    if K == 0:
        K = l
    
    moto = z + z
    point = [[0] * l for _ in range(l+K-1)]
    kari = -1 * (10 ** 10)
    for i in range(l+K-1):
        for j in range(l):
            if i == j:
                point[i][j] = moto[i]
            elif i - j >= K or i < j:
                continue
            else:
                point[i][j] = moto[i] + point[i-1][j]
            kari = max(kari, point[i][j])
    ans = max(ans, plus + kari) z retsu K = KK K KK s = sum(z) s sum(z) sum z l = len(z) l len(z) len z if K <= l:
        plus = 0
    elif s <= 0:
        plus = 0
        K = l
    else:
        plus = s * (K // l)
        if K % l == 0:
            plus -= s
     K <= l K l plus = 0 plus 0 elif s <= 0:
        plus = 0
        K = l
     s <= 0 s 0 plus = 0 plus 0 K = l K l plus = s * (K // l) plus s * (K // l) s K // l K l if K % l == 0:
            plus -= s
     K % l == 0 K % l K l 0 plus -= s plus s K %= l K l if K == 0:
        K = l
    
     K == 0 K 0 K = l K l moto = z + z moto z + z z z [0] * l for _ in range(l+K-1) for _ in range(l+K-1) _ range(l+K-1) range l+K-1 l+K l K 1 for _ in range(l+K-1) [0] * l [0] 0 l point = [[0] * l for _ in range(l+K-1)] point [[0] * l for _ in range(l+K-1)] kari = -1 * (10 ** 10) kari -1 * (10 ** 10) -1 10 ** 10 10 10 for i in range(l+K-1):
        for j in range(l):
            if i == j:
                point[i][j] = moto[i]
            elif i - j >= K or i < j:
                continue
            else:
                point[i][j] = moto[i] + point[i-1][j]
            kari = max(kari, point[i][j])
     i range(l+K-1) range l+K-1 l+K l K 1 for j in range(l):
            if i == j:
                point[i][j] = moto[i]
            elif i - j >= K or i < j:
                continue
            else:
                point[i][j] = moto[i] + point[i-1][j]
            kari = max(kari, point[i][j])
     j range(l) range l if i == j:
                point[i][j] = moto[i]
            elif i - j >= K or i < j:
                continue
            else:
                point[i][j] = moto[i] + point[i-1][j]
             i == j i j point[i][j] = moto[i] point[i][j] [i] point i j moto[i] moto i elif i - j >= K or i < j:
                continue
             i - j >= K or i < j i - j >= K i - j i j K i < j i j continue point[i][j] = moto[i] + point[i-1][j] point[i][j] [i] point i j moto[i] + point[i-1][j] moto[i] moto i point[i-1][j] [i-1] point i-1 i 1 j kari = max(kari, point[i][j]) kari max(kari, point[i][j]) max kari point[i][j] [i] point i j ans = max(ans, plus + kari) ans max(ans, plus + kari) max ans plus + kari plus kari print(ans) print ans K = l l K idx = i i idx KK = map(int, input().split()) map(int, input().split()) KK retsu = [] [] retsu plus = s * (K // l) s * (K // l) plus K = KK KK K kari = -1 * (10 ** 10) -1 * (10 ** 10) kari r = [] [] r plus = 0 0 plus point = [[0] * l for _ in range(l+K-1)] [[0] * l for _ in range(l+K-1)] point s = sum(z) sum(z) s ans = max(ans, plus + kari) max(ans, plus + kari) ans ans = -1 * (10 ** 10) -1 * (10 ** 10) ans C = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) C l = len(z) len(z) l plus = 0 0 plus moto = z + z z + z moto cnt = [1] * (N+1) [1] * (N+1) cnt P = [0] + list(map(int, input().split())) [0] + list(map(int, input().split())) P N, KK = map(int, input().split()) map(int, input().split()) N plus -= s s plus idx = P[idx] P[idx] idx kari = max(kari, point[i][j]) max(kari, point[i][j]) kari K %= l l K K = l l K
N, KK = map(int, input().split())
P = [0] + list(map(int, input().split()))
C = [0] + list(map(int, input().split()))

cnt = [1] * (N+1)
retsu = []
for i in range(1,N+1):
    idx = i
    r = []
    for _ in range(N):
        if cnt[idx] == 0:
            break
        r.append(C[P[idx]])
        cnt[idx] = 0
        idx = P[idx]
    if r:
        retsu.append(r)

ans = -1 * (10 ** 10)
for z in retsu:
    K = KK
    s = sum(z)
    l = len(z)
    if K <= l:
        plus = 0
    elif s <= 0:
        plus = 0
        K = l
    else:
        plus = s * (K // l)
        if K % l == 0:
            plus -= s
    K %= l
    if K == 0:
        K = l
    
    moto = z + z
    point = [[0] * l for _ in range(l+K-1)]
    kari = -1 * (10 ** 10)
    for i in range(l+K-1):
        for j in range(l):
            if i == j:
                point[i][j] = moto[i]
            elif i - j >= K or i < j:
                continue
            else:
                point[i][j] = moto[i] + point[i-1][j]
            kari = max(kari, point[i][j])
    ans = max(ans, plus + kari)
print(ans)