n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [p - 1 for p in P]

def max_point(p, k):
    point = p
    visited = set()
    visited_record = []
    score = 0
    scores = []
    while point not in visited:
        visited.add(point)
        visited_record.append(point)
        score += C[point]
        scores.append(score)
        #update
        point = P[point]
    else:
        loop_entry_index = visited_record.index(point)
        loop_len = len(visited_record) - loop_entry_index
        if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
        head_len = loop_entry_index
    #print(scores)

    if k <= len(scores):
        return max(scores[:k])
    else:
        if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k))

point = -float("inf")
for p in P:
    point = max(max_point(p, k), point)
print(point) 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 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] def max_point(p, k):
    point = p
    visited = set()
    visited_record = []
    score = 0
    scores = []
    while point not in visited:
        visited.add(point)
        visited_record.append(point)
        score += C[point]
        scores.append(score)
        #update
        point = P[point]
    else:
        loop_entry_index = visited_record.index(point)
        loop_len = len(visited_record) - loop_entry_index
        if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
        head_len = loop_entry_index
    #print(scores)

    if k <= len(scores):
        return max(scores[:k])
    else:
        if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k)) max_point point = p point p visited = set() visited set() set visited_record = [] visited_record [] score = 0 score 0 scores = [] scores [] while point not in visited:
        visited.add(point)
        visited_record.append(point)
        score += C[point]
        scores.append(score)
        #update
        point = P[point]
    else:
        loop_entry_index = visited_record.index(point)
        loop_len = len(visited_record) - loop_entry_index
        if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
        head_len = loop_entry_index
    #print(scores)

     point not in visited point visited visited.add(point) visited.add visited add point visited_record.append(point) visited_record.append visited_record append point score += C[point] score C[point] C point scores.append(score) scores.append scores append score point = P[point] point P[point] P point loop_entry_index = visited_record.index(point) loop_entry_index visited_record.index(point) visited_record.index visited_record index point loop_len = len(visited_record) - loop_entry_index loop_len len(visited_record) - loop_entry_index len(visited_record) len visited_record loop_entry_index if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
         loop_entry_index > 0 loop_entry_index 0 loop_score = scores[-1] - scores[loop_entry_index-1] loop_score scores[-1] - scores[loop_entry_index-1] scores[-1] scores -1 scores[loop_entry_index-1] scores loop_entry_index-1 loop_entry_index 1 loop_score = scores[-1] loop_score scores[-1] scores -1 head_len = loop_entry_index head_len loop_entry_index if k <= len(scores):
        return max(scores[:k])
    else:
        if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k)) k <= len(scores) k len(scores) len scores return max(scores[:k]) max(scores[:k]) max scores[:k] scores :k k if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k)) loop_score > 0 loop_score 0 k -= head_len k head_len scores = scores[:head_len] scores scores[:head_len] scores :head_len head_len quotient = k // loop_len quotient k // loop_len k loop_len reminder = k % loop_len reminder k % loop_len k loop_len if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
             scores == [] scores [] score = 0 score 0 score = scores[head_len-1] score scores[head_len-1] scores head_len-1 head_len 1 score += (quotient - 1) * loop_score score (quotient - 1) * loop_score quotient - 1 quotient 1 loop_score point = visited_record[loop_entry_index] point visited_record[loop_entry_index] visited_record loop_entry_index for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
             i range(loop_len + reminder) range loop_len + reminder loop_len reminder score += C[point] score C[point] C point scores.append(score) scores.append scores append score point = P[point] point P[point] P point return max(scores) max(scores) max scores return max(scores) max(scores) max scores p p k k point = -float("inf") point -float("inf") float("inf") float "inf" for p in P:
    point = max(max_point(p, k), point) p P point = max(max_point(p, k), point) point max(max_point(p, k), point) max max_point(p, k) max_point p k point print(point) print point point = -float("inf") -float("inf") point n, k = map(int, input().split()) map(int, input().split()) n P = [p - 1 for p in P] [p - 1 for p in P] P point = max(max_point(p, k), point) max(max_point(p, k), point) point def max_point(p, k):
    point = p
    visited = set()
    visited_record = []
    score = 0
    scores = []
    while point not in visited:
        visited.add(point)
        visited_record.append(point)
        score += C[point]
        scores.append(score)
        #update
        point = P[point]
    else:
        loop_entry_index = visited_record.index(point)
        loop_len = len(visited_record) - loop_entry_index
        if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
        head_len = loop_entry_index
    #print(scores)

    if k <= len(scores):
        return max(scores[:k])
    else:
        if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k)) def max_point(p, k):
    point = p
    visited = set()
    visited_record = []
    score = 0
    scores = []
    while point not in visited:
        visited.add(point)
        visited_record.append(point)
        score += C[point]
        scores.append(score)
        #update
        point = P[point]
    else:
        loop_entry_index = visited_record.index(point)
        loop_len = len(visited_record) - loop_entry_index
        if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
        head_len = loop_entry_index
    #print(scores)

    if k <= len(scores):
        return max(scores[:k])
    else:
        if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k)) max_point C = list(map(int, input().split())) list(map(int, input().split())) C P = list(map(int, input().split())) list(map(int, input().split())) P k = map(int, input().split()) map(int, input().split()) k
n, k = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

P = [p - 1 for p in P]

def max_point(p, k):
    point = p
    visited = set()
    visited_record = []
    score = 0
    scores = []
    while point not in visited:
        visited.add(point)
        visited_record.append(point)
        score += C[point]
        scores.append(score)
        #update
        point = P[point]
    else:
        loop_entry_index = visited_record.index(point)
        loop_len = len(visited_record) - loop_entry_index
        if loop_entry_index > 0:
            loop_score = scores[-1] - scores[loop_entry_index-1]
        else:
            loop_score = scores[-1]
        head_len = loop_entry_index
    #print(scores)

    if k <= len(scores):
        return max(scores[:k])
    else:
        if loop_score > 0:
            k -= head_len
            scores = scores[:head_len]
            quotient = k // loop_len
            reminder = k % loop_len
            #print(quotient, reminder)
            if scores == []:
                score = 0
            else:
                score = scores[head_len-1]
            score += (quotient - 1) * loop_score
            point = visited_record[loop_entry_index]
            for i in range(loop_len + reminder):
                score += C[point]
                scores.append(score)
                #print(scores, point)
                point = P[point]
            return max(scores)
        else:
            #print(scores)
            return max(scores)

#print(max_point(0, k))

point = -float("inf")
for p in P:
    point = max(max_point(p, k), point)
print(point)
