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

Fixed submission:
import math

n, m = map(int, input().split())
if abs(n-m) == 0:
    print(2*math.factorial(n)*math.factorial(m) % 1000000007)
elif abs(n-m) == 1:
    print(math.factorial(n)*math.factorial(m) % 1000000007)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
from math import factorial as f
print(max(0,2-abs(n-m))*f(n)*f(m) % (10**9+7))

Buggy submission (verdict: Runtime Error):
def a(n):
    if n < 2:
        return 1
    return a(n-1) * n

n, m = map(int, input().split())
if abs(n-m) > 1:
    print(0)
elif abs(n-m) == 1:
    print(a(n) * a(m) % 1000000007)
else:
    print(2 * a(n) * a(m) % 1000000007)
Problem p03693 (python)

Fixed submission:
s = input().split()
if int(''.join(s)) % 4 == 0:
  print("YES")
else:
  print("NO")

Buggy submission (verdict: Runtime Error):
s = input().split()
int(''.join(s)) % 4 == 0:
  print("YES")
else:
  print("NO")
Problem p03695 (python)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
color = [0]*9
for i in a:
  color[min(8,i//400)] += 1
kind = 0
for i in color[:8]:
  if i:
    kind += 1
if kind == 0 and color[8]:
  color[8] -= 1
  kind += 1
print(kind,kind+color[8])

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
color = [0]*8
for i in a:
  color[min(8,i//400)] += 1
kind = 0
for i in color[:7]:
  if i:
    kind += 1
if kind == 0 and color[7]:
  color[7] -= 1
  kind += 1
print(kind,kind+color[7])
Problem p03711 (python)

Fixed submission:
s = [[1, 3, 5, 7, 8, 10, 12],[4, 6, 9, 11],[2]]
x, y = map(int, input().split())
for i in range(3):
  if x in s[i] and y in s[i]:
    print("Yes")
    exit()
print("No")

Buggy submission (verdict: Runtime Error):
s = [[1, 3, 5, 7, 8, 10, 12][4, 6, 9, 11][2]]
x, y = map(int, input().split())
for i in range(3):
  if x in s[i] and y in s[i]:
    print("Yes")
    exit()
print("No")
Problem p03712 (python)

Fixed submission:
h, w = map(int, input().split())
a = [list(input()) for i in range(h)]
a = [['#' for i in range(w)]] + a + [['#' for i in range(w)]]
for i in range(h+2):
    a[i] = ['#']+ a[i] +['#']
    print(''.join(a[i]))

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
a = [list(input()) for i in range(h)]
a = ['#' for i in range(w)] + a + ['#' for i in range(w)]
for i in a:
    i = '#'+ i +'#'
    print(i)
Problem p03730 (python)

Fixed submission:
a, b, c = map(int, input().split())
while a != 0:
    a, b = b%a, a
if c % b == 0:
    print("YES")
else:
    print("NO")

Buggy submission (verdict: Runtime Error):
a, b, c = map(int, input().split())
while a != 0:
    a, b = b%a, a
if c % b = 0:
    print("YES")
else:
    print("NO")
Problem p03734 (python)

Fixed submission:
n, w = map(int, input().split())
di = dict()
m = 0
for _ in range(n):
  x,y = map(int, input().split())
  if x not in di:
    di[x] = []
  di[x].append(y)
for i in di:
  di[i].sort(reverse=True)
a = min(di.keys())
b,c,d = a+1,a+2,a+3
for i in b,c,d:
  if i not in di:
    di[i] = []
for i in range(len(di[a])+1):
  for j in range(len(di[b])+1):
    for k in range(len(di[c])+1):
      for l in range(len(di[d])+1):
        if i*a + j*b + k*c + l*d <= w:
          m = max(m, sum(di[a][:i] + di[b][:j] + di[c][:k] + di[d][:l]))
print(m)

Buggy submission (verdict: Runtime Error):
n, w = map(int, input().split())
di = dict()
m = 0
for _ in range(n):
  x,y = map(int, input().split())
  if x not in di:
    di[x] = []
  di[x].append(y)
for i in di:
  di[i].sort(reverse=True)
a,b,c,d = di.keys()
for i in range(len(di[a])):
  for j in range(len(di[b])):
    for k in range(len(di[c])):
      for l in range(len(di[d])):
        if i*a + j*b + k*c + l*d <= w:
          m = max(m, sum(di[a][:i+1] + di[b][:j+1] + di[c][:k+1] + di[d][:l+1]))
print(m)
Problem p03786 (python)

Fixed submission:
n = int(input())
a = sorted(list(map(int, input().split())),reverse=True)
tot = sum(a)
cnt = 1
for i in a:
  tot -= i
  if i > tot*2:
    break
  cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = sorted(list(map(int, input().split())),reverse=True)
mintot = sum(a)
cnt = 1
for i in range(n):
  tot -= a[i]
  if a[i] <= tot*2:
    break
  cnt += 1
print(cnt)
Problem p03844 (python)

Fixed submission:
a, op, b =[i for i in input().split()]
if op == "+":
  print(int(a) + int(b))
else:
  print(int(a) - int(b))

Buggy submission (verdict: Runtime Error):
a, op, b =[i for i in input()]
if op == "+":
  print(int(a) + int(b))
else:
  print(int(a) - int(b))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
t = [a.count(i) for i in range(n)]
for i in range(n//2):
  if t[i*2+n%2] != 0 or t[i*2+1+n%2] != 2 or t[0] > 1:
    print(0)
    exit()
print(2**(n//2))
Problem p03846 (python)

Fixed submission:
n = int(input())
l = list(map(int, input().split()))
d = dict()
mod = 10**9 + 7
for i in l:
  if i in d:
    d[i] += 1
  else:
    d[i] = 1
d2 = dict()
for i in range(1+n%2,n,2):
  d2[i] = 2
if n%2:
  d2[0] = 1
if d == d2:
  print(2**(n//2)%mod)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
d = {}
s = 1
b = 1
m = 1 - n%2
for i in a:
    if i in d:
        d[i] += 1
    else:
        d[i] = 1
for i in range(n):
    if i % 2 == m:
        if (i > 0 and d[i] != 2) or (i == 0 and d[i] != 1):
            b = 0
            break
        s *= d[i]
    elif i in d:
        b = 0
        break
if b:
    print(s)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
t = [a.count(i) for i in range(n)]
for i in range(n//2):
  if t[i*2+n%2] != 0 or t[i*2+1+n%2] != 2: or t[0] > 1:
    print(0)
    exit()
print(2**(n//2))
Problem p03852 (python)

Fixed submission:
v = set("aeiou")
if input() in v:
  print("vowel")
else:
  print("consonant")

Buggy submission (verdict: Runtime Error):
v = {a, e, i, o, u}
if input() in v:
  print("vowel")
else:
  print("consonant")
Problem p03853 (python)

Fixed submission:
h, _ = map(int, input().split())
for i in range(h):
  a = input()
  print(a)
  print(a)

Buggy submission (verdict: Runtime Error):
h, _ = map(int, input().split())
for i in range(h):
  a = input()
  print(a)*2
Problem p03857 (python)

Fixed submission:
from collections import defaultdict as dd
def find(u,x):
    if u[x]<0:
        return x
    u[x] = find(u,u[x])
    return u[x]

def unite(u,a,b):
    a = find(u,a)
    b = find(u,b)
    if a == b:
        return False
    if u[b] < u[a]:
        a,b = b,a
    u[a] += u[b]
    u[b] = a
    return True
n,k,l = map(int, input().split())
ufr = [-1]*(n+1)
uft = [-1]*(n+1)
for _ in range(k):
    a,b = map(int, input().split())
    unite(ufr,a,b)
for _ in range(l):
    a,b = map(int, input().split())
    unite(uft,a,b)
tot = dd(int)
for i in range(1,n+1):
  tot[(find(ufr,i),find(uft,i))] += 1
print(" ".join([str(tot[(find(ufr,i),find(uft,i))]) for i in range(1,n+1)]))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict as dd
road = dd(list)
train = dd(list)
def gr(tree,start):
    island = set()
    rest = [start,]
    while rest:
        node = rest.pop()
        island.add(node)
        for x in tree[node]:
            if x not in island:
                rest.append(x)
    return(island)
n,k,l = map(int, input().split())
for _ in range(k):
    a,b = map(int,input().split())
    road[a].append(b)
    road[b].append(a)
for _ in range(k):
    a,b = map(int,input().split())
    train[a].append(b)
    train[b].append(a)
for i in range(1,n+1):
    print(len(gr(road,i)&gr(train,i)))
Problem p03860 (python)

Fixed submission:
s = input().split()
c = [t[0] for t in s]
print("".join(c))

Buggy submission (verdict: Runtime Error):
a, b, x = map(int, input().split())
ans = b//x - a//x
if a%x == 0:
  ans += 1
print(ans)
Problem p03864 (python)

Fixed submission:
n,k = map(int, input().split())
a = list(map(int, input().split()))
tot = 0
for i in range(1,n):
  if a[i-1] + a[i] > k:
    c = min(a[i],a[i]+a[i-1]-k)
    tot += c
    a[i] -= c
    if a[i-1]+a[i] > k:
      d = a[i-1]-k
      tot += d
      a[i-1] -= d
print(tot)

Buggy submission (verdict: Runtime Error):
n,k = map(int, input().split())
l = list(map(int, input().split()))
tot = 0
for i in range(1,n):
  if a[i-1] + a[i] > k:
    c = min(a[i],k-(a[i]+a[i-1]))
    tot += c
    a[i] -= c
    if a[i-1]+a[i] > k:
      d = k-a[i-1]
      tot += d
      a[i-1] -= d
print(tot)
Problem p03919 (python)

Fixed submission:
h, w = map(int, input().split())
s = [input().split() for _ in range(h)]
al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(h):
  for j in range(w):
    if s[i][j] == "snuke":
      print("{}{}".format(al[j], i+1))
      exit()
    else:
      pass

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [input().split() for _ in range(h)]
al = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(h):
  for j in range(w):
    if s[i][j] = "snuke":
      print("{}{}".format(al[i], j+1))
      exit()
    else:
      pass
Problem p03920 (python)

Fixed submission:
n = int(input())
k = int((n*2)**.5)
while k*(k+1)//2 < n:
  k += 1
l = list(range(1,k+1))
if k*(k+1)//2-n:
  l.remove(k*(k+1)//2-n)
for i in l:
  print(i)

Buggy submission (verdict: Runtime Error):
n = int(input())
k = int((n*2)**.5)
while k*(k+1)//2 < n:
  k += 1
l = [range(1,k+1)]
if k*(k+1)//2-n:
  l.remove(k*(k+1)//2-n)
for i in l:
  print(l)
Problem p03945 (python)

Fixed submission:
s = input()
ans = 0
for i in range(len(s)-1):
  if s[i] != s[i+1]:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
ans = 0
for i in range(len(s-1)):
  if s[i] != s[i+1]:
    ans += 1
print(ans)
Problem p03966 (python)

Fixed submission:
n = int(input())
t2, a2 = map(int, input().split())
for _ in range(n-1):
  t, a = map(int, input().split())
  m = max((t2-1)//t, (a2-1)//a)+1
  t2 = m*t
  a2 = m*a
print(t2+a2)

Buggy submission (verdict: Runtime Error):
n = int(input())
ts, as = map(int, input().split())
for _ in range(n-1):
  t, a = map(int, input().split())
  m = max((ts-1)//t, (as-1)//a)+1
  ts = m*t
  as = m*a
print(ts+as)
Problem p03967 (python)

Fixed submission:
s = input()
m = "gp"*(len(s)//2+1)
g = 0
for i in range(len(s)):
  if s[i] == m[i]:
    continue
  elif s[i] == "p":
    g -= 1
  else:
    g += 1
print(g)

Buggy submission (verdict: Runtime Error):
s = input()
print(n//2-s.count("p"))

Buggy submission (verdict: Runtime Error):
s = input()
m = "gp"*(len(s)//2+1)
g = 0
for i in range(s):
  if s[i] == m[i]:
    continue
  elif s[i] == "p":
    g -= 1
  else:
    g += 1
print(g)
Problem p03971 (python)

Fixed submission:
n, a, b = map(int, input().split())
s = input()
cnt = a+b
fc = b
for i in range(n):
  if s[i] == 'a' and cnt:
    print("Yes")
    cnt -= 1
  elif s[i] == 'b' and cnt and fc:
    print("Yes")
    cnt -= 1
    fc -= 1
  else:
    print("No")

Buggy submission (verdict: Runtime Error):
n, a, b = map(int, input().split())
s = input()
cnt = a+b
fc = b
for i in range(n):
  if s[n] == 'a' and cnt:
    print("Yes")
    cnt -= 1
  elif s[n] == 'b' and cnt and fc:
    print("Yes")
    cnt -= 1
    fc -= 1
  else:
    print("No")
Problem p03987 (python)

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
p = [0]*(n+1)
ans = 0
for i in range(n):
  p[a[i]] = i+1
l = [i-1 for i in range(n+2)]
l[0] = 0
r = [i+1 for i in range(n+2)]
r[-1] = n+1
for i in range(n,0,-1):
  ans += (p[i]-l[p[i]])*(r[p[i]]-p[i])*i
  l[r[p[i]]] = l[p[i]]
  r[l[p[i]]] = r[p[i]]
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
  l,r = 1,1
  while a[i-l] > a[i] and i-l > -1:
    l += 1
  while a[i+r] > a[i] and i+r < n:
    r += 1
  ans += l*r*a[i]
print(ans)
Problem p04013 (python)

Fixed submission:
n,a = map(int,input().split())
dp = [[0]*2000 for _ in range(n+1)]
dp[0][1000] = 1
x = list(map(int,input().split()))
for i in range(n):
  x[i] -= a
for i in range(n):
  for j in range(2000):
    dp[i+1][j] += dp[i][j]
    if 0<=j+x[i]<2000:
      dp[i+1][j+x[i]] += dp[i][j]
print(dp[n][1000]-1)

Buggy submission (verdict: Runtime Error):
n,a = map(int,input().split())
dp = [[0]*2000 for _ in range(n+1)]
dp[0][1000] = 1
x = list(map(int,input().split()))
for i in range(n):
  x[i] -= a
for i in range(1,n+1):
  for j in range(2001):
    dp[i][j] = dp[i-1][j] + dp[i-1][j-x[i-1]]
print(dp[n][1000])
Problem p04019 (python)

Fixed submission:
s = input()
b, c = 0, 0
if "N" in s and "S" in s:
  b = 1
elif "N" not in s and "S" not in s:
  b = 1
if "E" in s and "W" in s:
  c = 1
elif "E" not in s and "W" not in s:
  c = 1
if b and c:
  print("Yes")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
s = input()
if ~("N" in s ^ "S" in s) and ~("E" in s ^ "W" in s):
  print("Yes")
else:
  print("No")
Problem p04043 (python)

Fixed submission:
if sorted(input().split()) == ["5", "5", "7"]:
  print("YES")
else:
  print("NO")

Buggy submission (verdict: Runtime Error):
if sorted(input().split()) == ["5", "5", "7"]:
  print("YES")
else:
  priny("NO")
Problem p02744 (python)

Fixed submission:
alpha = "abcdefghij"
n=int(input())
patterns = ["a"]
for _ in range(n-1):
    last_patterns = patterns.copy()
    patterns = []
    for string in last_patterns:
        useful_str = alpha[:len(set(string))+1]
        for str_ in useful_str:
            patterns.append(string + str_)

for pat in patterns:
    print(pat)

Buggy submission (verdict: Runtime Error):
n=int(input())
patterns = ["a"]
for _ in range(n-1):
    last_patterns = patterns.copy()
    patterns = []
    for string in last_patterns:
        useful_str = alpha[:len(set(string))+1]
        for str_ in useful_str:
            patterns.append(string + str_)

for pat in patterns:
    print(pat)
Problem p02909 (python)

Fixed submission:
s = input()
weather = {"Sunny":"Cloudy",
          "Cloudy":"Rainy",
          "Rainy":"Sunny"}
print(weather[s])

Buggy submission (verdict: Runtime Error):
n, k, q = list(map(int, input().split()))
score_b = [k - q]*n
for a in range(q):
    score_b[int(input())-1] += 1
for i in score_b:
    if i >0:
        print("Yes")
    else:
        print("No")
Problem p02981 (python)

Fixed submission:
n, a, b = input().split(' '); n, a, b = int(n), int(a), int(b)
 
if a*n <= b:
  print(a*n)
else:
  print(b)

Buggy submission (verdict: Runtime Error):
n, a, b = input().split(' '); n, a, b = int(n), int(a), int(b)

if a*n <= b:
  return a*n
else:
  return b
Problem p03274 (python)

Fixed submission:
import sys

N, K = map(int, sys.stdin.readline().strip().split(" "))
X = map(int, sys.stdin.readline().strip().split(" "))

isZero = False
zeroInd = 0
for x in X:
    if x == 0:
        isZero=True
    if x < 0:
        zeroInd+=1
    else:
        break

zeroInd-=1

if isZero:
    X.remove(0)
    K -= 1

if K == 0:
    print(0)
    sys.exit(0)

ans = []

for k in range(K+1):
    a = -1
    if zeroInd + k < len(X):
        a = X[zeroInd+k]

    b = -1
    if len(X) > zeroInd+k-K+1 >= 0:
        b = X[zeroInd+k-K+1] * -1

    if b != -1 and k == 0:
        ans.append(b)
    elif a != -1 and k == K:
        ans.append(a)
    elif a != -1 and b != -1:
        ans.append(a*2+b)
        ans.append(b*2+a)

print(min(ans))

Buggy submission (verdict: Runtime Error):
import sys

N, K = map(int, sys.stdin.readline().strip().split(" "))
X = map(int, sys.stdin.readline().strip().split(" "))

isZero = False
zeroInd = 0
for x in X:
    if x == 0:
        isZero=True
    if x < 0:
        zeroInd+=1
    else:
        break

zeroInd-=1

if isZero:
    X.remove(0)
    K -= 1

if K == 0:
    print(0)
    sys.exit(0)

ans = []

for k in range(K+1):
    a = -1
    if zeroInd + k < len(X):
        a = X[zeroInd+k]

    b = -1
    if len(X) > zeroInd+k-K+1 >= 0:
        b = X[zeroInd+k-K+1] * -1

    if b != -1 and k == 0:
        ans.append(b)
    elif a != -1 and k == K:
        ans.append(a)
    elif a != -1 and b != -1:
        ans.append(a*2+b)
        ans.append(b*2+a)

print(min(ans))
Problem p03493 (python)

Fixed submission:
# -*- coding: utf-8 -*-

import sys

for line in sys.stdin:
    print( sum( int(i) for i in line.strip() ) )

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
N = int(sys.stdin.readline())
line = sys.stdin.readline()
A = [ int(i) for i in line.strip().split() ]
min = 999999
for a in A:
    cnt = 0
    while(min > cnt):
        if a%2 == 1:
            min = cnt
            break
        else:
            a = a/2
            cnt += 1
print(cnt)
Problem p02612 (python)

Fixed submission:
cost = input()
amari = int(cost[-3:])
if amari == 0:
  amari = 1000
print(1000 - amari)

Buggy submission (verdict: Runtime Error):
cost = input()
amari = int(cost[-3:-1])

print(1000 - amari)
Problem p02658 (python)

Fixed submission:
count = int(input())
list = input().split(" ")
state = False
i = 0
num = int(list[i])
i += 1
limit = 10 ** 18
if "0" in list:
  num = 0
  count = 0

while i < count:
  num = num * int(list[i])

  if num > limit:
    print(-1)
    state = True
    break
    
  i += 1

if not state:
  print(num)

Buggy submission (verdict: Runtime Error):
count = input()
list = input().split(" ")

i = 0
num = int(list[i])
i += 1
while i>= count:
  num = num * int(list[i])
	
  if num > 10**18:
    print(-1)
    break
    
  i += 1
Problem p02973 (python)

Fixed submission:
import bisect
import collections
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
m = collections.deque([])
for i in range(0, n):
    j = bisect.bisect_left(m, a[i])
    if j == 0:
        m.appendleft(a[i])
    else:
        m[j - 1] = a[i]
print(len(m))

Buggy submission (verdict: Runtime Error):
import collections
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
c = collections.Counter(a)
print(c.most_common()[1])
Problem p03339 (python)

Fixed submission:
from itertools import accumulate
N = int(input())
s = input()
w = list(map(lambda x: 1 if x == 'W' else 0, s))
e = list(map(lambda x: 1 if x == 'E' else 0, s))
e.reverse()
w = list(accumulate(w))
e = list(accumulate(e))
e.reverse()
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

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

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(e)
e = np.add.accumulate(e)
e = np.flip(e)
ans = []
for i in range(N):
    num = 0
    ans.append(num)
print(min(ans))

Buggy submission (verdict: Runtime Error):
import numpy as np
# from itertools import accumulate

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(e)
e = np.add.accumulate(e)
e = np.flip(e)
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

Buggy submission (verdict: Runtime Error):
import numpy as np
# from itertools import accumulate

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(np.add.accumulate(np.flip(e)))
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

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

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(np.add.accumulate(np.flip(e)))
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))
Problem p02585 (python)

Fixed submission:
n, k = [int(z) for z in input().strip().split()]
p = [int(z) - 1 for z in input().strip().split()]
c = [int(z) for z in input().strip().split()]
 
visited = [False for _ in range(n)]
cycle = []
for start in range(n):
    if visited[start]:
        continue
    now = start
    route = []
    while not visited[now]:
        visited[now] = True
        route.append(now)
        now = p[now]
    cycle.append(route)
 
min_c = min(c)
max_value = min_c
for route in cycle:
    full_value = sum([c[i] for i in route])
    double_route = route + route
    v = [c[i] for i in double_route]
    max_sum_step = []
    for step in range(1, len(route)):
        s = sum(v[:step])
        max_sum = max(s, min_c)
        for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
        max_sum_step.append(max_sum)
    addition = k % len(route)
    if full_value > 0:
        addition_value = 0 if addition == 0 else max(max_sum_step[:addition])
        value1 = int(k / len(route)) * full_value + addition_value
        value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
        value = max(value1, value2)
    else:
        value = max(max_sum_step[:min(k, len(max_sum_step))])
    max_value = max(max_value, value)
print(max_value)

Buggy submission (verdict: Runtime Error):
n, k = [int(z) for z in input().strip().split()]
p = [int(z) - 1 for z in input().strip().split()]
c = [int(z) for z in input().strip().split()]

visited = [False for _ in range(n)]
cycle = []
for start in range(n):
    if visited[start]:
        continue
    now = start
    route = []
    while not visited[now]:
        visited[now] = True
        route.append(now)
        now = p[now]
    cycle.append(route)

min_c = min(c)
max_value = min_c
for route in cycle:
    full_value = sum([c[i] for i in route])
    double_route = route + route
    v = [c[i] for i in double_route]
    max_sum_step = []
    for step in range(1, len(route)):
        s = sum(v[:step])
        max_sum = max(s, min_c)
        for idx in range(step, step + len(route) - 1):
            s += v[idx]
            if idx >= step:
                s -= v[idx - step]
            max_sum = max(s, max_sum)
        max_sum_step.append(max_sum)
    addition = k % len(route)
    if full_value > 0:
        value1 = int(k / len(route)) * full_value + max(max_sum_step[:addition])
        value2 = (int(k / len(route)) - 1) * full_value + max(max_sum_step)
        value = max(value1, value2)
    else:
        value = max(max_sum_step[:min(k, len(max_sum_step))])
    max_value = max(max_value, value)
print(max_value)
Problem p02947 (python)

Fixed submission:
n = int(input())
dic = {}
for i in range(n):
  s = ''.join(sorted(list(input())))
  if s not in dic:
    dic[s] = 0
  dic[s] += 1
ans = 0
for i in dic.values():
  ans += i * (i - 1) // 2
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
dic = {}
for i in range(n):
  s = sorted(list(input()))
  if s not in dic:
    dic[s] = 0
  dic[s] += 1
ans = 0
for i in dic.values():
  ans += i * (i - 1)
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
dic = {}
for i in range(n):
  s = list(input()).sorted()
  if s not in dic:
    dic[s] = 0
  dic[s] += 1
ans = 0
for i in dic.values():
  ans += i * (i - 1)
print(ans)
Problem p03177 (python)

Fixed submission:
import numpy
M=10**9+7
n,k=map(int,input().split())
a=numpy.matrix([list(map(int,input().split()))for i in[0]*n],dtype='O')
b=numpy.eye(n,dtype='O')
while k:
 if k&1:b=b*a%M
 a=a*a%M
 k>>=1
print(b.sum()%M)

Buggy submission (verdict: Runtime Error):
import numpy as P
M=10**9+7
n,k=map(int,input().split())
a=P.matrix([list(map(int,input().split()))for i in[0]*n],dtype='O')
b=P.eye(n,dtype='O')
while k:
 if k&1:b=b*a%M
 a=a*a%M
 k>>=1
print(b.sum()%M)