n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))


def get_sum(array):
    score_array = [0]
    for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
    return max(score_array)

def get_max(array, q, mod):
    if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
    return m_score

max_score = -float('inf')

for i in range(n):
    score = 0
    score_mv_array = []
    pos = i
    start_pos = p[pos]-1
    for j in range(k):
        pos = p[pos]-1
        if j!=0 and pos == start_pos:
            len_mv = j
            q, mod = divmod(k-j, len_mv)
            score += get_max(score_mv_array, q, mod)
            max_score = max(score, max_score)
            break
        score += c[pos]
        score_mv_array.append(c[pos])
        max_score = max(score, max_score)

print(max_score) 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 c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split def get_sum(array):
    score_array = [0]
    for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
    return max(score_array) get_sum score_array = [0] score_array [0] 0 for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
     i a enumerate(array) enumerate array score_array.append(score_array[i]+a) score_array.append score_array append score_array[i]+a score_array[i] score_array i a return max(score_array) max(score_array) max score_array array array def get_max(array, q, mod):
    if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
    return m_score get_max if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
     q == 0 q 0 m_score = get_sum(array[:mod]) m_score get_sum(array[:mod]) get_sum array[:mod] array :mod mod m_score = get_sum(array+array[:mod]) m_score get_sum(array+array[:mod]) get_sum array+array[:mod] array array[:mod] array :mod mod m_score = sum(array)*(q-1) + m_score m_score sum(array)*(q-1) + m_score sum(array)*(q-1) sum(array) sum array q-1 q 1 m_score return m_score m_score array array q q mod mod max_score = -float('inf') max_score -float('inf') float('inf') float 'inf' for i in range(n):
    score = 0
    score_mv_array = []
    pos = i
    start_pos = p[pos]-1
    for j in range(k):
        pos = p[pos]-1
        if j!=0 and pos == start_pos:
            len_mv = j
            q, mod = divmod(k-j, len_mv)
            score += get_max(score_mv_array, q, mod)
            max_score = max(score, max_score)
            break
        score += c[pos]
        score_mv_array.append(c[pos])
        max_score = max(score, max_score) i range(n) range n score = 0 score 0 score_mv_array = [] score_mv_array [] pos = i pos i start_pos = p[pos]-1 start_pos p[pos]-1 p[pos] p pos 1 for j in range(k):
        pos = p[pos]-1
        if j!=0 and pos == start_pos:
            len_mv = j
            q, mod = divmod(k-j, len_mv)
            score += get_max(score_mv_array, q, mod)
            max_score = max(score, max_score)
            break
        score += c[pos]
        score_mv_array.append(c[pos])
        max_score = max(score, max_score) j range(k) range k pos = p[pos]-1 pos p[pos]-1 p[pos] p pos 1 if j!=0 and pos == start_pos:
            len_mv = j
            q, mod = divmod(k-j, len_mv)
            score += get_max(score_mv_array, q, mod)
            max_score = max(score, max_score)
            break
         j!=0 and pos == start_pos j!=0 j 0 pos == start_pos pos start_pos len_mv = j len_mv j q, mod = divmod(k-j, len_mv) q divmod(k-j, len_mv) divmod k-j k j len_mv mod score += get_max(score_mv_array, q, mod) score get_max(score_mv_array, q, mod) get_max score_mv_array q mod max_score = max(score, max_score) max_score max(score, max_score) max score max_score break score += c[pos] score c[pos] c pos score_mv_array.append(c[pos]) score_mv_array.append score_mv_array append c[pos] c pos max_score = max(score, max_score) max_score max(score, max_score) max score max_score print(max_score) print max_score max_score = -float('inf') -float('inf') max_score p = list(map(int, input().split())) list(map(int, input().split())) p def get_sum(array):
    score_array = [0]
    for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
    return max(score_array) def get_sum(array):
    score_array = [0]
    for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
    return max(score_array) get_sum len_mv = j j len_mv max_score = max(score, max_score) max(score, max_score) max_score score += c[pos] c[pos] score score_mv_array = [] [] score_mv_array k = map(int, input().split()) map(int, input().split()) k q, mod = divmod(k-j, len_mv) divmod(k-j, len_mv) q start_pos = p[pos]-1 p[pos]-1 start_pos pos = p[pos]-1 p[pos]-1 pos score += get_max(score_mv_array, q, mod) get_max(score_mv_array, q, mod) score n, k = map(int, input().split()) map(int, input().split()) n c = list(map(int, input().split())) list(map(int, input().split())) c mod = divmod(k-j, len_mv) divmod(k-j, len_mv) mod def get_max(array, q, mod):
    if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
    return m_score def get_max(array, q, mod):
    if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
    return m_score get_max score = 0 0 score pos = i i pos max_score = max(score, max_score) max(score, max_score) max_score
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))


def get_sum(array):
    score_array = [0]
    for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
    return max(score_array)

def get_max(array, q, mod):
    if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
    return m_score

max_score = -float('inf')

for i in range(n):
    score = 0
    score_mv_array = []
    pos = i
    start_pos = p[pos]-1
    for j in range(k):
        pos = p[pos]-1
        if j!=0 and pos == start_pos:
            len_mv = j
            q, mod = divmod(k-j, len_mv)
            score += get_max(score_mv_array, q, mod)
            max_score = max(score, max_score)
            break
        score += c[pos]
        score_mv_array.append(c[pos])
        max_score = max(score, max_score)

print(max_score)