import math

class doubling:
    def __init__(self, seni, max_jump, reward = None):
        self.depth = math.ceil(math.log2(max_jump))
        self.N = len(seni)
        self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
        self.jumps[0] = seni
        
        if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
        for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
    def calculation(self, now, move):
        count = move.bit_length()
        reward = 0
        for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
        return now, reward
#%%

N, K = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))
#%%

answer = -int(1e+50)

if K < N:
    minimum = K
else:
    minimum = N

for i in range(N):
    now2, reward2 = i, 0
    for j in range(minimum):
        reward2 = reward2 + C[now2]
        now2 = P[now2]
        if reward2 > answer:
            answer = reward2

consume = K-N
if consume > 0:
    dbl = doubling(P, consume, C)
    for i in range(N):
        now1, reward1 = dbl.calculation(i, consume)
        for j in range(N):
            reward1 = reward1 + C[now1]
            now1 = P[now1]
            if reward1 > answer:
                answer = reward1
print(answer) import math math class doubling:
    def __init__(self, seni, max_jump, reward = None):
        self.depth = math.ceil(math.log2(max_jump))
        self.N = len(seni)
        self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
        self.jumps[0] = seni
        
        if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
        for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
    def calculation(self, now, move):
        count = move.bit_length()
        reward = 0
        for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
        return now, reward
#%% doubling def __init__(self, seni, max_jump, reward = None):
        self.depth = math.ceil(math.log2(max_jump))
        self.N = len(seni)
        self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
        self.jumps[0] = seni
        
        if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
        for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
     __init__ self.depth = math.ceil(math.log2(max_jump)) self.depth self depth math.ceil(math.log2(max_jump)) math.ceil math ceil math.log2(max_jump) math.log2 math log2 max_jump self.N = len(seni) self.N self N len(seni) len seni [0 for i in range(self.N)] for _ in range(self.depth+1) for _ in range(self.depth+1) _ range(self.depth+1) range self.depth+1 self.depth self depth 1 for _ in range(self.depth+1) [0 for i in range(self.N)] self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)] self.jumps self jumps [[0 for i in range(self.N)] for _ in range(self.depth+1)] self.jumps[0] = seni self.jumps[0] self.jumps self jumps 0 seni if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
         reward [0 for i in range(self.N)] for _ in range(self.depth+1) for _ in range(self.depth+1) _ range(self.depth+1) range self.depth+1 self.depth self depth 1 for _ in range(self.depth+1) [0 for i in range(self.N)] self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)] self.rewards self rewards [[0 for i in range(self.N)] for _ in range(self.depth+1)] self.rewards[0] = reward self.rewards[0] self.rewards self rewards 0 reward for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
     i range(self.depth) range self.depth self depth for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
     j range(self.N) range self.N self N self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]] self.jumps[i+1][j] self.jumps[i+1] self.jumps self jumps i+1 i 1 j self.jumps[i][self.jumps[i][j]] self.jumps[i] self.jumps self jumps i self.jumps[i][j] self.jumps[i] self.jumps self jumps i j if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
     reward self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]] self.rewards[i+1][j] self.rewards[i+1] self.rewards self rewards i+1 i 1 j self.rewards[i][j] + self.rewards[i][self.jumps[i][j]] self.rewards[i][j] self.rewards[i] self.rewards self rewards i j self.rewards[i][self.jumps[i][j]] self.rewards[i] self.rewards self rewards i self.jumps[i][j] self.jumps[i] self.jumps self jumps i j self self seni seni max_jump max_jump reward = None reward None def calculation(self, now, move):
        count = move.bit_length()
        reward = 0
        for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
        return now, reward
#%% calculation count = move.bit_length() count move.bit_length() move.bit_length move bit_length reward = 0 reward 0 for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
         i range(count) range count if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
         move & 1<<i == 1<<i move & 1<<i move 1<<i 1 i 1<<i 1 i if self.rewards:
                    reward += self.rewards[i][now]
                 self.rewards self rewards reward += self.rewards[i][now] reward self.rewards[i][now] self.rewards[i] self.rewards self rewards i now now = self.jumps[i][now] now self.jumps[i][now] self.jumps[i] self.jumps self jumps i now return now, reward return now, reward now reward self self now now move move N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K P = list(map(lambda x: int(x)-1, input().split())) P list(map(lambda x: int(x)-1, input().split())) list map(lambda x: int(x)-1, input().split()) map lambda x: int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split C = list(map(int, input().split())) C list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split answer = -int(1e+50) answer -int(1e+50) int(1e+50) int 1e+50 if K < N:
    minimum = K
