N, K = map(int,input().split())
P = [int(x)-1 for x in input().split()]
C = [int(x) for x in input().split()]

def loop(s):
    value = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        value += C[s]
    return len(done), value

def f(s,K):
    ans = -10**18
    total = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1

        if K==0:
            return ans

    lamb, value = loop(s)

    if K > 2*lamb:
        if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
    if value <= 0:
        K = min(K,lamb+5)
        


    while K > 0:
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1
    return ans

print(max([f(s,K) for s in range(N)])) N, K = map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K int(x)-1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x)-1 int(x) int x 1 P = [int(x)-1 for x in input().split()] P [int(x)-1 for x in input().split()] int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x C = [int(x) for x in input().split()] C [int(x) for x in input().split()] def loop(s):
    value = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        value += C[s]
    return len(done), value loop value = 0 value 0 done = set() done set() set while s not in done:
        done.add(s)
        s = P[s]
        value += C[s]
     s not in done s done done.add(s) done.add done add s s = P[s] s P[s] P s value += C[s] value C[s] C s return len(done), value return len(done), value len(done) len done value s s def f(s,K):
    ans = -10**18
    total = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1

        if K==0:
            return ans

    lamb, value = loop(s)

    if K > 2*lamb:
        if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
    if value <= 0:
        K = min(K,lamb+5)
        


    while K > 0:
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1
    return ans f ans = -10**18 ans -10**18 -10 18 total = 0 total 0 done = set() done set() set while s not in done:
        done.add(s)
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1

        if K==0:
            return ans

     s not in done s done done.add(s) done.add done add s s = P[s] s P[s] P s total += C[s] total C[s] C s ans = max(ans,total) ans max(ans,total) max ans total K -= 1 K 1 if K==0:
            return ans

     K==0 K 0 return ans ans lamb, value = loop(s) lamb loop(s) loop s value if K > 2*lamb:
        if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
     K > 2*lamb K 2*lamb 2 lamb if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
     value > 0 value 0 K -= lamb K lamb total += (K//lamb)*value total (K//lamb)*value K//lamb K lamb value ans = max(ans,total) ans max(ans,total) max ans total K -= (K//lamb)*lamb K (K//lamb)*lamb K//lamb K lamb lamb K += lamb K lamb if value <= 0:
        K = min(K,lamb+5)
        


     value <= 0 value 0 K = min(K,lamb+5) K min(K,lamb+5) min K lamb+5 lamb 5 while K > 0:
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1
     K > 0 K 0 s = P[s] s P[s] P s total += C[s] total C[s] C s ans = max(ans,total) ans max(ans,total) max ans total K -= 1 K 1 return ans ans s s K K f(s,K) for s in range(N) for s in range(N) s range(N) range N for s in range(N) f(s,K) f s K print(max([f(s,K) for s in range(N)])) print max([f(s,K) for s in range(N)]) max [f(s,K) for s in range(N)] C = [int(x) for x in input().split()] [int(x) for x in input().split()] C N, K = map(int,input().split()) map(int,input().split()) N P = [int(x)-1 for x in input().split()] [int(x)-1 for x in input().split()] P K = map(int,input().split()) map(int,input().split()) K def loop(s):
    value = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        value += C[s]
    return len(done), value def loop(s):
    value = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        value += C[s]
    return len(done), value loop def f(s,K):
    ans = -10**18
    total = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1

        if K==0:
            return ans

    lamb, value = loop(s)

    if K > 2*lamb:
        if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
    if value <= 0:
        K = min(K,lamb+5)
        


    while K > 0:
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1
    return ans def f(s,K):
    ans = -10**18
    total = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1

        if K==0:
            return ans

    lamb, value = loop(s)

    if K > 2*lamb:
        if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
    if value <= 0:
        K = min(K,lamb+5)
        


    while K > 0:
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1
    return ans f
N, K = map(int,input().split())
P = [int(x)-1 for x in input().split()]
C = [int(x) for x in input().split()]

def loop(s):
    value = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        value += C[s]
    return len(done), value

def f(s,K):
    ans = -10**18
    total = 0
    done = set()
    while s not in done:
        done.add(s)
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1

        if K==0:
            return ans

    lamb, value = loop(s)

    if K > 2*lamb:
        if value > 0:
            K -= lamb
            total += (K//lamb)*value
            ans = max(ans,total)
            K -= (K//lamb)*lamb
            K += lamb
    if value <= 0:
        K = min(K,lamb+5)
        


    while K > 0:
        s = P[s]
        total += C[s]
        ans = max(ans,total)
        K -= 1
    return ans

print(max([f(s,K) for s in range(N)]))