
n, k = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
c = [int(x) for x in input().split()]
pow_num = 31
point = [[0] * n for i in range(pow_num)]
point_max = [[0] * n for i in range(pow_num)]
posi = [[0] * n for i in range(pow_num)]

c_set = set(c)
if all([i < 0 for i in c_set]):
    print(max(c_set))
else:
    for i in range(pow_num):
        if i == 0:
            for j in range(n):
                posi[i][j] = p[j]
                point[i][j] = c[p[j]]
            continue
        for j in range(n):
            posi[i][j] = posi[i-1][posi[i-1][j]]
            point[i][j] = point[i-1][j] + point[i-1][posi[i-1][j]]
    for i in range(pow_num):
        if i == 0:
            for j in range(n):
                point_max[i][j] = point[i][j]
            continue
        for j in range(n):
            point_max[i][j] = max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]])

    power_count = list()
    for i in range(pow_num):
        if k & (1 << i):
            power_count.append(i)

        ans = 0
        for i in range(n):
            next_posi = i
            cumsum_point = 0
            for posi_idx in power_count:
                if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                cumsum_point += point[posi_idx][next_posi]
                next_posi = posi[posi_idx][next_posi]

    print(ans) 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()] pow_num = 31 pow_num 31 [0] * n for i in range(pow_num) for i in range(pow_num) i range(pow_num) range pow_num for i in range(pow_num) [0] * n [0] 0 n point = [[0] * n for i in range(pow_num)] point [[0] * n for i in range(pow_num)] [0] * n for i in range(pow_num) for i in range(pow_num) i range(pow_num) range pow_num for i in range(pow_num) [0] * n [0] 0 n point_max = [[0] * n for i in range(pow_num)] point_max [[0] * n for i in range(pow_num)] [0] * n for i in range(pow_num) for i in range(pow_num) i range(pow_num) range pow_num for i in range(pow_num) [0] * n [0] 0 n posi = [[0] * n for i in range(pow_num)] posi [[0] * n for i in range(pow_num)] c_set = set(c) c_set set(c) set c if all([i < 0 for i in c_set]):
    print(max(c_set))
