n, k = map(int, input().split())


ps = list(map(int, input().split()))
cs = list(map(int, input().split()))

graphs = []
vGraphs = []
flags = [False]*n
sumDict = dict()
for i in range(len(ps)):
    p = ps[i]
    index = p - 1
    if flags[index]:
        continue

    current = set()
    values = set()
    currentIndex = index
    while True:
        if flags[currentIndex]:
            break

        current.add(currentIndex)
        values.add(cs[currentIndex])
        flags[currentIndex] = True
        nextIndex = ps[currentIndex] - 1
        currentIndex = nextIndex
    graphs.append(current)
    vGraphs.append(values)

answer = -float('inf')
for i in range(n):

    searchI = 0
    loopSum = 0
    nextIndex = i
    loopCount = 0
    while True:
        nextIndex = ps[nextIndex] - 1
        loopSum += cs[nextIndex]
        loopCount += 1
        if nextIndex == i:
            break

    total = loopSum

    currentSum = 0
    count = 0
    ci = i
    while True:
        count += 1
        if count > k:
            break
        loop = (k - count) // loopCount
        multi = max(0, total) * loop

        ci = ps[ci] - 1

        currentV = cs[ci]
        currentSum += currentV

        answer = max(answer, multi + currentSum)
        if ci == i:
            break

print(answer) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k ps = list(map(int, input().split())) ps list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split cs = list(map(int, input().split())) cs list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split graphs = [] graphs [] vGraphs = [] vGraphs [] flags = [False]*n flags [False]*n [False] False n sumDict = dict() sumDict dict() dict for i in range(len(ps)):
    p = ps[i]
    index = p - 1
    if flags[index]:
        continue

    current = set()
    values = set()
    currentIndex = index
    while True:
        if flags[currentIndex]:
            break

        current.add(currentIndex)
        values.add(cs[currentIndex])
        flags[currentIndex] = True
        nextIndex = ps[currentIndex] - 1
        currentIndex = nextIndex
    graphs.append(current)
    vGraphs.append(values) i range(len(ps)) range len(ps) len ps p = ps[i] p ps[i] ps i index = p - 1 index p - 1 p 1 if flags[index]:
        continue

     flags[index] flags index continue current = set() current set() set values = set() values set() set currentIndex = index currentIndex index while True:
        if flags[currentIndex]:
            break

        current.add(currentIndex)
        values.add(cs[currentIndex])
        flags[currentIndex] = True
        nextIndex = ps[currentIndex] - 1
        currentIndex = nextIndex
     True if flags[currentIndex]:
            break

         flags[currentIndex] flags currentIndex break current.add(currentIndex) current.add current add currentIndex values.add(cs[currentIndex]) values.add values add cs[currentIndex] cs currentIndex flags[currentIndex] = True flags[currentIndex] flags currentIndex True nextIndex = ps[currentIndex] - 1 nextIndex ps[currentIndex] - 1 ps[currentIndex] ps currentIndex 1 currentIndex = nextIndex currentIndex nextIndex graphs.append(current) graphs.append graphs append current vGraphs.append(values) vGraphs.append vGraphs append values answer = -float('inf') answer -float('inf') float('inf') float 'inf' for i in range(n):

    searchI = 0
    loopSum = 0
    nextIndex = i
    loopCount = 0
    while True:
        nextIndex = ps[nextIndex] - 1
        loopSum += cs[nextIndex]
        loopCount += 1
        if nextIndex == i:
            break

    total = loopSum

    currentSum = 0
    count = 0
    ci = i
    while True:
        count += 1
        if count > k:
            break
        loop = (k - count) // loopCount
        multi = max(0, total) * loop

        ci = ps[ci] - 1

        currentV = cs[ci]
        currentSum += currentV

        answer = max(answer, multi + currentSum)
        if ci == i:
            break i range(n) range n searchI = 0 searchI 0 loopSum = 0 loopSum 0 nextIndex = i nextIndex i loopCount = 0 loopCount 0 while True:
        nextIndex = ps[nextIndex] - 1
        loopSum += cs[nextIndex]
        loopCount += 1
        if nextIndex == i:
            break

     True nextIndex = ps[nextIndex] - 1 nextIndex ps[nextIndex] - 1 ps[nextIndex] ps nextIndex 1 loopSum += cs[nextIndex] loopSum cs[nextIndex] cs nextIndex loopCount += 1 loopCount 1 if nextIndex == i:
            break

     nextIndex == i nextIndex i break total = loopSum total loopSum currentSum = 0 currentSum 0 count = 0 count 0 ci = i ci i while True:
        count += 1
        if count > k:
            break
        loop = (k - count) // loopCount
        multi = max(0, total) * loop

        ci = ps[ci] - 1

        currentV = cs[ci]
        currentSum += currentV

        answer = max(answer, multi + currentSum)
        if ci == i:
            break True count += 1 count 1 if count > k:
            break
         count > k count k break loop = (k - count) // loopCount loop (k - count) // loopCount k - count k count loopCount multi = max(0, total) * loop multi max(0, total) * loop max(0, total) max 0 total loop ci = ps[ci] - 1 ci ps[ci] - 1 ps[ci] ps ci 1 currentV = cs[ci] currentV cs[ci] cs ci currentSum += currentV currentSum currentV answer = max(answer, multi + currentSum) answer max(answer, multi + currentSum) max answer multi + currentSum multi currentSum if ci == i:
            break ci == i ci i break print(answer) print answer count += 1 1 count multi = max(0, total) * loop max(0, total) * loop multi current = set() set() current vGraphs = [] [] vGraphs values = set() set() values ci = i i ci loop = (k - count) // loopCount (k - count) // loopCount loop answer = -float('inf') -float('inf') answer currentIndex = index index currentIndex currentIndex = nextIndex nextIndex currentIndex answer = max(answer, multi + currentSum) max(answer, multi + currentSum) answer index = p - 1 p - 1 index currentSum = 0 0 currentSum loopSum = 0 0 loopSum currentSum += currentV currentV currentSum k = map(int, input().split()) map(int, input().split()) k flags = [False]*n [False]*n flags nextIndex = ps[currentIndex] - 1 ps[currentIndex] - 1 nextIndex total = loopSum loopSum total nextIndex = ps[nextIndex] - 1 ps[nextIndex] - 1 nextIndex count = 0 0 count cs = list(map(int, input().split())) list(map(int, input().split())) cs graphs = [] [] graphs searchI = 0 0 searchI nextIndex = i i nextIndex ps = list(map(int, input().split())) list(map(int, input().split())) ps loopSum += cs[nextIndex] cs[nextIndex] loopSum p = ps[i] ps[i] p currentV = cs[ci] cs[ci] currentV sumDict = dict() dict() sumDict ci = ps[ci] - 1 ps[ci] - 1 ci loopCount = 0 0 loopCount n, k = map(int, input().split()) map(int, input().split()) n loopCount += 1 1 loopCount
