import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

def check(cir,num):
    ans = -10**20
    l = len(cir)
    for start in range(l):
        a = 0
        for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
    return ans

n,k = readints()
p = [x-1 for x in readints()]
c = readints()

circles = []
used = [0]*n

for i in range(n):
    if not used[i]:
        circles.append([c[i]])
        used[i] = 1
        j = p[i]
        while not used[j]:
            circles[-1].append(c[j])
            used[j] = 1
            j = p[j]

score = -10**20

for cir in circles:
    m = len(cir)
    a = sum(cir)
    if k>m:
        if a>0:
            score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m))
        else:
            score = max(score,check(cir,m))
    else:
        score = max(score,check(cir,k))

print(score)







 import sys sys sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline def readstr():return readline().rstrip().decode() readstr def readstrs():return list(readline().decode().split()) readstrs def readint():return int(readline()) readint def readints():return list(map(int,readline().split())) readints def printrows(x):print('\n'.join(map(str,x))) printrows x x def printline(x):print(' '.join(map(str,x))) printline x x def check(cir,num):
    ans = -10**20
    l = len(cir)
    for start in range(l):
        a = 0
        for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
    return ans check ans = -10**20 ans -10**20 -10 20 l = len(cir) l len(cir) len cir for start in range(l):
        a = 0
        for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
     start range(l) range l a = 0 a 0 for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
     i range(num) range num if start+i == l:
                start = -i
             start+i == l start+i start i l start = -i start -i i a += cir[start + i] a cir[start + i] cir start + i start i ans = max(ans,a) ans max(ans,a) max ans a return ans ans cir cir num num n,k = readints() n readints() readints k x-1 for x in readints() for x in readints() x readints() readints for x in readints() x-1 x 1 p = [x-1 for x in readints()] p [x-1 for x in readints()] c = readints() c readints() readints circles = [] circles [] used = [0]*n used [0]*n [0] 0 n for i in range(n):
    if not used[i]:
        circles.append([c[i]])
        used[i] = 1
        j = p[i]
        while not used[j]:
            circles[-1].append(c[j])
            used[j] = 1
            j = p[j] i range(n) range n if not used[i]:
        circles.append([c[i]])
        used[i] = 1
        j = p[i]
        while not used[j]:
            circles[-1].append(c[j])
            used[j] = 1
            j = p[j] not used[i] used[i] used i circles.append([c[i]]) circles.append circles append [c[i]] c[i] c i used[i] = 1 used[i] used i 1 j = p[i] j p[i] p i while not used[j]:
            circles[-1].append(c[j])
            used[j] = 1
            j = p[j] not used[j] used[j] used j circles[-1].append(c[j]) [-1].append [-1] circles -1 append c[j] c j used[j] = 1 used[j] used j 1 j = p[j] j p[j] p j score = -10**20 score -10**20 -10 20 for cir in circles:
    m = len(cir)
    a = sum(cir)
    if k>m:
        if a>0:
            score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m))
        else:
            score = max(score,check(cir,m))
    else:
        score = max(score,check(cir,k)) cir circles m = len(cir) m len(cir) len cir a = sum(cir) a sum(cir) sum cir if k>m:
        if a>0:
            score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m))
        else:
            score = max(score,check(cir,m))
    else:
        score = max(score,check(cir,k)) k>m k m if a>0:
            score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m))
        else:
            score = max(score,check(cir,m))
     a>0 a 0 score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m)) score max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m)) max score (k//m)*a + check(cir,k%m) (k//m)*a k//m k m a check(cir,k%m) check cir k%m k m (k//m-1)*a + check(cir,m) (k//m-1)*a k//m-1 k//m k m 1 a check(cir,m) check cir m score = max(score,check(cir,m)) score max(score,check(cir,m)) max score check(cir,m) check cir m score = max(score,check(cir,k)) score max(score,check(cir,k)) max score check(cir,k) check cir k print(score) print score score = max(score,check(cir,m)) max(score,check(cir,m)) score def check(cir,num):
    ans = -10**20
    l = len(cir)
    for start in range(l):
        a = 0
        for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
    return ans def check(cir,num):
    ans = -10**20
    l = len(cir)
    for start in range(l):
        a = 0
        for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
    return ans check j = p[j] p[j] j score = -10**20 -10**20 score def readint():return int(readline()) def readint():return int(readline()) readint score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m)) max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m)) score used = [0]*n [0]*n used readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline def printrows(x):print('\n'.join(map(str,x))) def printrows(x):print('\n'.join(map(str,x))) printrows def readstrs():return list(readline().decode().split()) def readstrs():return list(readline().decode().split()) readstrs def printline(x):print(' '.join(map(str,x))) def printline(x):print(' '.join(map(str,x))) printline def readints():return list(map(int,readline().split())) def readints():return list(map(int,readline().split())) readints m = len(cir) len(cir) m p = [x-1 for x in readints()] [x-1 for x in readints()] p k = readints() readints() k c = readints() readints() c a = sum(cir) sum(cir) a j = p[i] p[i] j def readstr():return readline().rstrip().decode() def readstr():return readline().rstrip().decode() readstr n,k = readints() readints() n circles = [] [] circles score = max(score,check(cir,k)) max(score,check(cir,k)) score
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

def check(cir,num):
    ans = -10**20
    l = len(cir)
    for start in range(l):
        a = 0
        for i in range(num):
            if start+i == l:
                start = -i
            a += cir[start + i]
            ans = max(ans,a)
    return ans

n,k = readints()
p = [x-1 for x in readints()]
c = readints()

circles = []
used = [0]*n

for i in range(n):
    if not used[i]:
        circles.append([c[i]])
        used[i] = 1
        j = p[i]
        while not used[j]:
            circles[-1].append(c[j])
            used[j] = 1
            j = p[j]

score = -10**20

for cir in circles:
    m = len(cir)
    a = sum(cir)
    if k>m:
        if a>0:
            score = max(score, (k//m)*a + check(cir,k%m), (k//m-1)*a + check(cir,m))
        else:
            score = max(score,check(cir,m))
    else:
        score = max(score,check(cir,k))

print(score)








