n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [_p-1 for _p in p]
c = list(map(int, input().split()))

ans = -float('inf')

visited = [False] * n
ss = []
for i in range(n):
    if visited[i]: continue
    cur = i
    s = []
    while not visited[cur]:
        visited[cur] = True
        s.append(c[cur])
        cur = p[cur]
    ss.append(s)

for s in ss:
    m = len(s)
    cumsum = [0] * (m*2+1)
    for i in range(2*m):
        cumsum[i+1] = cumsum[i] + s[i%m]
    
    amari = [-float('inf')] * m
    for i in range(m):
        for j in range(m):
            amari[j] = max(amari[j], cumsum[i+j] - cumsum[i])
    
    for r in range(0, m):
        if r > k: continue
        q = (k-r)//m
        if r==0 and q==0:
            continue
        
        if cumsum[m] > 0:
            ans = max(ans, amari[r] + cumsum[m]*q)
        elif r > 0:
            ans = max(ans, amari[r])

print(ans) n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k p = list(map(int, input().split())) p list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split _p-1 for _p in p for _p in p _p p for _p in p _p-1 _p 1 p = [_p-1 for _p in p] p [_p-1 for _p in p] c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = -float('inf') ans -float('inf') float('inf') float 'inf' visited = [False] * n visited [False] * n [False] False n ss = [] ss [] for i in range(n):
    if visited[i]: continue
    cur = i
    s = []
    while not visited[cur]:
        visited[cur] = True
        s.append(c[cur])
        cur = p[cur]
    ss.append(s) i range(n) range n if visited[i]: continue
     visited[i] visited i cur = i cur i s = [] s [] while not visited[cur]:
        visited[cur] = True
        s.append(c[cur])
        cur = p[cur]
     not visited[cur] visited[cur] visited cur visited[cur] = True visited[cur] visited cur True s.append(c[cur]) s.append s append c[cur] c cur cur = p[cur] cur p[cur] p cur ss.append(s) ss.append ss append s for s in ss:
    m = len(s)
    cumsum = [0] * (m*2+1)
    for i in range(2*m):
        cumsum[i+1] = cumsum[i] + s[i%m]
    
    amari = [-float('inf')] * m
    for i in range(m):
        for j in range(m):
            amari[j] = max(amari[j], cumsum[i+j] - cumsum[i])
    
    for r in range(0, m):
        if r > k: continue
        q = (k-r)//m
        if r==0 and q==0:
            continue
        
        if cumsum[m] > 0:
            ans = max(ans, amari[r] + cumsum[m]*q)
        elif r > 0:
            ans = max(ans, amari[r]) s ss m = len(s) m len(s) len s cumsum = [0] * (m*2+1) cumsum [0] * (m*2+1) [0] 0 m*2+1 m*2 m 2 1 for i in range(2*m):
        cumsum[i+1] = cumsum[i] + s[i%m]
    
     i range(2*m) range 2*m 2 m cumsum[i+1] = cumsum[i] + s[i%m] cumsum[i+1] cumsum i+1 i 1 cumsum[i] + s[i%m] cumsum[i] cumsum i s[i%m] s i%m i m amari = [-float('inf')] * m amari [-float('inf')] * m [-float('inf')] -float('inf') float('inf') float 'inf' m for i in range(m):
        for j in range(m):
            amari[j] = max(amari[j], cumsum[i+j] - cumsum[i])
    
     i range(m) range m for j in range(m):
            amari[j] = max(amari[j], cumsum[i+j] - cumsum[i])
    
     j range(m) range m amari[j] = max(amari[j], cumsum[i+j] - cumsum[i]) amari[j] amari j max(amari[j], cumsum[i+j] - cumsum[i]) max amari[j] amari j cumsum[i+j] - cumsum[i] cumsum[i+j] cumsum i+j i j cumsum[i] cumsum i for r in range(0, m):
        if r > k: continue
        q = (k-r)//m
        if r==0 and q==0:
            continue
        
        if cumsum[m] > 0:
            ans = max(ans, amari[r] + cumsum[m]*q)
        elif r > 0:
            ans = max(ans, amari[r]) r range(0, m) range 0 m if r > k: continue
         r > k r k q = (k-r)//m q (k-r)//m k-r k r m if r==0 and q==0:
            continue
        
         r==0 and q==0 r==0 r 0 q==0 q 0 continue if cumsum[m] > 0:
            ans = max(ans, amari[r] + cumsum[m]*q)
        elif r > 0:
            ans = max(ans, amari[r]) cumsum[m] > 0 cumsum[m] cumsum m 0 ans = max(ans, amari[r] + cumsum[m]*q) ans max(ans, amari[r] + cumsum[m]*q) max ans amari[r] + cumsum[m]*q amari[r] amari r cumsum[m]*q cumsum[m] cumsum m q elif r > 0:
            ans = max(ans, amari[r]) r > 0 r 0 ans = max(ans, amari[r]) ans max(ans, amari[r]) max ans amari[r] amari r print(ans) print ans p = [_p-1 for _p in p] [_p-1 for _p in p] p p = list(map(int, input().split())) list(map(int, input().split())) p k = map(int, input().split()) map(int, input().split()) k cur = i i cur s = [] [] s ans = max(ans, amari[r]) max(ans, amari[r]) ans c = list(map(int, input().split())) list(map(int, input().split())) c ans = -float('inf') -float('inf') ans cumsum = [0] * (m*2+1) [0] * (m*2+1) cumsum ans = max(ans, amari[r] + cumsum[m]*q) max(ans, amari[r] + cumsum[m]*q) ans visited = [False] * n [False] * n visited m = len(s) len(s) m amari = [-float('inf')] * m [-float('inf')] * m amari q = (k-r)//m (k-r)//m q cur = p[cur] p[cur] cur ss = [] [] ss n, k = map(int, input().split()) map(int, input().split()) n
n, k = map(int, input().split())
p = list(map(int, input().split()))
p = [_p-1 for _p in p]
c = list(map(int, input().split()))

ans = -float('inf')

visited = [False] * n
ss = []
for i in range(n):
    if visited[i]: continue
    cur = i
    s = []
    while not visited[cur]:
        visited[cur] = True
        s.append(c[cur])
        cur = p[cur]
    ss.append(s)

for s in ss:
    m = len(s)
    cumsum = [0] * (m*2+1)
    for i in range(2*m):
        cumsum[i+1] = cumsum[i] + s[i%m]
    
    amari = [-float('inf')] * m
    for i in range(m):
        for j in range(m):
            amari[j] = max(amari[j], cumsum[i+j] - cumsum[i])
    
    for r in range(0, m):
        if r > k: continue
        q = (k-r)//m
        if r==0 and q==0:
            continue
        
        if cumsum[m] > 0:
            ans = max(ans, amari[r] + cumsum[m]*q)
        elif r > 0:
            ans = max(ans, amari[r])

print(ans)
