from collections import deque
it = lambda: list(map(int, input().strip().split()))


def maximise(L, R, cycle):
    cur = 0
    ans = max(cycle)
    queue = deque([0])
    dp = [0] * (2 * L + 1)
    for i, num in enumerate(cycle + cycle):
        j = i + 1
        cur += num
        while queue and j - queue[0] > R:
            queue.popleft()
        if queue:
            ans = max(ans, cur - dp[queue[0]])
        while queue and dp[queue[-1]] >= cur:
            queue.pop()
        queue.append(j)
        dp[j] = cur
    return ans


def get(K, cycle):
    L = len(cycle)
    T = sum(cycle)

    if K > L:
        B = (K - 1) // L
        R = K - B * L
    else:
        B = 0
        R = K
    return max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T))


def solve():
    # square labelled from 1 to N
    N, K = it()
    P = it()
    C = it()

    for i in range(N):
        P[i] -= 1

    cycles = []
    seen = [False] * N
    for i in range(N):
        if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
    ans = float('-inf')
    for cycle in cycles:
        ans = max(ans, get(K, cycle))
    return ans


if __name__ == '__main__':
    ans = solve()
    print(ans) from collections import deque it = lambda: list(map(int, input().strip().split())) it lambda: list(map(int, input().strip().split())) list(map(int, input().strip().split())) list map(int, input().strip().split()) map int input().strip().split() ().strip().split ().strip() ().strip () input strip split def maximise(L, R, cycle):
    cur = 0
    ans = max(cycle)
    queue = deque([0])
    dp = [0] * (2 * L + 1)
    for i, num in enumerate(cycle + cycle):
        j = i + 1
        cur += num
        while queue and j - queue[0] > R:
            queue.popleft()
        if queue:
            ans = max(ans, cur - dp[queue[0]])
        while queue and dp[queue[-1]] >= cur:
            queue.pop()
        queue.append(j)
        dp[j] = cur
    return ans maximise cur = 0 cur 0 ans = max(cycle) ans max(cycle) max cycle queue = deque([0]) queue deque([0]) deque [0] 0 dp = [0] * (2 * L + 1) dp [0] * (2 * L + 1) [0] 0 2 * L + 1 2 * L 2 L 1 for i, num in enumerate(cycle + cycle):
        j = i + 1
        cur += num
        while queue and j - queue[0] > R:
            queue.popleft()
        if queue:
            ans = max(ans, cur - dp[queue[0]])
        while queue and dp[queue[-1]] >= cur:
            queue.pop()
        queue.append(j)
        dp[j] = cur
     i num enumerate(cycle + cycle) enumerate cycle + cycle cycle cycle j = i + 1 j i + 1 i 1 cur += num cur num while queue and j - queue[0] > R:
            queue.popleft()
         queue and j - queue[0] > R queue j - queue[0] > R j - queue[0] j queue[0] queue 0 R queue.popleft() queue.popleft queue popleft if queue:
            ans = max(ans, cur - dp[queue[0]])
         queue ans = max(ans, cur - dp[queue[0]]) ans max(ans, cur - dp[queue[0]]) max ans cur - dp[queue[0]] cur dp[queue[0]] dp queue[0] queue 0 while queue and dp[queue[-1]] >= cur:
            queue.pop()
         queue and dp[queue[-1]] >= cur queue dp[queue[-1]] >= cur dp[queue[-1]] dp queue[-1] queue -1 cur queue.pop() queue.pop queue pop queue.append(j) queue.append queue append j dp[j] = cur dp[j] dp j cur return ans ans L L R R cycle cycle def get(K, cycle):
    L = len(cycle)
    T = sum(cycle)

    if K > L:
        B = (K - 1) // L
        R = K - B * L
    else:
        B = 0
        R = K
    return max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T)) get L = len(cycle) L len(cycle) len cycle T = sum(cycle) T sum(cycle) sum cycle if K > L:
        B = (K - 1) // L
        R = K - B * L
    else:
        B = 0
        R = K
     K > L K L B = (K - 1) // L B (K - 1) // L K - 1 K 1 L R = K - B * L R K - B * L K B * L B L B = 0 B 0 R = K R K return max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T)) max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T)) max maximise(L, K, cycle) maximise L K cycle maximise(L, R, cycle) + max(0, B * T) maximise(L, R, cycle) maximise L R cycle max(0, B * T) max 0 B * T B T K K cycle cycle def solve():
    # square labelled from 1 to N
    N, K = it()
    P = it()
    C = it()

    for i in range(N):
        P[i] -= 1

    cycles = []
    seen = [False] * N
    for i in range(N):
        if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
    ans = float('-inf')
    for cycle in cycles:
        ans = max(ans, get(K, cycle))
    return ans solve N, K = it() N it() it K P = it() P it() it C = it() C it() it for i in range(N):
        P[i] -= 1

     i range(N) range N P[i] -= 1 P[i] P i 1 cycles = [] cycles [] seen = [False] * N seen [False] * N [False] False N for i in range(N):
        if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
     i range(N) range N if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
     not seen[i] seen[i] seen i cycle = [] cycle [] first = curr = i first i = curr = i curr i while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
             True seen[curr] = True seen[curr] seen curr True cycle.append(C[curr]) cycle.append cycle append C[curr] C curr curr = P[curr] curr P[curr] P curr if curr == first:
                    break
             curr == first curr first break cycles.append(cycle) cycles.append cycles append cycle ans = float('-inf') ans float('-inf') float '-inf' for cycle in cycles:
        ans = max(ans, get(K, cycle))
     cycle cycles ans = max(ans, get(K, cycle)) ans max(ans, get(K, cycle)) max ans get(K, cycle) get K cycle return ans ans if __name__ == '__main__':
    ans = solve()
    print(ans) __name__ == '__main__' __name__ '__main__' ans = solve() ans solve() solve print(ans) print ans it = lambda: list(map(int, input().strip().split())) lambda: list(map(int, input().strip().split())) it def solve():
    # square labelled from 1 to N
    N, K = it()
    P = it()
    C = it()

    for i in range(N):
        P[i] -= 1

    cycles = []
    seen = [False] * N
    for i in range(N):
        if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
    ans = float('-inf')
    for cycle in cycles:
        ans = max(ans, get(K, cycle))
    return ans def solve():
    # square labelled from 1 to N
    N, K = it()
    P = it()
    C = it()

    for i in range(N):
        P[i] -= 1

    cycles = []
    seen = [False] * N
    for i in range(N):
        if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
    ans = float('-inf')
    for cycle in cycles:
        ans = max(ans, get(K, cycle))
    return ans solve def maximise(L, R, cycle):
    cur = 0
    ans = max(cycle)
    queue = deque([0])
    dp = [0] * (2 * L + 1)
    for i, num in enumerate(cycle + cycle):
        j = i + 1
        cur += num
        while queue and j - queue[0] > R:
            queue.popleft()
        if queue:
            ans = max(ans, cur - dp[queue[0]])
        while queue and dp[queue[-1]] >= cur:
            queue.pop()
        queue.append(j)
        dp[j] = cur
    return ans def maximise(L, R, cycle):
    cur = 0
    ans = max(cycle)
    queue = deque([0])
    dp = [0] * (2 * L + 1)
    for i, num in enumerate(cycle + cycle):
        j = i + 1
        cur += num
        while queue and j - queue[0] > R:
            queue.popleft()
        if queue:
            ans = max(ans, cur - dp[queue[0]])
        while queue and dp[queue[-1]] >= cur:
            queue.pop()
        queue.append(j)
        dp[j] = cur
    return ans maximise ans = solve() solve() ans def get(K, cycle):
    L = len(cycle)
    T = sum(cycle)

    if K > L:
        B = (K - 1) // L
        R = K - B * L
    else:
        B = 0
        R = K
    return max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T)) def get(K, cycle):
    L = len(cycle)
    T = sum(cycle)

    if K > L:
        B = (K - 1) // L
        R = K - B * L
    else:
        B = 0
        R = K
    return max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T)) get
