Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02658 (python)

Fixed submission:
def main():
    N = int(input())
    A = list(map(int, input().split()))

    if 0 in A:
        print(0)
        return

    ans = 1
    for a in A:
        ans *= a
        if ans > 1000000000000000000:
            print(-1)
            return

    print(ans)

main()

Buggy submission (verdict: Runtime Error):
#include<bits/stdc++.h>
#define ll long long int
 
using namespace std;
 
int main(){
  ll n;
  cin>>n;
  ll arr[n];
  ll m =1;
  for(int i=0;i<n;i++){
    cin>> arr[i];
  }
  for(int i=0;i<n;i++){
    m = m* arr[i];
  }
  if(m <=1000000000000000000)
    cout<<m<<endl;
  else
    cout<<'-1'<<endl;
  return 0;
}
Problem p02780 (python)

Fixed submission:
def main():
    N, K  = map(int, input().split())
    P = list(map(int, input().split()))
    best = sum(P[:K]) # sum of first K terms
    curr = best # tmp var for iteration
    for i in range(N - K):
        curr += P[i+K] - P[i] # slide window
        best = max(best, curr)
    print((best + K) / 2) # based on n(n+1)/2 == sum(range(1, n))

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> p(n);
    for (int i = 0; i < n; i++) cin >> p[i];
    vector<double> e(n);
    for (int i = 0; i < n; i++) {
        int s = 0;
        for (int j = 1; j <= p[i]; j++) s+=j;
        e[i] = (double) s / p[i];
    }

    double curr = 0;
    for (int i = 0; i < k-1; i++) curr += e[i];
    double best = curr;
    for (int i = k-1; i < n; i++) {
        curr += e[i];
        best = max(best, curr);
        curr -= e[i-k+1];
    }
    cout << best << endl;
    
    return 0;
}
Problem p02791 (python)

Fixed submission:
N = int(input())
P = list(map(int, input().split()))

ret = 0
currmin = P[0]
for i in range(N):
    if P[i] <= currmin:
        currmin = P[i]
        ret += 1

print(ret)

Buggy submission (verdict: Runtime Error):
N = int(input())
P = map(int, input().split())

ret = 0
currmin = P[0]
for i in range(N):
    if P[i] <= currmin:
        currmin = P[i]
        ret += 1

print(ret)
Problem p02801 (python)

Fixed submission:
import numpy as np

def main():
    C = input()
    print('{}'.format(chr(ord(C) + 1)))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
acc = [False] * N
pen = 0
for _ in range(M):
    p, S = input().split()
    p = int(p) - 1
    if S[0] == 'A':
        acc[p] = True
    else:
        if not acc[p]:
            pen += 1
print('{} {}'.format(sum(acc), pen))
Problem p02802 (python)

Fixed submission:
import numpy as np

def main():
    N, M = map(int, input().split())
    PS = {}
    for i in range(M):
        row = input().split()
        row[0] = int(row[0])
        if row[0] not in PS:
            PS[row[0]] = [row[1]]
        else:
            PS[row[0]].append(row[1])

    n_correct = 0
    n_penalty = 0
    for p in PS:
        if 'AC' in PS[p]:
            n_correct += 1
            for result in PS[p]:
                if result == 'AC':
                    break
                if result == 'WA':
                    n_penalty += 1
    
    print('{} {}'.format(n_correct, n_penalty))
        


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import numpy as np

def main():
    # N, M = map(int, input().split())
    # PS = {}
    # for i in range(M):
    #     row = input().split()
    #     row[0] = int(row[0])
    #     if row[0] not in PS:
    #         PS[row[0]] = [row[1]]
    #     else:
    #         PS[row[0]].append(row[1])

    # n_correct = 0
    # n_penalty = 0
    # for p in PS:
    #     if 'AC' in PS[p]:
    #         n_correct += 1
    #         for result in PS[p]:
    #             if result == 'AC':
    #                 break
    #             if result == 'WA':
    #                 n_penalty += 1
    
    # print('{} {}'.format(n_correct, n_penalty))

    
    N, M = map(int, input().split())
    acc = [False] * (N+1)
    pen = [0] * (N+1)
    for _ in range(M):
        p, S = input().split()
        p = int(p)
        if S[0] == 'A':
            acc[p] = True
        else:
            pen[p] += 1
    pen = 0
    for i in range(1, N+1):
        if not acc[p]:
            pen += pen[p]
    print('{} {}'.format(sum(acc), pen))

    

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
acc = [False] * N
pen = 0
for _ in range(M):
	p, S = input().split()
	p = int(p) - 1
	if S[0] == 'A':
		acc[p] = True
	else:
		if not acc[p]:
            pen += 1
