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):
    m = len(cir)
    a = sum(cir)
    if num == 0:
        ss = 0
    elif num == 1:
        ss = max(cir)
    else:
        ac = list(accumulate([0]+cir))
        l = 0
        r = 1
        ss = ac[r]-ac[l]
        i = 0
        while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

        num = m-num
        l = 0
        r = num
        i = 0
        ss = max(ss,a-ac[r]+ac[l])
        while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
    return ss


from itertools import accumulate

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):
    m = len(cir)
    a = sum(cir)
    if num == 0:
        ss = 0
    elif num == 1:
        ss = max(cir)
    else:
        ac = list(accumulate([0]+cir))
        l = 0
        r = 1
        ss = ac[r]-ac[l]
        i = 0
        while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

        num = m-num
        l = 0
        r = num
        i = 0
        ss = max(ss,a-ac[r]+ac[l])
        while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
    return ss check m = len(cir) m len(cir) len cir a = sum(cir) a sum(cir) sum cir if num == 0:
        ss = 0
    elif num == 1:
        ss = max(cir)
    else:
        ac = list(accumulate([0]+cir))
        l = 0
        r = 1
        ss = ac[r]-ac[l]
        i = 0
        while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

        num = m-num
        l = 0
        r = num
        i = 0
        ss = max(ss,a-ac[r]+ac[l])
        while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
     num == 0 num 0 ss = 0 ss 0 elif num == 1:
        ss = max(cir)
     num == 1 num 1 ss = max(cir) ss max(cir) max cir ac = list(accumulate([0]+cir)) ac list(accumulate([0]+cir)) list accumulate([0]+cir) accumulate [0]+cir [0] 0 cir l = 0 l 0 r = 1 r 1 ss = ac[r]-ac[l] ss ac[r]-ac[l] ac[r] ac r ac[l] ac l i = 0 i 0 while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

         1 if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
             r == m r m l = ac[l+1:r].index(min(ac[l+1:r])) + l+1 l ac[l+1:r].index(min(ac[l+1:r])) + l+1 ac[l+1:r].index(min(ac[l+1:r])) + l ac[l+1:r].index(min(ac[l+1:r])) [l+1:r].index [l+1:r] ac l+1:r l+1 l 1 r index min(ac[l+1:r]) min ac[l+1:r] ac l+1:r l+1 l 1 r l 1 ss = max(ss,ac[r]-ac[l]) ss max(ss,ac[r]-ac[l]) max ss ac[r]-ac[l] ac[r] ac r ac[l] ac l break elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
             i%2==0 i%2 i 2 0 r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1 r ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1 ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) [r+1:l+num+1].index [r+1:l+num+1] ac r+1:l+num+1 r+1 r 1 l+num+1 l+num l num 1 index max(ac[r+1:l+num+1]) max ac[r+1:l+num+1] ac r+1:l+num+1 r+1 r 1 l+num+1 l+num l num 1 r 1 l = ac[l+1:r].index(min(ac[l+1:r])) + l+1 l ac[l+1:r].index(min(ac[l+1:r])) + l+1 ac[l+1:r].index(min(ac[l+1:r])) + l ac[l+1:r].index(min(ac[l+1:r])) [l+1:r].index [l+1:r] ac l+1:r l+1 l 1 r index min(ac[l+1:r]) min ac[l+1:r] ac l+1:r l+1 l 1 r l 1 i+=1 i 1 ss = max(ss,ac[r]-ac[l]) ss max(ss,ac[r]-ac[l]) max ss ac[r]-ac[l] ac[r] ac r ac[l] ac l num = m-num num m-num m num l = 0 l 0 r = num r num i = 0 i 0 ss = max(ss,a-ac[r]+ac[l]) ss max(ss,a-ac[r]+ac[l]) max ss a-ac[r]+ac[l] a-ac[r] a ac[r] ac r ac[l] ac l while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
     1 if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
             r == m r m l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1 l ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1 ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) [l+1:r-num+1].index [l+1:r-num+1] ac l+1:r-num+1 l+1 l 1 r-num+1 r-num r num 1 index max(ac[l+1:r-num+1]) max ac[l+1:r-num+1] ac l+1:r-num+1 l+1 l 1 r-num+1 r-num r num 1 l 1 ss = max(ss,a-ac[r]+ac[l]) ss max(ss,a-ac[r]+ac[l]) max ss a-ac[r]+ac[l] a-ac[r] a ac[r] ac r ac[l] ac l break elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
             i%2==0 i%2 i 2 0 r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1 r ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1 ac[r+1:l+m].index(min(ac[r+1:l+m])) + r ac[r+1:l+m].index(min(ac[r+1:l+m])) [r+1:l+m].index [r+1:l+m] ac r+1:l+m r+1 r 1 l+m l m index min(ac[r+1:l+m]) min ac[r+1:l+m] ac r+1:l+m r+1 r 1 l+m l m r 1 l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1 l ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1 ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) [l+1:r-num+1].index [l+1:r-num+1] ac l+1:r-num+1 l+1 l 1 r-num+1 r-num r num 1 index max(ac[l+1:r-num+1]) max ac[l+1:r-num+1] ac l+1:r-num+1 l+1 l 1 r-num+1 r-num r num 1 l 1 i+=1 i 1 ss = max(ss,a-ac[r]+ac[l]) ss max(ss,a-ac[r]+ac[l]) max ss a-ac[r]+ac[l] a-ac[r] a ac[r] ac r ac[l] ac l return ss ss cir cir num num from itertools import accumulate 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 readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline def printline(x):print(' '.join(map(str,x))) def printline(x):print(' '.join(map(str,x))) printline n,k = readints() readints() n m = len(cir) len(cir) m def check(cir,num):
    m = len(cir)
    a = sum(cir)
    if num == 0:
        ss = 0
    elif num == 1:
        ss = max(cir)
    else:
        ac = list(accumulate([0]+cir))
        l = 0
        r = 1
        ss = ac[r]-ac[l]
        i = 0
        while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

        num = m-num
        l = 0
        r = num
        i = 0
        ss = max(ss,a-ac[r]+ac[l])
        while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
    return ss def check(cir,num):
    m = len(cir)
    a = sum(cir)
    if num == 0:
        ss = 0
    elif num == 1:
        ss = max(cir)
    else:
        ac = list(accumulate([0]+cir))
        l = 0
        r = 1
        ss = ac[r]-ac[l]
        i = 0
        while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

        num = m-num
        l = 0
        r = num
        i = 0
        ss = max(ss,a-ac[r]+ac[l])
        while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
    return ss check score = max(score,check(cir,k)) max(score,check(cir,k)) score def readstrs():return list(readline().decode().split()) def readstrs():return list(readline().decode().split()) readstrs j = p[j] p[j] j k = readints() readints() k def readint():return int(readline()) def readint():return int(readline()) readint score = -10**20 -10**20 score def readstr():return readline().rstrip().decode() def readstr():return readline().rstrip().decode() readstr c = readints() readints() c 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 def readints():return list(map(int,readline().split())) def readints():return list(map(int,readline().split())) readints p = [x-1 for x in readints()] [x-1 for x in readints()] p circles = [] [] circles score = max(score,check(cir,m)) max(score,check(cir,m)) score j = p[i] p[i] j a = sum(cir) sum(cir) a used = [0]*n [0]*n used def printrows(x):print('\n'.join(map(str,x))) def printrows(x):print('\n'.join(map(str,x))) printrows
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):
    m = len(cir)
    a = sum(cir)
    if num == 0:
        ss = 0
    elif num == 1:
        ss = max(cir)
    else:
        ac = list(accumulate([0]+cir))
        l = 0
        r = 1
        ss = ac[r]-ac[l]
        i = 0
        while 1:
            if r == m:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
                ss = max(ss,ac[r]-ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+num+1].index(max(ac[r+1:l+num+1])) + r+1
            else:
                l = ac[l+1:r].index(min(ac[l+1:r])) + l+1
            i+=1
            ss = max(ss,ac[r]-ac[l])

        num = m-num
        l = 0
        r = num
        i = 0
        ss = max(ss,a-ac[r]+ac[l])
        while 1:
            if r == m:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
                ss = max(ss,a-ac[r]+ac[l])
                break
            elif i%2==0:
                r = ac[r+1:l+m].index(min(ac[r+1:l+m])) + r+1
            else:
                l = ac[l+1:r-num+1].index(max(ac[l+1:r-num+1])) + l+1
            i+=1
            ss = max(ss,a-ac[r]+ac[l])
    return ss


from itertools import accumulate

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)








