class Unionfind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def root (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def isConnect(self, x):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parents[self.root(x)]

def solve():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x) - 1, input().split()))
    C = list(map(int, input().split()))
    INF = 10 ** 18
    dp = [[-INF] * (N + 1) for i in range(N)]
    ans = -INF
    uf = Unionfind(N)
    for i in range(N):
        dp[i][0] = 0
        now = i
        for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
    for i in range(N):
        M = uf.size(i)
        if dp[i][M] < 0:
            continue
        roop = K // M
        move = K % M
        if move == 0:
            roop -= 1
            move = M
        for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
    return ans

print(solve()) class Unionfind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def root (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def isConnect(self, x):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parents[self.root(x)] Unionfind def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

     __init__ self.n = n self.n self n n self.parents = [-1] * n self.parents self parents [-1] * n [-1] -1 n self self n n def root (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

     root if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

     self.parents[x] < 0 self.parents[x] self.parents self parents x 0 return x x self.parents[x] = self.root(self.parents[x]) self.parents[x] self.parents self parents x self.root(self.parents[x]) self.root self root self.parents[x] self.parents self parents x return self.parents[x] self.parents[x] self.parents self parents x self self x x def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

     connect x = self.root(x) x self.root(x) self.root self root x y = self.root(y) y self.root(y) self.root self root y if x == y:
            return
         x == y x y return if self.parents[x] > self.parents[y]:
            x, y = y, x
         self.parents[x] > self.parents[y] self.parents[x] self.parents self parents x self.parents[y] self.parents self parents y x, y = y, x x y y x self.parents[x] += self.parents[y] self.parents[x] self.parents self parents x self.parents[y] self.parents self parents y self.parents[y] = x self.parents[y] self.parents self parents y x self self x x y y def isConnect(self, x):
        return self.root(x) == self.root(y)

     isConnect return self.root(x) == self.root(y) self.root(x) == self.root(y) self.root(x) self.root self root x self.root(y) self.root self root y self self x x def size(self, x):
        return -self.parents[self.root(x)] size return -self.parents[self.root(x)] -self.parents[self.root(x)] self.parents[self.root(x)] self.parents self parents self.root(x) self.root self root x self self x x def solve():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x) - 1, input().split()))
    C = list(map(int, input().split()))
    INF = 10 ** 18
    dp = [[-INF] * (N + 1) for i in range(N)]
    ans = -INF
    uf = Unionfind(N)
    for i in range(N):
        dp[i][0] = 0
        now = i
        for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
    for i in range(N):
        M = uf.size(i)
        if dp[i][M] < 0:
            continue
        roop = K // M
        move = K % M
        if move == 0:
            roop -= 1
            move = M
        for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
    return ans solve 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 INF = 10 ** 18 INF 10 ** 18 10 18 [-INF] * (N + 1) for i in range(N) for i in range(N) i range(N) range N for i in range(N) [-INF] * (N + 1) [-INF] -INF INF N + 1 N 1 dp = [[-INF] * (N + 1) for i in range(N)] dp [[-INF] * (N + 1) for i in range(N)] ans = -INF ans -INF INF uf = Unionfind(N) uf Unionfind(N) Unionfind N for i in range(N):
        dp[i][0] = 0
        now = i
        for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
     i range(N) range N dp[i][0] = 0 dp[i][0] [i] dp i 0 0 now = i now i for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
     j range(min(N, K)) range min(N, K) min N K now = P[now] now P[now] P now uf.connect(i, now) uf.connect uf connect i now dp[i][j + 1] = dp[i][j] + C[now] dp[i][j + 1] [i] dp i j + 1 j 1 dp[i][j] + C[now] dp[i][j] [i] dp i j C[now] C now ans = max(ans, dp[i][j + 1]) ans max(ans, dp[i][j + 1]) max ans dp[i][j + 1] [i] dp i j + 1 j 1 for i in range(N):
        M = uf.size(i)
        if dp[i][M] < 0:
            continue
        roop = K // M
        move = K % M
        if move == 0:
            roop -= 1
            move = M
        for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
     i range(N) range N M = uf.size(i) M uf.size(i) uf.size uf size i if dp[i][M] < 0:
            continue
         dp[i][M] < 0 dp[i][M] [i] dp i M 0 continue roop = K // M roop K // M K M move = K % M move K % M K M if move == 0:
            roop -= 1
            move = M
         move == 0 move 0 roop -= 1 roop 1 move = M move M for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
     j range(move) range move ans = max(ans, dp[i][M] * roop + dp[i][j + 1]) ans max(ans, dp[i][M] * roop + dp[i][j + 1]) max ans dp[i][M] * roop + dp[i][j + 1] dp[i][M] * roop dp[i][M] [i] dp i M roop dp[i][j + 1] [i] dp i j + 1 j 1 return ans ans print(solve()) print solve() solve def solve():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x) - 1, input().split()))
    C = list(map(int, input().split()))
    INF = 10 ** 18
    dp = [[-INF] * (N + 1) for i in range(N)]
    ans = -INF
    uf = Unionfind(N)
    for i in range(N):
        dp[i][0] = 0
        now = i
        for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
    for i in range(N):
        M = uf.size(i)
        if dp[i][M] < 0:
            continue
        roop = K // M
        move = K % M
        if move == 0:
            roop -= 1
            move = M
        for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
    return ans def solve():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x) - 1, input().split()))
    C = list(map(int, input().split()))
    INF = 10 ** 18
    dp = [[-INF] * (N + 1) for i in range(N)]
    ans = -INF
    uf = Unionfind(N)
    for i in range(N):
        dp[i][0] = 0
        now = i
        for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
    for i in range(N):
        M = uf.size(i)
        if dp[i][M] < 0:
            continue
        roop = K // M
        move = K % M
        if move == 0:
            roop -= 1
            move = M
        for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
    return ans solve class Unionfind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def root (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def isConnect(self, x):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parents[self.root(x)] class Unionfind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def root (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def isConnect(self, x):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parents[self.root(x)] Unionfind
class Unionfind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def root (self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

    def connect(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def isConnect(self, x):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parents[self.root(x)]

def solve():
    N, K = map(int, input().split())
    P = list(map(lambda x: int(x) - 1, input().split()))
    C = list(map(int, input().split()))
    INF = 10 ** 18
    dp = [[-INF] * (N + 1) for i in range(N)]
    ans = -INF
    uf = Unionfind(N)
    for i in range(N):
        dp[i][0] = 0
        now = i
        for j in range(min(N, K)):
            now = P[now]
            uf.connect(i, now)
            dp[i][j + 1] = dp[i][j] + C[now]
            ans = max(ans, dp[i][j + 1])
    for i in range(N):
        M = uf.size(i)
        if dp[i][M] < 0:
            continue
        roop = K // M
        move = K % M
        if move == 0:
            roop -= 1
            move = M
        for j in range(move):
            ans = max(ans, dp[i][M] * roop + dp[i][j + 1])
    return ans

print(solve())