n, k = map(int, input().split())


ps = list(map(int, input().split()))
cs = list(map(int, input().split()))

graphs = []
vGraphs = []
flags = [False]*n
sumDict = dict()
for i in range(len(ps)):
    p = ps[i]
    index = p - 1
    if flags[index]:
        continue

    current = set()
    values = set()
    currentIndex = index
    while True:
        if flags[currentIndex]:
            break

        current.add(currentIndex)
        values.add(cs[currentIndex])
        flags[currentIndex] = True
        nextIndex = ps[currentIndex] - 1
        currentIndex = nextIndex
    graphs.append(current)
    vGraphs.append(values)

answer = -float('inf')
for i in range(n):

    searchI = 0
    loopSum = 0
    nextIndex = i
    loopCount = 0
    while True:
        nextIndex = ps[nextIndex] - 1
        loopSum += cs[nextIndex]
        loopCount += 1
        if nextIndex == i:
            break

    total = loopSum

    currentSum = 0
    count = 0
    ci = i
    while True:
        count += 1
        if count > k:
            break
        loop = (k - count) // loopCount
        multi = max(0, total) * loop

        ci = ps[ci] - 1

        currentV = cs[ci]
        currentSum += currentV

        answer = max(answer, multi + currentSum)
        if ci == i:
            break

print(answer)