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)))

from itertools import accumulate

def sum_max(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
    return ans

def sum_min(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
    return ans

def circle_sum_max(circle,num):
    n = len(circle)
    s = sum(circle)
    if num == 0:
        ans = 0
    else:
        ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1))
    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 + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m))
        else:
            score = max(score,circle_sum_max(cir,m))
    else:
        score = max(score,circle_sum_max(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 from itertools import accumulate def sum_max(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
    return ans sum_max n = len(line) n len(line) len line ac = list(accumulate([0]+line)) ac list(accumulate([0]+line)) list accumulate([0]+line) accumulate [0]+line [0] 0 line l = 0 l 0 r = n_min r n_min ans = ac[r]-ac[l] ans ac[r]-ac[l] ac[r] ac r ac[l] ac l i  = 0 i 0 if n_min==n_max:
        for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
     n_min==n_max n_min n_max for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
     i range(n-n_min+1) range n-n_min+1 n-n_min n n_min 1 ans = max(ans,ac[i+n_min]-ac[i]) ans max(ans,ac[i+n_min]-ac[i]) max ans ac[i+n_min]-ac[i] ac[i+n_min] ac i+n_min i n_min ac[i] ac i while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
     r!=n or i%2==1 r!=n r n i%2==1 i%2 i 2 1 if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
             i%2==0 i%2 i 2 0 r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1 r ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1 ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) [r+1:l+n_max+1].index [r+1:l+n_max+1] ac r+1:l+n_max+1 r+1 r 1 l+n_max+1 l+n_max l n_max 1 index max(ac[r+1:l+n_max+1]) max ac[r+1:l+n_max+1] ac r+1:l+n_max+1 r+1 r 1 l+n_max+1 l+n_max l n_max 1 r 1 l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1 l ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1 ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) [l+1:r-n_min+1].index [l+1:r-n_min+1] ac l+1:r-n_min+1 l+1 l 1 r-n_min+1 r-n_min r n_min 1 index min(ac[l+1:r-n_min+1]) min ac[l+1:r-n_min+1] ac l+1:r-n_min+1 l+1 l 1 r-n_min+1 r-n_min r n_min 1 l 1 i+=1 i 1 ans = max(ans,ac[r]-ac[l]) ans max(ans,ac[r]-ac[l]) max ans ac[r]-ac[l] ac[r] ac r ac[l] ac l return ans ans line line n_min n_min n_max n_max def sum_min(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
    return ans sum_min n = len(line) n len(line) len line ac = list(accumulate([0]+line)) ac list(accumulate([0]+line)) list accumulate([0]+line) accumulate [0]+line [0] 0 line l = 0 l 0 r = n_min r n_min ans = ac[r]-ac[l] ans ac[r]-ac[l] ac[r] ac r ac[l] ac l i  = 0 i 0 if n_min==n_max:
        for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
     n_min==n_max n_min n_max for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
     i range(n-n_min+1) range n-n_min+1 n-n_min n n_min 1 ans = min(ans,ac[i+n_min]-ac[i]) ans min(ans,ac[i+n_min]-ac[i]) min ans ac[i+n_min]-ac[i] ac[i+n_min] ac i+n_min i n_min ac[i] ac i while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
     r!=n or i%2==1 r!=n r n i%2==1 i%2 i 2 1 if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
             i%2==0 i%2 i 2 0 r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1 r ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1 ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) [r+1:l+n_max+1].index [r+1:l+n_max+1] ac r+1:l+n_max+1 r+1 r 1 l+n_max+1 l+n_max l n_max 1 index min(ac[r+1:l+n_max+1]) min ac[r+1:l+n_max+1] ac r+1:l+n_max+1 r+1 r 1 l+n_max+1 l+n_max l n_max 1 r 1 l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1 l ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1 ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) [l+1:r-n_min+1].index [l+1:r-n_min+1] ac l+1:r-n_min+1 l+1 l 1 r-n_min+1 r-n_min r n_min 1 index max(ac[l+1:r-n_min+1]) max ac[l+1:r-n_min+1] ac l+1:r-n_min+1 l+1 l 1 r-n_min+1 r-n_min r n_min 1 l 1 i+=1 i 1 ans = min(ans,ac[r]-ac[l]) ans min(ans,ac[r]-ac[l]) min ans ac[r]-ac[l] ac[r] ac r ac[l] ac l return ans ans line line n_min n_min n_max n_max def circle_sum_max(circle,num):
    n = len(circle)
    s = sum(circle)
    if num == 0:
        ans = 0
    else:
        ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1))
    return ans
  circle_sum_max n = len(circle) n len(circle) len circle s = sum(circle) s sum(circle) sum circle if num == 0:
        ans = 0
    else:
        ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1))
     num == 0 num 0 ans = 0 ans 0 ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1)) ans max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1)) max sum_max(circle,1,num) sum_max circle 1 num s-sum_min(circle,n-num,n-1) s sum_min(circle,n-num,n-1) sum_min circle n-num n num n-1 n 1 return ans ans circle circle 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 + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m))
        else:
            score = max(score,circle_sum_max(cir,m))
    else:
        score = max(score,circle_sum_max(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 + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m))
        else:
            score = max(score,circle_sum_max(cir,m))
    else:
        score = max(score,circle_sum_max(cir,k)) k>m k m if a>0:
            score = max(score, (k//m)*a + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m))
        else:
            score = max(score,circle_sum_max(cir,m))
     a>0 a 0 score = max(score, (k//m)*a + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m)) score max(score, (k//m)*a + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m)) max score (k//m)*a + circle_sum_max(cir,k%m) (k//m)*a k//m k m a circle_sum_max(cir,k%m) circle_sum_max cir k%m k m (k//m-1)*a + circle_sum_max(cir,m) (k//m-1)*a k//m-1 k//m k m 1 a circle_sum_max(cir,m) circle_sum_max cir m score = max(score,circle_sum_max(cir,m)) score max(score,circle_sum_max(cir,m)) max score circle_sum_max(cir,m) circle_sum_max cir m score = max(score,circle_sum_max(cir,k)) score max(score,circle_sum_max(cir,k)) max score circle_sum_max(cir,k) circle_sum_max cir k print(score) print score j = p[i] p[i] j def readints():return list(map(int,readline().split())) def readints():return list(map(int,readline().split())) readints used = [0]*n [0]*n used n,k = readints() readints() n score = max(score,circle_sum_max(cir,m)) max(score,circle_sum_max(cir,m)) score def circle_sum_max(circle,num):
    n = len(circle)
    s = sum(circle)
    if num == 0:
        ans = 0
    else:
        ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1))
    return ans
  def circle_sum_max(circle,num):
    n = len(circle)
    s = sum(circle)
    if num == 0:
        ans = 0
    else:
        ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1))
    return ans
  circle_sum_max j = p[j] p[j] j score = -10**20 -10**20 score score = max(score, (k//m)*a + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m)) max(score, (k//m)*a + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m)) score k = readints() readints() k p = [x-1 for x in readints()] [x-1 for x in readints()] p def readint():return int(readline()) def readint():return int(readline()) readint def readstrs():return list(readline().decode().split()) def readstrs():return list(readline().decode().split()) readstrs c = readints() readints() c m = len(cir) len(cir) m def sum_min(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
    return ans def sum_min(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
    return ans sum_min circles = [] [] circles def printrows(x):print('\n'.join(map(str,x))) def printrows(x):print('\n'.join(map(str,x))) printrows def printline(x):print(' '.join(map(str,x))) def printline(x):print(' '.join(map(str,x))) printline score = max(score,circle_sum_max(cir,k)) max(score,circle_sum_max(cir,k)) score readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline def sum_max(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
    return ans def sum_max(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
    return ans sum_max a = sum(cir) sum(cir) a def readstr():return readline().rstrip().decode() def readstr():return readline().rstrip().decode() readstr
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)))

from itertools import accumulate

def sum_max(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = max(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(max(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(min(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = max(ans,ac[r]-ac[l])
    return ans

def sum_min(line,n_min,n_max):
    n = len(line)
    ac = list(accumulate([0]+line))
    l = 0
    r = n_min
    ans = ac[r]-ac[l]
    i  = 0
    if n_min==n_max:
        for i in range(n-n_min+1):
            ans = min(ans,ac[i+n_min]-ac[i])
    else:
        while r!=n or i%2==1:
            if i%2==0:
                r = ac[r+1:l+n_max+1].index(min(ac[r+1:l+n_max+1])) + r+1
            else:
                l = ac[l+1:r-n_min+1].index(max(ac[l+1:r-n_min+1])) + l+1
            i+=1
            ans = min(ans,ac[r]-ac[l])
    return ans

def circle_sum_max(circle,num):
    n = len(circle)
    s = sum(circle)
    if num == 0:
        ans = 0
    else:
        ans = max(sum_max(circle,1,num), s-sum_min(circle,n-num,n-1))
    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 + circle_sum_max(cir,k%m), (k//m-1)*a + circle_sum_max(cir,m))
        else:
            score = max(score,circle_sum_max(cir,m))
    else:
        score = max(score,circle_sum_max(cir,k))

print(score)