print('{} {}'.format(sum(acc), pen))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
acc = [False] * N
pen = 0
for _ in range(M):
  p, S = input().split()
  p = int(p) - 1
  if S[0] == 'A':
    acc[p] = True
  else:
  	if not acc[p]:
      pen += 1
print('{} {}'.format(sum(acc), pen))
Problem p03166 (python)

Fixed submission:
from collections import deque

def main():
    n, m = map(int, input().split())
    graph = [[] for _ in range(n+1)]
    parents = [0] * (n+1)
    for _ in range(m):
        src, dst = map(int, input().split())
        graph[src].append(dst)
        parents[dst] += 1

    q = deque()
    for i in range(1, n+1):
        if parents[i] == 0:
            q.append(i)

    dists = [0] * (n+1)
    for i in q:
        dists[0] = 0

    while q:
        at = q.popleft()
        for to in graph[at]:
            parents[to] -= 1
            if parents[to] == 0:
                q.append(to)
                dists[to] = dists[at] + 1

    print(max(dists[1:]))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import deque

def main():
    n, m = map(int, input().split())

    dag = [[] for _ in range(n+1)]
    for _ in range(m):
        src, dst = map(int, input().split())
        dag[src].append(dst)
    for arr in dag:
        arr = sorted(arr)

    # Distance of longest path starting at node i
    dists = [0] * (n+1)

    def longest(v):
        if dists[v]:
            return dists[v]
        for to in dag[v]:
            dists[v] = max(dists[i], longest(to) + 1)
        return dists[v]

    ans = 0
    for i in range(1, n+1):
        ans = max(ans, longest(i))

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    n, m = map(int, input().split())

    dag = [[] for _ in range(n+1)]
    for _ in range(m):
        src, dst = map(int, input().split())
        dag[src].append(dst)

    # Distance of longest path starting at node i
    dists = [0] * (n+1)

    def longest(v):
        if dists[v]:
            return dists[v]
        for to in dag[v]:
            dists[v] = max(dists[i], longest(to) + 1)
        return dists[v]

    max_len = 0
    for i in range(1, n+1):
        max_len = max(max_len, longest(i))

    print(max_len)



if __name__ == '__main__':
    main()
Problem p02658 (python)

Fixed submission:
n = int(input())
A = [*map(int,input().split())]
A.sort()
ans = 1
for a in A:
    ans *= a
    if ans > 10**18:
        ans = -1
        break
print(ans)

Buggy submission (verdict: Runtime Error):
def main():
    from decimal import Decimal
    import numpy as np
    _=input()
    lis = np.array(list(map(Decimal,input().split())))
    lis=sorted(lis)

    if 0 in lis:
        print(0)
        return
    ans = np.prod(lis)
    if ans >10**18:
        print(-1)
        return
    print(ans)

main()

Buggy submission (verdict: Runtime Error):
from decimal import Decimal
import numpy as np
_=input()
lis = np.array(list(map(Decimal,input().split())))
lis=sorted(lis)

ans = np.prod(lis)
if ans >10**18:
    ans=-1
print(ans)
Problem p02719 (python)