else:
    minimum = N K < N K N minimum = K minimum K minimum = N minimum N for i in range(N):
    now2, reward2 = i, 0
    for j in range(minimum):
        reward2 = reward2 + C[now2]
        now2 = P[now2]
        if reward2 > answer:
            answer = reward2 i range(N) range N now2, reward2 = i, 0 now2 i reward2 0 for j in range(minimum):
        reward2 = reward2 + C[now2]
        now2 = P[now2]
        if reward2 > answer:
            answer = reward2 j range(minimum) range minimum reward2 = reward2 + C[now2] reward2 reward2 + C[now2] reward2 C[now2] C now2 now2 = P[now2] now2 P[now2] P now2 if reward2 > answer:
            answer = reward2 reward2 > answer reward2 answer answer = reward2 answer reward2 consume = K-N consume K-N K N if consume > 0:
    dbl = doubling(P, consume, C)
    for i in range(N):
        now1, reward1 = dbl.calculation(i, consume)
        for j in range(N):
            reward1 = reward1 + C[now1]
            now1 = P[now1]
            if reward1 > answer:
                answer = reward1 consume > 0 consume 0 dbl = doubling(P, consume, C) dbl doubling(P, consume, C) doubling P consume C for i in range(N):
        now1, reward1 = dbl.calculation(i, consume)
        for j in range(N):
            reward1 = reward1 + C[now1]
            now1 = P[now1]
            if reward1 > answer:
                answer = reward1 i range(N) range N now1, reward1 = dbl.calculation(i, consume) now1 dbl.calculation(i, consume) dbl.calculation dbl calculation i consume reward1 for j in range(N):
            reward1 = reward1 + C[now1]
            now1 = P[now1]
            if reward1 > answer:
                answer = reward1 j range(N) range N reward1 = reward1 + C[now1] reward1 reward1 + C[now1] reward1 C[now1] C now1 now1 = P[now1] now1 P[now1] P now1 if reward1 > answer:
                answer = reward1 reward1 > answer reward1 answer answer = reward1 answer reward1 print(answer) print answer K = map(int, input().split()) map(int, input().split()) K now1, reward1 = dbl.calculation(i, consume) dbl.calculation(i, consume) now1 reward1 = dbl.calculation(i, consume) dbl.calculation(i, consume) reward1 answer = reward1 reward1 answer C = list(map(int, input().split())) list(map(int, input().split())) C dbl = doubling(P, consume, C) doubling(P, consume, C) dbl reward2 = i, 0 0 reward2 consume = K-N K-N consume N, K = map(int, input().split()) map(int, input().split()) N class doubling:
    def __init__(self, seni, max_jump, reward = None):
        self.depth = math.ceil(math.log2(max_jump))
        self.N = len(seni)
        self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
        self.jumps[0] = seni
        
        if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
        for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
    def calculation(self, now, move):
        count = move.bit_length()
        reward = 0
        for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
        return now, reward
#%% class doubling:
    def __init__(self, seni, max_jump, reward = None):
        self.depth = math.ceil(math.log2(max_jump))
        self.N = len(seni)
        self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
        self.jumps[0] = seni
        
        if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
        for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
    def calculation(self, now, move):
        count = move.bit_length()
        reward = 0
        for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
        return now, reward
#%% doubling minimum = N N minimum answer = -int(1e+50) -int(1e+50) answer answer = reward2 reward2 answer now1 = P[now1] P[now1] now1 reward2 = reward2 + C[now2] reward2 + C[now2] reward2 minimum = K K minimum P = list(map(lambda x: int(x)-1, input().split())) list(map(lambda x: int(x)-1, input().split())) P now2, reward2 = i, 0 i now2 now2 = P[now2] P[now2] now2 reward1 = reward1 + C[now1] reward1 + C[now1] reward1
import math

class doubling:
    def __init__(self, seni, max_jump, reward = None):
        self.depth = math.ceil(math.log2(max_jump))
        self.N = len(seni)
        self.jumps = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
        self.jumps[0] = seni
        
        if reward:
            self.rewards = [[0 for i in range(self.N)] for _ in range(self.depth+1)]
            self.rewards[0] = reward
            
        for i in range(self.depth):
            for j in range(self.N):
                self.jumps[i+1][j] = self.jumps[i][self.jumps[i][j]]
                if reward:
                    self.rewards[i+1][j] = self.rewards[i][j] + self.rewards[i][self.jumps[i][j]]
    def calculation(self, now, move):
        count = move.bit_length()
        reward = 0
        for i in range(count):
            if move & 1<<i == 1<<i:
                if self.rewards:
                    reward += self.rewards[i][now]
                now = self.jumps[i][now]
        return now, reward
#%%

N, K = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))
#%%

answer = -int(1e+50)

if K < N:
    minimum = K
else:
    minimum = N

for i in range(N):
    now2, reward2 = i, 0
    for j in range(minimum):
        reward2 = reward2 + C[now2]
        now2 = P[now2]
        if reward2 > answer:
            answer = reward2

consume = K-N
if consume > 0:
    dbl = doubling(P, consume, C)
    for i in range(N):
        now1, reward1 = dbl.calculation(i, consume)
        for j in range(N):
            reward1 = reward1 + C[now1]
            now1 = P[now1]
            if reward1 > answer:
                answer = reward1
print(answer)