Buggy python submission (verdict: Runtime Error):
#from functools import lru_cache
import sys
from itertools import product,combinations,accumulate
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s = rvfunc(int)(reader())
n,k=s[0]
p=s[1]
c=s[2]

def pnt(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(c[current-1])
    return out
def loc(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(current)
    return out
    
def acc(i):    
    p,l=pnt(i),loc(i)
    sp = sum(p)
#    print(p,sp)
    init = l.index(i)
    pini = p[init:]+p[:init]
    last = p[(init+int((k-1)%n))%n]
    plst = p[:last]
    if k+1<n:
        return max(list(accumulate(pini[:k-1])))
    else:
        if sp<0:
            return max(list(accumulate(p)))
        else:
            return max(list(accumulate(p)))+sum(p[:last])+sp*int((k-1)/n)

print(max(vfunc(acc)(range(1,n+1))))


Fixed submission:
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=reader()

print(max(vfunc(len)(s[0][0].split('S'))))

Buggy python submission (verdict: Runtime Error):
#from functools import lru_cache
import sys
from itertools import product,combinations,accumulate
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s = rvfunc(int)(reader())
n,k=s[0]
p=s[1]
c=s[2]

def pnt(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(c[current-1])
    return out
def loc(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(current)
    return out
    
def acc(i):    
    p,l=pnt(i),loc(i)
    sp = sum(p)
#    print(p,sp)
    init = l.index(i)
    pini = p[init:]+p[:init]
    last = p[(init+int((k-1)%n))%n]
    plst = p[:last]
    if k+1<n:
        return max(list(accumulate(pini[:k-1])))
    else:
        if sp<0:
            return max(list(accumulate(p)))
        else:
            return max(list(accumulate(p)))+sum(p[:last])+sp*int((k-1)/n)

print(max(vfunc(acc)(range(1,n+1))))


Fixed submission:
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=reader()

print(max(vfunc(len)(s[0][0].split('S'))))

Buggy python submission (verdict: Runtime Error):
#from functools import lru_cache
import sys
from itertools import product,combinations,accumulate
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s = rvfunc(int)(reader())
n,k=s[0]
p=s[1]
c=s[2]

def pnt(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(c[current-1])
    return out
def loc(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(current)
    return out
    
def acc(i):    
    p,l=pnt(i),loc(i)
    sp = sum(p)
#    print(p,sp)
    init = l.index(i)
    pini = p[init:]+p[:init]
    last = p[(init+int((k-1)%n))%n]
    plst = p[:last]
    if k+1<n:
        return max(list(accumulate(pini[:k-1])))
    else:
        if sp<0:
            return max(list(accumulate(p)))
        else:
            return max(list(accumulate(p)))+sum(p[:last])+sp*int((k-1)/n)

print(max(vfunc(acc)(range(1,n+1))))


Fixed submission:
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=reader()

print(max(vfunc(len)(s[0][0].split('S'))))

Buggy python submission (verdict: Runtime Error):
#from functools import lru_cache
import sys
from itertools import product,combinations,accumulate
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s = rvfunc(int)(reader())
n,k=s[0]
p=s[1]
c=s[2]

def pnt(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(c[current-1])
    return out
def loc(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(current)
    return out
    
def acc(i):    
    p,l=pnt(i),loc(i)
    sp = sum(p)
#    print(p,sp)
    init = l.index(i)
    pini = p[init:]+p[:init]
    last = p[(init+int((k-1)%n))%n]
    plst = p[:last]
    if k+1<n:
        return max(list(accumulate(pini[:k-1])))
    else:
        if sp<0:
            return max(list(accumulate(p)))
        else:
            return max(list(accumulate(p)))+sum(p[:last])+sp*int((k-1)/n)

print(max(vfunc(acc)(range(1,n+1))))


Fixed submission:
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=reader()

print(max(vfunc(len)(s[0][0].split('S'))))

Buggy python submission (verdict: Runtime Error):
#from functools import lru_cache
import sys
from itertools import product,combinations,accumulate
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s = rvfunc(int)(reader())
n,k=s[0]
p=s[1]
c=s[2]

def pnt(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(c[current-1])
    return out
def loc(i):
    out = []
    current=i
    for j in range(n):
        current=p[current-1]
        out.append(current)
    return out
    
def acc(i):    
    p,l=pnt(i),loc(i)
    sp = sum(p)
    print(p,sp)
    init = l.index(i)
    pini = p[init:]+p[:init]
    last = p[(init+int((k-1)%n))%n]
    plst = p[:last]
    if k+1<n:
        return max(list(accumulate(pini[:k-1])))
    else:
        if sp<0:
            return max(list(accumulate(p)))
        else:
            return max(list(accumulate(p)))+sum(p[:last])+sp*int((k-1)/n)

print(max(vfunc(acc)(range(1,n+1))))


Fixed submission:
import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

s=reader()

print(max(vfunc(len)(s[0][0].split('S'))))