Fixed submission:
n,k=map(int,input().split())
print(min(n,n%k,abs(n%k-k)))

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
print(min(n%k,abs(n%k-k))
Problem p02720 (python)

Fixed submission:
k=input()

ln=[1,2,3,4,5,6,7,8,9]
i=0
while len(ln)<100001:
    if ln[i]%10==0:
        ln.append(ln[i]*10)
        ln.append(ln[i]*10+1)
    elif ln[i]%10!=9:
        ln.append(ln[i]*10+ln[i]%10-1)
        ln.append(ln[i]*10+ln[i]%10)
        ln.append(ln[i]*10+ln[i]%10+1)
    else:
        ln.append(ln[i]*10+8)
        ln.append(ln[i]*10+9)
    i+=1

# print ln
# print ln[9]
print ln[k-1]

Buggy submission (verdict: Runtime Error):
k=input()

ln=[1,2,3,4,5,6,7,8,9]
old=0
new=9
while len(ln)<100:
    for i in xrange(old, new):
        if ln[i]%10!=9:
            ln.append(ln[i]*10+ln[i]%10-1)
            ln.append(ln[i]*10+ln[i]%10)
            ln.append(ln[i]*10+ln[i]%10+1)
        else:
            ln.append(ln[i]*10+ln[i]%10-1)
            ln.append(ln[i]*10+ln[i]%10)
    old=new
    new=len(ln)

ln.sort()
print ln[k-1]
Problem p02582 (python)

Fixed submission:
s1, s2, s3 = str(input())
if s1 == "R":
  if s2 == "S":
    result = 1
  else:
    if s3 == "S":
      result = 2
    else:
      result = 3
else:
  if s2 == "S":
    if s3 == "S":
      result = 0
    else:
      result = 1
  else:
    if s3 == "S":
      result = 1
    else:
      result = 2

print(result)

Buggy submission (verdict: Runtime Error):
x, k, d = map(int, input().split())
distance = k * d
result = abs(x - distance)


while distance >= 0:
  distance -= d * 2
  if result > result = abs(x - distance):
    result = abs(x - distance)
    
print(result)
Problem p02657 (python)

Fixed submission:
a, b = map(int,input().split())
p = a * b
print(p)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(ai) for ai in input().split()]
ans = 1
if 0 in A:
  print(0)
else:
  for ai in A:
    ans *= ai
    if ans > 10**18:
      print(-1)
      break
  
  print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
 
A=[int(i) for i in input().split()]
 
answer=1
if 0 in A:
  print(0)
else:
  for i in range(N):
    answer = answer*A[i]
    if answer>10**18:
      print(-1)
      break
  else:
    print(answer)

Buggy submission (verdict: Runtime Error):
a,b = map(int,intput().split())
p = a * b
print(p)
Problem p02658 (python)

Fixed submission:
n =int(input())
ls =[]
ls =list(map(int,input().split()))
if 0 in ls:
	print(0)
else:
	ans = 1
	for a in ls:
		ans *= a
		if ans > 10 ** 18:
			ans = -1;
			break
		
	print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [int(ai) for ai in input().split()]
ans = 1
if 0 in A:
  print(0)
else:
  for ai in A:
    ans *= ai
  if ans > 10**18:
    print(-1)
    break
  else:
    print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
 
list=[int(i) for i in input().split()]
 
answer=1
if 0 in list:
  print(0)
else:
  for i in range(N):
    answer = answer*A[i]
    if answer>10**18:
      print(-1)
      break
  else:
    print(answer)

Buggy submission (verdict: Runtime Error):
n = int(input())
result = 1
list=[]
list = list(map(int,input().split()))
if 0 in list:
  print(0)
else:
  for i in list:
  result *= list[i]
  if result > 10 ** 18:
  print(-1)
  break

print(result)

Buggy submission (verdict: Runtime Error):
n = int(input())
result = 1
list = []
list = list(map(int,input().split()))

for i in list:
  result *= i

if result > 10 ^ 18:
  print(-1)
else:
  print(result)

Buggy submission (verdict: Runtime Error):
n = int(input())
result = 1
for i in range(n):
  result *= int(input())

if result > 10 ^ 18:
  print(-1)
else:
  print(result)
Problem p02861 (python)

Fixed submission:
import numpy as np
import sys
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
XY = np.array(read().split(), np.int64)
X = XY[0::2]
Y = XY[1::2]

t = 0
for i in range(N):
    for j in range(i, N):
        t += math.sqrt((X[i] - X[j])**2 + (Y[i] - Y[j])**2)
print(2*t/N)

Buggy submission (verdict: Runtime Error):
from itertools import permutations as perm
import sys
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
XY = np.array(read().split(), np.int64)
X = XY[0::2]
Y = XY[1::2]