else:
    for i in range(pow_num):
        if i == 0:
            for j in range(n):
                posi[i][j] = p[j]
                point[i][j] = c[p[j]]
            continue
        for j in range(n):
            posi[i][j] = posi[i-1][posi[i-1][j]]
            point[i][j] = point[i-1][j] + point[i-1][posi[i-1][j]]
    for i in range(pow_num):
        if i == 0:
            for j in range(n):
                point_max[i][j] = point[i][j]
            continue
        for j in range(n):
            point_max[i][j] = max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]])

    power_count = list()
    for i in range(pow_num):
        if k & (1 << i):
            power_count.append(i)

        ans = 0
        for i in range(n):
            next_posi = i
            cumsum_point = 0
            for posi_idx in power_count:
                if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                cumsum_point += point[posi_idx][next_posi]
                next_posi = posi[posi_idx][next_posi]

    print(ans) all([i < 0 for i in c_set]) all [i < 0 for i in c_set] print(max(c_set)) print max(c_set) max c_set for i in range(pow_num):
        if i == 0:
            for j in range(n):
                posi[i][j] = p[j]
                point[i][j] = c[p[j]]
            continue
        for j in range(n):
            posi[i][j] = posi[i-1][posi[i-1][j]]
            point[i][j] = point[i-1][j] + point[i-1][posi[i-1][j]]
     i range(pow_num) range pow_num if i == 0:
            for j in range(n):
                posi[i][j] = p[j]
                point[i][j] = c[p[j]]
            continue
         i == 0 i 0 for j in range(n):
                posi[i][j] = p[j]
                point[i][j] = c[p[j]]
             j range(n) range n posi[i][j] = p[j] posi[i][j] [i] posi i j p[j] p j point[i][j] = c[p[j]] point[i][j] [i] point i j c[p[j]] c p[j] p j continue for j in range(n):
            posi[i][j] = posi[i-1][posi[i-1][j]]
            point[i][j] = point[i-1][j] + point[i-1][posi[i-1][j]]
     j range(n) range n posi[i][j] = posi[i-1][posi[i-1][j]] posi[i][j] [i] posi i j posi[i-1][posi[i-1][j]] [i-1] posi i-1 i 1 posi[i-1][j] [i-1] posi i-1 i 1 j point[i][j] = point[i-1][j] + point[i-1][posi[i-1][j]] point[i][j] [i] point i j point[i-1][j] + point[i-1][posi[i-1][j]] point[i-1][j] [i-1] point i-1 i 1 j point[i-1][posi[i-1][j]] [i-1] point i-1 i 1 posi[i-1][j] [i-1] posi i-1 i 1 j for i in range(pow_num):
        if i == 0:
            for j in range(n):
                point_max[i][j] = point[i][j]
            continue
        for j in range(n):
            point_max[i][j] = max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]])

     i range(pow_num) range pow_num if i == 0:
            for j in range(n):
                point_max[i][j] = point[i][j]
            continue
         i == 0 i 0 for j in range(n):
                point_max[i][j] = point[i][j]
             j range(n) range n point_max[i][j] = point[i][j] point_max[i][j] [i] point_max i j point[i][j] [i] point i j continue for j in range(n):
            point_max[i][j] = max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]])

     j range(n) range n point_max[i][j] = max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]]) point_max[i][j] [i] point_max i j max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]]) max point_max[i-1][j] [i-1] point_max i-1 i 1 j point[i-1][j] + point_max[i-1][posi[i-1][j]] point[i-1][j] [i-1] point i-1 i 1 j point_max[i-1][posi[i-1][j]] [i-1] point_max i-1 i 1 posi[i-1][j] [i-1] posi i-1 i 1 j power_count = list() power_count list() list for i in range(pow_num):
        if k & (1 << i):
            power_count.append(i)

        ans = 0
        for i in range(n):
            next_posi = i
            cumsum_point = 0
            for posi_idx in power_count:
                if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                cumsum_point += point[posi_idx][next_posi]
                next_posi = posi[posi_idx][next_posi]

     i range(pow_num) range pow_num if k & (1 << i):
            power_count.append(i)

         k & (1 << i) k 1 << i 1 i power_count.append(i) power_count.append power_count append i ans = 0 ans 0 for i in range(n):
            next_posi = i
            cumsum_point = 0
            for posi_idx in power_count:
                if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                cumsum_point += point[posi_idx][next_posi]
                next_posi = posi[posi_idx][next_posi]

     i range(n) range n next_posi = i next_posi i cumsum_point = 0 cumsum_point 0 for posi_idx in power_count:
                if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                cumsum_point += point[posi_idx][next_posi]
                next_posi = posi[posi_idx][next_posi]

     posi_idx power_count if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                 ans < cumsum_point + point_max[posi_idx][next_posi] ans cumsum_point + point_max[posi_idx][next_posi] cumsum_point point_max[posi_idx][next_posi] [posi_idx] point_max posi_idx next_posi ans = cumsum_point + point_max[posi_idx][next_posi] ans cumsum_point + point_max[posi_idx][next_posi] cumsum_point point_max[posi_idx][next_posi] [posi_idx] point_max posi_idx next_posi cumsum_point += point[posi_idx][next_posi] cumsum_point point[posi_idx][next_posi] [posi_idx] point posi_idx next_posi next_posi = posi[posi_idx][next_posi] next_posi posi[posi_idx][next_posi] [posi_idx] posi posi_idx next_posi print(ans) print ans next_posi = posi[posi_idx][next_posi] posi[posi_idx][next_posi] next_posi point_max = [[0] * n for i in range(pow_num)] [[0] * n for i in range(pow_num)] point_max ans = 0 0 ans p = [int(x) - 1 for x in input().split()] [int(x) - 1 for x in input().split()] p cumsum_point = 0 0 cumsum_point k = map(int, input().split()) map(int, input().split()) k ans = cumsum_point + point_max[posi_idx][next_posi] cumsum_point + point_max[posi_idx][next_posi] ans point = [[0] * n for i in range(pow_num)] [[0] * n for i in range(pow_num)] point power_count = list() list() power_count c_set = set(c) set(c) c_set cumsum_point += point[posi_idx][next_posi] point[posi_idx][next_posi] cumsum_point n, k = map(int, input().split()) map(int, input().split()) n pow_num = 31 31 pow_num next_posi = i i next_posi c = [int(x) for x in input().split()] [int(x) for x in input().split()] c posi = [[0] * n for i in range(pow_num)] [[0] * n for i in range(pow_num)] posi

n, k = map(int, input().split())
p = [int(x) - 1 for x in input().split()]
c = [int(x) for x in input().split()]
pow_num = 31
point = [[0] * n for i in range(pow_num)]
point_max = [[0] * n for i in range(pow_num)]
posi = [[0] * n for i in range(pow_num)]

c_set = set(c)
if all([i < 0 for i in c_set]):
    print(max(c_set))
else:
    for i in range(pow_num):
        if i == 0:
            for j in range(n):
                posi[i][j] = p[j]
                point[i][j] = c[p[j]]
            continue
        for j in range(n):
            posi[i][j] = posi[i-1][posi[i-1][j]]
            point[i][j] = point[i-1][j] + point[i-1][posi[i-1][j]]
    for i in range(pow_num):
        if i == 0:
            for j in range(n):
                point_max[i][j] = point[i][j]
            continue
        for j in range(n):
            point_max[i][j] = max(point_max[i-1][j], \
                point[i-1][j] + point_max[i-1][posi[i-1][j]])

    power_count = list()
    for i in range(pow_num):
        if k & (1 << i):
            power_count.append(i)

        ans = 0
        for i in range(n):
            next_posi = i
            cumsum_point = 0
            for posi_idx in power_count:
                if ans < cumsum_point + point_max[posi_idx][next_posi]:
                    ans = cumsum_point + point_max[posi_idx][next_posi]
                cumsum_point += point[posi_idx][next_posi]
                next_posi = posi[posi_idx][next_posi]

    print(ans)