from collections import deque
it = lambda: list(map(int, input().strip().split()))


def maximise(L, R, cycle):
    cur = 0
    ans = max(cycle)
    queue = deque([0])
    dp = [0] * (2 * L + 1)
    for i, num in enumerate(cycle + cycle):
        j = i + 1
        cur += num
        while queue and j - queue[0] > R:
            queue.popleft()
        if queue:
            ans = max(ans, cur - dp[queue[0]])
        while queue and dp[queue[-1]] >= cur:
            queue.pop()
        queue.append(j)
        dp[j] = cur
    return ans


def get(K, cycle):
    L = len(cycle)
    T = sum(cycle)

    if K > L:
        B = (K - 1) // L
        R = K - B * L
    else:
        B = 0
        R = K
    return max(maximise(L, K, cycle), maximise(L, R, cycle) + max(0, B * T))


def solve():
    # square labelled from 1 to N
    N, K = it()
    P = it()
    C = it()

    for i in range(N):
        P[i] -= 1

    cycles = []
    seen = [False] * N
    for i in range(N):
        if not seen[i]:
            cycle = []
            first = curr = i
            while True:
                seen[curr] = True
                cycle.append(C[curr])
                curr = P[curr]
                if curr == first:
                    break
            cycles.append(cycle)
    
    # print(cycles)
    ans = float('-inf')
    for cycle in cycles:
        ans = max(ans, get(K, cycle))
    return ans


if __name__ == '__main__':
    ans = solve()
    print(ans)