res = 0
cnt = 0
for p in perm(range(N)):
    for i in range(N - 1):
        res += math.sqrt((X[p[i + 1]] - X[p[i]])**2 + (Y[p[i + 1]] - Y[p[i]])**2)
    cnt += 1    
print(res/cnt)
Problem p02866 (python)

Fixed submission:
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
else:
    D = D[1:]
    C = [0]*N
    m = max(D)
    for d in D:
        C[d] += 1
    if C[0] != 0 or 0 in C[1: m + 1]:
        print(0)
    else:
        res = 1
        i = 1
        for i in range(1, m):
            res *= (C[i]**C[i + 1])%998244353
        print(res%998244353)

Buggy submission (verdict: Runtime Error):
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
else:
    D = D[1:]
    C = [0]*N
    for d in D:
        C[d] += 1
    if C[0] != 0:
        print(0)
    else:
        res = 1
        for i in range(1, N - 1):
            res *= C[i]**C[i + 1]
    print(res%998244353)

Buggy submission (verdict: Runtime Error):
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
else:
    D = D[1:]
    C = [0]*N
    for d in D:
        C[d] += 1
    if C[0] != 0:
        print(0)
    else:
        res = 1
        for i in range(1, N - 1):
            res *= C[i]**C[i + 1]
    print(res)
Problem p02889 (python)

Fixed submission:
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
INF = 10**12
N, M, L = map(int, input().split())

A = []; B = []; C = []
for i in range(M):
    a, b, c = map(int, input().split())
    A.append(a - 1); B.append(b - 1); C.append(c)
A = np.array(A); B = np.array(B); C = np.array(C)

graph = csr_matrix((C, (A, B)), (N, N))
d = floyd_warshall(graph, directed=False)

d[d <= L] = 1
d[d > L] = INF

d = floyd_warshall(d, directed=False)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split())
    if d[s - 1][t - 1] != INF:
        print(int(d[s - 1][t - 1]) - 1)
    else:
        print(- 1)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix

INF = 10**12
N, M, L = map(int, readline().split())

ABCQST = np.array(read().split(), dtype=np.int64)
ABC = ABCQST[:3*M]
A = ABC[::3]; B = ABC[1::3]; C = ABC[2::3]

Q = ABCQST[3*M]

ST = ABCST[3*M + 1:]
S = ST[::2]; T = ST[1::2]

graph = csr_matrix((C, (A, B)), (N, N))
d = floyd_warshall(graph, directed=False)

d[d <= L] = 1
d[d > L] = INF

d = floyd_warshall(d, directed=False).astype(int)
d[d == INF] = 0

d -= 1
print('\n'.join(d.astype(str)[S, T]))

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
INF = 10**12
N, M, L = map(int, input().split())

A = []; B = []; C = []
for i in range(M):
    a, b, c = map(int, input().split())
    A.append(a - 1); B.append(b - 1); C.append(c)
A = np.array(A); B = np.array(B); C = np.array(C)

graph = csr_matrix((C, (A, B)), (N, N))
d = floyd_warshall(graph, directed=False)

d[0 < d <= L] = 1
#d[d > L] = INF

d = floyd_warshall(d, directed=False)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split())
    if d[s - 1][t - 1] != 0:
        print(int(d[s - 1][t - 1]) - 1)
    else:
        print(- 1)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
INF = 10**12
N, M, L = map(int, input().split())

A = []; B = []; C = []
for i in range(M):
    a, b, c = map(int, input().split())
    A.append(a - 1); B.append(b - 1); C.append(c)
A = np.array(A); B = np.array(B); C = np.array(C)

graph = csr_matrix((C, (A, B)), (N, N))
d = floyd_warshall(graph, directed=False)

d[d <= L] = 1
d[d > L] = INF

d = floyd_warshall(d, directed=False)

Q = int(input())
for i in range(Q):
    s, t = map(int, input().split())
    if d[s][t] != INF:
        print(int(d[s - 1][t - 1]) - 1)
    else:
        print(- 1)
Problem p02900 (python)

Fixed submission:
def gcd(x, y):
    """
    ユークリッドの互除法を用いてxとyの最大公約数を求める。
    """
    while True:
        r = x%y
        if r == 0:
            break
        x = y
        y = r
    return y
    
