Problem p02585 (python)

Fixed submission:
N, K = map(int, input().split())
P = list(map(lambda x: int(x) - 1, input().split()))
C = list(map(int, input().split()))

maxscore = -(10 ** 18)

for st in range(N):
	cumsum = [0]
	now = st
	while(P[now] != st):
		now = P[now]
		cumsum.append(cumsum[-1] + C[now])
	cumsum.append(cumsum[-1] + C[st])
	
	m = len(cumsum) - 1
	loopsum = cumsum[-1]
	
	if loopsum < 0:
		maxscore = max(maxscore, max(cumsum[1:min(K + 1, m + 1)]))
		continue
	
	score = (K // m - 1) * loopsum
	extended_cumsum = cumsum + [cumsum[i] + loopsum for i in range(1, K % m + 1)]
	score += max(extended_cumsum)
	maxscore = max(maxscore, score)
print(maxscore)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=-10**10
for st in range(N):
  now = st+1
  temp=[0]
  while(P[now-1] != st+1):
    temp.append(temp[-1]+C[P[now-1]-1])
    now = P[now-1]
    temp.append(temp[-1]+C[P[now-1]-1])
  if len(temp)>=K+1:
    ans=max(ans, max(temp[1:K+1]))
    continue
  if temp[-1]<=0:
    ans = max(ans, (max(temp[1:])))
  else:
    t=temp[-1]*(K//(len(temp)-1))-temp[-1]
    t+=max(max(temp),max([temp[-1]+temp[i+1] for i in range(K%(len(temp)-1))]))
    ans=max(ans, t)
		
print(int(ans))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=-10**10
for st in range(N):
  now = st+1
  temp=[0]
  while(P[now-1] != st+1):
    temp.append(temp[-1]+C[P[now-1]-1])
    now = P[now-1]
    temp.append(temp[-1]+C[P[now-1]-1])
  if len(temp)>=K+1:
    ans=max(ans, max(temp[1:K+1]))
    continue
  if temp[-1]<=0:
    ans = max(ans, (max(temp[1:])))
  else:
    t=temp[-1]*(K//(len(temp)-1))-temp[-1]
    pりんt(temp+[temp[-1]+temp[i+1] for i in range(K%(len(temp)-1))])
    ans=max(ans, t)
		
print(int(ans))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=-10**10
for st in range(N):
  now = st+1
  temp=[0]
  while(P[now-1] != st+1):
    temp.append(temp[-1]+C[P[now-1]-1])
    now = P[now-1]
    temp.append(temp[-1]+C[P[now-1]-1])
  if len(temp)>=K+1:
    ans=max(ans, max(temp[1:K+1]))
    continue
  if temp[-1]<=0:
    ans = max(ans, (max(temp[1:])))
  else::
    t=temp[-1]*(K//(len(temp)-1))-temp[-1]
    t+=max(temp+[temp[-1]+temp[i+1] for i in range(K%(len(temp)-1)+1)])
    ans=max(ans, t)
		
print(int(ans))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=-10**10
for st in range(N):
  now = st+1
  temp=[0]
  while(P[now-1] != st+1):
    temp.append(temp[-1]+C[P[now-1]-1])
    now = P[now-1]
    temp.append(temp[-1]+C[P[now-1]-1])
  if len(temp)>=K+1:
    ans=max(ans, max(temp[1:K+1]))
    continue
  if temp[-1]<=0:
    ans = max(ans, (max(temp[1:])))
  else
    t=temp[-1]*(K//(len(temp)-1))-temp[-1]
    t+=max(temp+[temp[-1]+temp[i+1] for i in range(K%(len(temp)-1)+1)])
    ans=max(ans, t)
		
print(int(ans))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
P=list(map(int,input().split()))
C=list(map(int,input().split()))
ans=-10**10
for st in range(N):
	now = st+1
	temp=[0]
	while(P[now-1] != st+1):
		temp.append(temp[-1]+C[P[now-1]-1])
		now = P[now-1]
	temp.append(temp[-1]+C[P[now-1]-1])
	if len(temp)>=K+1:
		ans=max(ans, max(temp[1:K+1]))
		continue
	if temp[-1]<=0:
		ans = max(ans, (max(temp[1:])))
	elif K%(len(temp)-1)!=0:
		t=temp[-1]*(K//(len(temp)-1))
		t+=max(temp[:1+K%(len(temp)-1)])
		ans=max(ans, t)
    else:
        t=temp[-1]*(K//(len(temp)-1))-temp[-1]
        t+=max(temp)
        ans=max(ans,t)
		
print(int(ans))