Buggy python submission (verdict: Time Limit Exceeded):
def main():

    N,K = map(int,input().split())
    P = list(map(int,input().split()))
    C = list(map(int,input().split()))

    # グルーピング
    import copy
    P2 = set(P)
    Group = []
    while len(P2)>0:
        temp = list(P2)[0]
        tempGroup = []
        while temp in P2:
            tempGroup.append(temp)
            P2.remove(temp)
            temp = P[temp-1]
        Group.append(tempGroup)
    Point = []
    for x in Group:
        tempPoint = []
        for p in x:
            tempPoint.append(C[p-1])
        Point.append(tempPoint)

    #print(Group)
    #print(Point)

    max00 = -10**9
    for x in Point:
        sumx = sum(x)
        tempx = copy.copy(x)
        tempx.extend(x)
        lenx = len(x)
        max0 = -10**9
        dp = [0]*lenx
        if sumx <= 0 or K < lenx:
            for i in range(min(lenx-1,K)):
                for j in range(lenx):
                    dp[j] = dp[j] + tempx[j+i]
                max0 = max(max0,max(dp))
                #print(dp)
        else:
            K2 = K % lenx
            SN = int(K/lenx)
            for i in range(lenx):
                for j in range(lenx):
                    dp[j] = dp[j] + tempx[j+i]
                #print(dp)
                if i < K2:
                    max0 = max(max0,max(dp) + sumx*SN)
                    #print(max0)
                else:
                    max0 = max(max0,max(dp) + sumx*(SN-1))
        max00 = max(max00,max0)
    print(max00)

if __name__ == "__main__":
    main()


Fixed submission:
def main():
 
    N,K = map(int,input().split())
    P = list(map(int,input().split()))
    C = list(map(int,input().split()))
 
    # グルーピング
    import copy
    P2 = set(P)
    Group = []
    while len(P2)>0:
        temp = list(P2)[0]
        tempGroup = []
        while temp in P2:
            tempGroup.append(temp)
            P2.remove(temp)
            temp = P[temp-1]
        Group.append(tempGroup)
    Point = []
    for x in Group:
        tempPoint = []
        for p in x:
            tempPoint.append(C[p-1])
        Point.append(tempPoint)
 
    #print(Group)
    #print(Point)
 
    max00 = -10**9
    for x in Point:
        sumx = sum(x)
        tempx = copy.copy(x)
        tempx.extend(x)
        lenx = len(x)
        max0 = -10**9
        dp = [0]*lenx
        if sumx <= 0 or K < lenx:
            for i in range(min(lenx-1,K)):
                for j in range(lenx):
                    dp[j] = dp[j] + tempx[j+i]
                max0 = max(max0,max(dp))
                #print(dp)
        else:
            K2 = K % lenx
            SN = int(K/lenx)
            for i in range(lenx):
                for j in range(lenx):
                    dp[j] = dp[j] + tempx[j+i]
                #print(dp)
                if i < K2:
                    max0 = max(max0,max(dp) + sumx*SN)
                    #print(max0)
                else:
                    max0 = max(max0,max(dp) + sumx*(SN-1))
        max00 = max(max00,max0)
    print(max00)
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))

# グルーピング
import copy
P2 = copy.copy(P)
Group = []
while len(P2)>0:
    temp = P2[0]
    tempGroup = []
    while temp in P2:
        tempGroup.append(temp)
        P2.remove(temp)
        temp = P[temp-1]
    Group.append(tempGroup)
Point = []
for x in Group:
    tempPoint = []
    for p in x:
        tempPoint.append(C[p-1])
    Point.append(tempPoint)

#print(Group)
#print(Point)

max00 = -10**9
for x in Point:
    sumx = sum(x)
    tempx = copy.copy(x)
    tempx.extend(x)
    lenx = len(x)
    max0 = -10**9
    dp = [0]*lenx
    if sumx <= 0 or K < lenx:
        for i in range(min(lenx-1,K)):
            for j in range(lenx):
                dp[j] = dp[j] + tempx[j+i]
            max0 = max(max0,max(dp))
            #print(dp)
    else:
        K2 = K % lenx
        SN = int(K/lenx)
        for i in range(lenx):
            for j in range(lenx):
                dp[j] = dp[j] + tempx[j+i]
            #print(dp)
            if i < K2:
                max0 = max(max0,max(dp) + sumx*SN)
                #print(max0)
            else:
                max0 = max(max0,max(dp) + sumx*(SN-1))
    max00 = max(max00,max0)
print(max00)


Fixed submission:
def main():
 
    N,K = map(int,input().split())
    P = list(map(int,input().split()))
    C = list(map(int,input().split()))
 
    # グルーピング
    import copy
    P2 = set(P)
    Group = []
    while len(P2)>0:
        temp = list(P2)[0]
        tempGroup = []
        while temp in P2:
            tempGroup.append(temp)
            P2.remove(temp)
            temp = P[temp-1]
        Group.append(tempGroup)
    Point = []
    for x in Group:
        tempPoint = []
        for p in x:
            tempPoint.append(C[p-1])
        Point.append(tempPoint)
 
    #print(Group)
    #print(Point)
 
    max00 = -10**9
    for x in Point:
        sumx = sum(x)
        tempx = copy.copy(x)
        tempx.extend(x)
        lenx = len(x)
        max0 = -10**9
        dp = [0]*lenx
        if sumx <= 0 or K < lenx:
            for i in range(min(lenx-1,K)):
                for j in range(lenx):
                    dp[j] = dp[j] + tempx[j+i]
                max0 = max(max0,max(dp))
                #print(dp)
        else:
            K2 = K % lenx
            SN = int(K/lenx)
            for i in range(lenx):
                for j in range(lenx):
                    dp[j] = dp[j] + tempx[j+i]
                #print(dp)
                if i < K2:
                    max0 = max(max0,max(dp) + sumx*SN)
                    #print(max0)
                else:
                    max0 = max(max0,max(dp) + sumx*(SN-1))
        max00 = max(max00,max0)
    print(max00)
 
if __name__ == "__main__":
    main()