def prime_factorize(x):
    """
    素因数分解
    """
    res = []
    while x%2 == 0:
        res.append(2)
        x = x//2
    p = 3
    while p*p <= x:
        while x%p == 0:
            res.append(p)
            x = x//p
        p += 2
    if x != 1:
        res.append(x)
    return res


A, B = map(int, input().split())
print(len(set(prime_factorize(gcd(A, B)))) + 1)

Buggy submission (verdict: Runtime Error):
mport numpy as np
A, B = map(int, input().split())
while A%B > 0:
    c = A
    A = B
    B = c%B
P = [1]
if B%2 == 0:
    P.append(2)
for i in range(3, B//2 + 1, 2):
    if B%i == 0:
        P.append(i)
if B > 2:
    P.append(B)
    
l = len(P)
for i in range(2, l):
    if P[i] == 0:
        continue
    for j in range(i + 1, l):
        if P[j]%P[i] == 0:
            P[j] = 0
P = np.array(P)
print(np.where(P != 0)[0].size)
Problem p02901 (python)

Fixed submission:
N, M = map(int, input().split())

INF = 10**7
dp = [INF]*2**N
dp[0] = 0

for i in range(M):
    a, b = map(int, input().split())
    d = sum((1<< (i - 1)) for i in  map(int, input().split()))    
    for j in range(2**N):
        jord = j | d
        dp[jord] = min(dp[j] + a, dp[jord])
        
if dp[- 1] == INF:
    print(- 1)
else:
    print(dp[- 1])

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

INF = 10**7
dp = [INF]*2**N
dp[0] = 0
bits = set(0)
for i in range(M):
    a, b = map(int, input().split())
    d = sum((1<< (i - 1)) for i in  map(int, input().split()))
    add_set = set() # i個目の鍵を使ってbitsに新しく追加する分の状態を入れる
    for j in bits:
        jord = j | d
        dp[jord] = min(dp[jord], dp[j] + a)
        add_set.add(jord)
    bits |= add_set
    
if dp[- 1] == INF:
    print(- 1)
else:
    print(dp[- 1])

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())

INF = 10**7
dp = [INF]*2**N
dp[0] = 0

for i in range(M):
    a, b = map(int, input().split())
    c_ls = map(int, input().split())
    #d = sum((1<< (i - 1)) for i in  map(int, input().split()))  
    d = 0
    for c in c:
        d += 2**(c - 1)
    for j in range(2**N):
        jord = j | d
        dp[jord] = min(dp[j] + a, dp[jord])
        
if dp[- 1] == INF:
    print(- 1)
else:
    print(dp[- 1])

Buggy submission (verdict: Runtime Error):
import copy
N, M = map(int, input().split())

def f(x):
    return int(x) - 1

A = []
#B = []
C = []
for i in range(M):
    a, b = map(int, input().split())
    A.append(a)
    #B.append(b)
    C.append(list(map(f, input().split())))



def rec(cost, still_closed, d):
    """
    初期値
    cost = 0
    still_closed = [1]*N
    d = 0
    """
    if sum(still_closed) == 0:
        return cost
    if d == M:
        return - 1
    still_closed1 = copy.copy(still_closed)
    still_closed2 = copy.copy(still_closed)
    res1 = rec(cost, still_closed1, d + 1)
    for c in C[d]:
        still_closed[c] = 0
    res2 = rec(cost + A[d], still_closed, d + 1)
    if res1 == - 1 or res2 == - 1:
        return max(res1, res2)
    else:
        return min(res1, res2)

print(rec(0, [1]*N, 0))

Buggy submission (verdict: Runtime Error):
import copy
N, M = map(int, input().split())

def f(x):
    return int(x) - 1

A = []
#B = []
C = []
for i in range(M):
    a, b = map(int, input().split())
    A.append(a)
    #B.append(b)
    C.append(list(map(f, input().split())))



def rec(cost, still_closed, d):
    """
    初期値
    cost = 0
    still_closed = [1]*N
    d = 0
    """
    if d == M:
        if sum(still_closed) == 0:
            return cost
        else:
            return - 1
    still_closed1 = copy.copy(still_closed)
    still_closed2 = copy.copy(still_closed)
    res1 = rec(cost, still_closed1, d + 1)
    for c in C[d]:
        still_closed[c] = 0
    res2 = rec(cost + A[d], still_closed, d + 1)
    if res1 == - 1 or res2 == - 1:
        return max(res1, res2)
    else:
        return min(res1, res2)

