Buggy python submission (verdict: Time Limit Exceeded):
N,K = (int(T) for T in input().split())
Next = [int(T)-1 for T in input().split()]
GetP = [int(T) for T in input().split()]
DispP = [0]*N
for TN in range(0,N):
    Now   = Next[TN]
    Point = GetP[Now]
    MAXP  = Point
    RoopC = 1
    RoopFlag = False
    for TK in range(1,K):
        Now = Next[Now]
        Point += GetP[Now]
        RoopC += 1
        if Now==TN and Point>=0:
            RoopFlag = True
            break
        elif Now==TN and Point<0:
            RoopFlag = False
            break
        if MAXP<Point:
            MAXP = Point
            MInd = TK
    if RoopFlag:
        Omit = K-RoopC*(K//RoopC)
        PointO = 0
        MAXO = 0
        for TO in range(0,Omit):
            Now = Next[Now]
            PointO += GetP[Now]
            if MAXO<PointO:
                MAXO = PointO
        if Point*(K//RoopC)+MAXO>=Point*((K//RoopC)-1)+MAXP:
            DispP[TN] = Point*(K//RoopC)+MAXO
        else:
            DispP[TN] = Point*((K//RoopC)-1)+MAXP
    else:
        DispP[TN] = MAXP
print(max(DispP))

Fixed submission:
N,K = (int(T) for T in input().split())
Next = [int(T)-1 for T in input().split()]
GetP = [int(T) for T in input().split()]
DispP = [0]*N
for TN in range(0,N):
    Now   = Next[TN]
    Point = GetP[Now]
    MAXP  = Point
    RoopC = 1
    RoopFlag = False
    for TK in range(1,K):
        Now = Next[Now]
        Point += GetP[Now]
        RoopC += 1
        if Now==TN and Point>=0:
            RoopFlag = True
            break
        elif Now==TN and Point<0:
            RoopFlag = False
            break
        if MAXP<Point:
            MAXP = Point
            MInd = TK
    if RoopFlag:
        Omit = K-RoopC*(K//RoopC)
        PointO = 0
        MAXO = 0
        for TO in range(0,Omit):
            Now = Next[Now]
            PointO += GetP[Now]
            if MAXO<PointO:
                MAXO = PointO
        if Point*(K//RoopC)+MAXO>=Point*((K//RoopC)-1)+MAXP:
            DispP[TN] = Point*(K//RoopC)+MAXO
        else:
            DispP[TN] = Point*((K//RoopC)-1)+MAXP
    else:
        DispP[TN] = MAXP
print(max(DispP))

Buggy python submission (verdict: Time Limit Exceeded):
N,K = (int(T) for T in input().split())
Next = [int(T)-1 for T in input().split()]
GetP = [int(T) for T in input().split()]
DispP = [0]*N
for TN in range(0,N):
    Now   = Next[TN]
    Point = GetP[Now]
    MAXP  = Point
    RoopC = 1
    RoopFlag = False
    for TK in range(1,K):
        Now = Next[Now]
        Point += GetP[Now]
        RoopC += 1
        if Now==TN and Point>=0:
            RoopFlag = True
            break
        elif Now==TN and Point<0:
            RoopFlag = False
            break
        if MAXP<Point:
            MAXP = Point
    if RoopFlag:
        Omit = K-RoopC*((K//RoopC)-1)
        Point *= (K//RoopC)-1
        MAXP = Point
        for TO in range(0,Omit):
            Now = Next[Now]
            Point += GetP[Now]
            if MAXP<Point:
                MAXP = Point
    DispP[TN] = MAXP
print(max(DispP))

Fixed submission:
N,K = (int(T) for T in input().split())
Next = [int(T)-1 for T in input().split()]
GetP = [int(T) for T in input().split()]
DispP = [0]*N
for TN in range(0,N):
    Now   = Next[TN]
    Point = GetP[Now]
    MAXP  = Point
    RoopC = 1
    RoopFlag = False
    for TK in range(1,K):
        Now = Next[Now]
        Point += GetP[Now]
        RoopC += 1
        if Now==TN and Point>=0:
            RoopFlag = True
            break
        elif Now==TN and Point<0:
            RoopFlag = False
            break
        if MAXP<Point:
            MAXP = Point
            MInd = TK
    if RoopFlag:
        Omit = K-RoopC*(K//RoopC)
        PointO = 0
        MAXO = 0
        for TO in range(0,Omit):
            Now = Next[Now]
            PointO += GetP[Now]
            if MAXO<PointO:
                MAXO = PointO
        if Point*(K//RoopC)+MAXO>=Point*((K//RoopC)-1)+MAXP:
            DispP[TN] = Point*(K//RoopC)+MAXO
        else:
            DispP[TN] = Point*((K//RoopC)-1)+MAXP
    else:
        DispP[TN] = MAXP
print(max(DispP))