print(rec(0, [1]*N, 0))
Problem p02913 (python)

Fixed submission:
N = int(input())
S = input()
r = 1
ans = 0
for i in range(N):
    while True:
        if S.rfind(S[i : r + 1]) >= r + 1:
            r += 1
            ans = max(ans, r - i)
        else:
            break
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
def Zalgo(S):
    N = len(S)
    A = [0]*N
    A[0] = N
    i = 1
    j = 0
    while i < N:
        while i + j < N and S[j] == S[i + j]:
            j += 1
        A[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i + k < N and k + A[k] < j:
            A[i + k] = A[k]
            k += 1
        i += k
        j -= k
res = 0
for i in range(N - 1):
    lcp = Zalgo(S[i:])
    for j in range(N - i):
        l = min(j, lcp[j])
        res = max(l, res)
print(res)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = input()
k = 1
for i in range(N - 1):
    while True:
        if S[i : i + k] in S[i + k :]:
            k += 1
        else:
            break
print(l)
Problem p02919 (python)

Fixed submission:
import bisect
import array
N = int(input())
P = list(map(int, input().split()))
P_id = [0]*(N + 1)

for i in range(N):
    P_id[P[i]] = i

A = array.array('i', [- 1, P_id[N], N])


res = 0
for i in range(N - 1, 0, - 1):
    j = bisect.bisect_left(A, P_id[i])
    A.insert(j, P_id[i])
    l1 = A[j - 1]
    r1 = A[j + 1]
    l2 = l1
    r2 = r1
    if l1 != - 1:
        l2 = A[j - 2]
    if r1 != N:
        r2 = A[j + 2]
    
    res += i*((A[j] - l1)*(r2 - r1) + (l1 - l2)*(r1 - A[j]))

print(res)

Buggy submission (verdict: Runtime Error):
import bisect
N = int(input())
P = map(int, input().split())
P_id = [0]*(N + 1)

for i in range(N):
    P_id[P[i]] = i

A = [- 1, P_id[N], N] 

res = 0
for i in range(N - 1, 0, - 1):
    j = bisect.bisect_left(A, P_id[i])
    A.insert(j, P_id[i])
    l1 = A[j - 1]
    r1 = A[j + 1]
    l2 = l1
    r2 = r1
    if l1 != - 1:
        l2 = A[j - 2]
    if r1 != N:
        r2 = A[j + 2]
    
    res += i*((A[j] - l1)*(r2 - r1) + (l1 - l2)*(r1 - A[j]))

print(res)
Problem p02585 (python)

Fixed submission:
def resolve():
    N, K = map(int, input().split())
    P = list(map(int, input().split()))
    C = list(map(int, input().split()))
    for i in range(N):
        P[i] -= 1

    ans = -1e18
    for i in range(N):
        n = P[i]
        score = C[n]
        t = [score]
        while n != i:
            n = P[n]
            score += C[n]
            t.append(score)

        total = t[-1]
        l = len(t)
        for i in range(min(K, l)):
            temp_score = t[i]
            if total > 0 and K > l:
                e = int((K-(i+1))/l)
                temp_score += total*e
            ans = max(ans, temp_score)

    print(ans)


if __name__ == "__main__":
    resolve()

Buggy submission (verdict: Runtime Error):
def resolve():
    N, K = map(int, input().split())
    P = list(map(int, input().split()))
    C = list(map(int, input().split()))

    ans = -100100100
    for i in range(N):
        node_set = set()
        n = P[i]
        score = 0
        t = []
        while n not in node_set:
            score += C[n-1]
            t.append(score)
            node_set.add(n)
            n = P[n-1]

        if K < len(t):
            max_t = max(t[:K])
        else:
            if t[-1] > 0:
                temp = t[-1] * int(K / len(t)) + max([0] + t[:K%len(t)])
                max_t = max(max_t, temp)
            else:
                max_t = max(t)
        ans = max(ans, max_t)

    print(ans)


if __name__ == "__main__":
    resolve()