Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
a = input()
A = int(a[0])
o = a[1]
B = int(a[2])
if o == "+":
 print(A+B)
else:
 print(A-B)

Fixed submission:
a = input().split()
A = int(a[0])
o = a[1]
B = int(a[2])
if o == "+":
 print(A+B)
else:
 print(A-B)
Buggy python submission (verdict: Runtime Error):
n = int(input())
lis = list(map(int,input().split()))
m = int(input())
num = sum(lis)
for i in range(n):
  a,b = map(int,input().split())
  print(num-(lis[a-1]-b))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
m = int(input())
for i in range(m):
    b,c = map(int,input().split())
    nu = a[b-1] -c
    print(sum(a)-nu)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n = int(input())
lis = list(map(int,input().split()))
if n % 2 == 0:key = 1
else:key = 2
for i in range(n//2):
    if lis.count(i*2+key) != 2:
        print("0")
        exit()
if n % 2 == 1:
    if lis.count(0) != 1:
        print("0")
        exit()
print(2 ** (n//2))

Fixed submission:
import math
n = int(input())
lis = list(map(int,input().split()))
li = [0 for _ in range(math.ceil(n / 2))]

for i in range(n):
  li[math.floor(lis[i] / 2)] += 1
for i in range(len(li)):
  if n % 2 == 1 and i == 0:
    if li[i] != 1:
      print("0")
      exit()
  else:
    if li[i] != 2:
      print("0")
      exit()
cou = 1
for i in range(math.floor(n/2)):
  cou *= 2
  cou %= (10 ** 9 + 7)
print(cou)

Buggy python submission (verdict: Runtime Error):
import math
n = int(input())
lis = list(map(str,input().split()))
li = [0 for _ in range(math.ceil(n / 2))]

for i in range(n):
  li[math.floor(lis[i] / 2)] += 1
for i in range(len(li)):
  if n % 2 == 1 and i == 0:
    if li[i] != 1:
      print("0")
      exit()
  else:
    if li[i] != 2:
      print("0")
      exit()
print(2 ** (math.floor(n/2)))

Fixed submission:
import math
n = int(input())
lis = list(map(int,input().split()))
li = [0 for _ in range(math.ceil(n / 2))]

for i in range(n):
  li[math.floor(lis[i] / 2)] += 1
for i in range(len(li)):
  if n % 2 == 1 and i == 0:
    if li[i] != 1:
      print("0")
      exit()
  else:
    if li[i] != 2:
      print("0")
      exit()
cou = 1
for i in range(math.floor(n/2)):
  cou *= 2
  cou %= (10 ** 9 + 7)
print(cou)

Buggy python submission (verdict: Time Limit Exceeded):
import math
from collections import Counter
n = int(input())
lis = list(map(str,input().split()))
if n % 2 == 1:
  for i in range(math.ceil(n / 2)):
    if i == 0:
      if lis.count("0") != 1:
        print("0")
        exit()
    else:
      if lis.count(str(i * 2)) != 2:
        print("0")
        exit()
  print(2 ** (math.floor(n/2)))
else:
  for i in range(n//2):
    if lis.count(str(i * 2 + 1)) != 2:
      print("0")
      exit()
  print(2 ** (n//2))

Fixed submission:
import math
n = int(input())
lis = list(map(int,input().split()))
li = [0 for _ in range(math.ceil(n / 2))]

for i in range(n):
  li[math.floor(lis[i] / 2)] += 1
for i in range(len(li)):
  if n % 2 == 1 and i == 0:
    if li[i] != 1:
      print("0")
      exit()
  else:
    if li[i] != 2:
      print("0")
      exit()
cou = 1
for i in range(math.floor(n/2)):
  cou *= 2
  cou %= (10 ** 9 + 7)
print(cou)
Buggy python submission (verdict: Runtime Error):
h,w=map(int,input().split())
num=0
s=[str(input()) for i in range(h):]
for i in range(h):
  num+=s[i].count("#")
if num>h+w-1:
  print("Impossible")
  exit()
print("Possible")

Fixed submission:
h,w = map(int,input().split())
x = 0
y = 0
time = 0
cou = 0
lis = ""
for i in range(h):lis += str(input())
for i in range(h * w):
    if lis[i] == "#":
        cou += 1
for i in range(h):
    while lis[i * w + x] == "#" and y == 0:
        x += 1
        time += 1
        if x == w:
            x -= 1
            y = 1
    if i == h-1 and time == (h + w)-1 and time == cou:
        print("Possible")
        exit()
    time += 1
print("Impossible")
Buggy python submission (verdict: Runtime Error):
lis = list(input())
cou = 0
ans = 0
for i in range(len(lis)):
	if lis[i] == "g":
		if cou > 0:
        	cou -= 1
        	ans += 1
		else:
        	cou += 1
	else:
		if cou > 0:
        	cou -= 1
		else:
			cou += 1
			ans -= 1
print(max(0,ans))


Fixed submission:
j = input().split()
a = int(j[2])
b = int(j[1])
c = int(j[0])
if a == b and b== c:
 print("1")
elif a == b:
 print("2")
elif b == c:
 print("2")
elif a==c:
 print("2")
else:
 print("3")

Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
if a == b and b== c:
 print("1")
elif a == b:
 print("2")
elif b == c:
 print("2")
elif a==c:
 print("2")
else:
 print("3")

Fixed submission:
j = input().split()
a = int(j[2])
b = int(j[1])
c = int(j[0])
if a == b and b== c:
 print("1")
elif a == b:
 print("2")
elif b == c:
 print("2")
elif a==c:
 print("2")
else:
 print("3")

Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
if a == b and b== c:
 print("1")
elif a == b or b ==c or a == c:
 print("2")
else:
 print("3")

Fixed submission:
j = input().split()
a = int(j[2])
b = int(j[1])
c = int(j[0])
if a == b and b== c:
 print("1")
elif a == b:
 print("2")
elif b == c:
 print("2")
elif a==c:
 print("2")
else:
 print("3")

Buggy python submission (verdict: Runtime Error):
a = int(input())
b = int(input())
c = int(input())
if a == b == c:
 print("1")
elif a == b or b ==c or a ==c:
 print("2")
else:
 print("3")

Fixed submission:
j = input().split()
a = int(j[2])
b = int(j[1])
c = int(j[0])
if a == b and b== c:
 print("1")
elif a == b:
 print("2")
elif b == c:
 print("2")
elif a==c:
 print("2")
else:
 print("3")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
t = 0
r = 0
for i in range(n):
    a,b = map(int,input().split())
    g = a
    h = b
    while a < t or b < r:
        a += g
        b += h
    t = a
    r = b
print(a + b)

Fixed submission:
n = int(input())
t = 1; a = 1;
for _ in range(n):
    ti,ai = map(int,input().split())
    if t%ti != 0 : t = (t//ti+1)*ti
    if a%ai != 0 : a = (a//ai+1)*ai
    r = max(t//ti,a//ai)
    t = ti*r
    a = ai*r

print(a+t)
Buggy python submission (verdict: Runtime Error):
lis = list(input())
cou = 0
ans = 0
for i in range(len(lis)):
	if lis[i] == "g":
		if cou > 0:
        	cou -= 1
        	ans += 1
		else:
        	cou += 1
	else:
		if cou > 0:
        	cou -= 1
		else:
			cou += 1
			ans -= 1
print(max(0,ans))


Fixed submission:
lis = list(input())
cou = 0
ans = 0
for i in range(len(lis)):
	if lis[i] == "g":
		if cou > 0:
			cou -= 1
			ans += 1
		else:
			cou += 1
	else:
		if cou > 0:
			cou -= 1
		else:
			cou += 1
			ans -= 1
print(max(0,ans))

Buggy python submission (verdict: Runtime Error):
lis = list(input())
cou = 0
ans = 0
for i in range(len(lis)):
	if lis[i] == "g":
      if cou > 0:
        cou -= 1
        ans += 1
      else:
        cou += 1
    else:
      if cou > 0:
        cou -= 1
      else:
        cou += 1
        ans -= 1
print(max(0,ans))

Fixed submission:
lis = list(input())
cou = 0
ans = 0
for i in range(len(lis)):
	if lis[i] == "g":
		if cou > 0:
			cou -= 1
			ans += 1
		else:
			cou += 1
	else:
		if cou > 0:
			cou -= 1
		else:
			cou += 1
			ans -= 1
print(max(0,ans))
Buggy python submission (verdict: Runtime Error):
n = int(input())
lis = list(map(int,input().split()))
ans = 0
for i in range(n):
  if lis[lis[i]] == i+1:
    ans += 1
print(ans//2)

Fixed submission:
n = int(input())
cou = 0
ans = 1
lis = list(map(int,input().split()))
for i in range(n):
    a = lis[i]
    if lis[a-1] == i + 1:
        ans += 1
print(ans//2)

Buggy python submission (verdict: Runtime Error):
n =int(input())
lis =list(map(int,input().split()))
ans =0
for i in range(n):
  if lis[lis[i]-1]=i+1:
    ans +=1
print(ans//2)

Fixed submission:
n = int(input())
cou = 0
ans = 1
lis = list(map(int,input().split()))
for i in range(n):
    a = lis[i]
    if lis[a-1] == i + 1:
        ans += 1
print(ans//2)
Buggy python submission (verdict: Runtime Error):
from collections import Counter
h, w, num = map(int, input().split())
lis = []
for i in range(num):
    k, j = map(lambda x: int(x) - 1,input().split())
    for a in range(3):
        for b in range(3):
            cou1 = k - a
            cou2 = j - b 
            if cou1 < 0 or cou1 > h - 3:
                continue
            if cou2 < 0 or cou2 > w - 3:
                continue
            lis.append((cou1, cou2))

lis = Counter(Counter(lis).values())

print((h - 2) * (w - 2) - sum(lis.values()))
for i in range(1, 10):print(lis[i])

Fixed submission:
n = str(input())
ans = 0
if int(n) < 10:
  print(int(n))
  exit()
for i in range(2 ** (len(n) -1)):
  num = format(i,"b").zfill(len(n)-1)
  lis = ""
  for j in range(len(str(num))):
    if str(num[j]) == "1":
      lis += (str(n[j])+"+")
    else:
      lis += str(n[j])
  lis += n[-1]
  li = lis.split("+")
  for h in range(len(li)):
    li[h] = int(li[h])
  ans += sum(li)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,x = map(int,input().split())
lis = list(map(int,input().split()))
ans = 0
magic = 0
for i in range(n):
    cou = lis[i]
    for m in range(1,n):
        if i + m >= n:co = i + m - n
        else:co = i + m
        cou = min(cou,lis[co]+x * max(m-magic,0))
        if cou == lis[co] + x * max(m-magic,0):magic = max(m,magic)
    ans += cou
print(ans)

Fixed submission:
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
s_cost = a.copy()
cost = sum(s_cost)
for k in range(1,N):
    for i in range(N):
        if s_cost[i] > a[(i-k)%N] :
            s_cost[i] = a[(i-k)%N]
    if cost > k*x + sum(s_cost):
        cost = k*x + sum(s_cost)
    else:
        break

print(cost)

Buggy python submission (verdict: Time Limit Exceeded):
n,x = map(int,input().split())
lis = list(map(int,input().split()))
ans = 0
magic = 0
for i in range(n):
    cou = lis[i]
    for m in range(1,n):
        if i + m >= n:
            co = i + m - n
        else:
            co = i + m
        cou = min(cou,lis[co]+x * max(m-magic,0))
        if cou == lis[co] + x * max(m-magic,0):
            magic = max(m,magic)
    ans += cou
print(ans)

Fixed submission:
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
s_cost = a.copy()
cost = sum(s_cost)
for k in range(1,N):
    for i in range(N):
        if s_cost[i] > a[(i-k)%N] :
            s_cost[i] = a[(i-k)%N]
    if cost > k*x + sum(s_cost):
        cost = k*x + sum(s_cost)
    else:
        break

print(cost)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = sorted(map(int,input().split()))
b = sorted(map(int,input().split()))
c = sorted(map(int,input().split()))

lis = [0] * n
j = 0
for i in range(n):
    while j < n and a[j] < b[i]:
        j += 1
    lis[i] = j

li = [0] * n
m = 0
j = 0
for i in range(n):
    while j < n and b[j] < c[i]:
        m += lis[j]
        j += 1
    li[i] = m
print(sum(li))


Fixed submission:
n, a = map(int, input().split())
x = list(map(lambda x: int(x) - a, input().split()))
dp = [[0 for j in range(100 * n + 1)] for i in range(n + 1)]
dp[0][50 * n] = 1
for i in range(n):
    for j in range(50, 100 * n + 1 - 50):
        dp[i + 1][j] = dp[i][j] + dp[i][j - x[i]]
print(dp[n][50 * n] - 1)
Buggy python submission (verdict: Runtime Error):
a, b, c = map(int,input().split())
if a == 7:
 if b == c and c ==5:
  print("YES")
 else:
  print("NO")
elif a == 5:
 if b ==7 and c == 5:
  print("YES")
 ellif b == 5 and c ==7:
  print("YES")
 else:
  print("NO")
else:
 print("NO")

Fixed submission:
ABC = list(map(int,input().split()))
ABC.sort()
if ABC[0] == 5 and ABC[1] == 5 and ABC[2] == 7:
 print("YES")
else:
 print("NO")

Buggy python submission (verdict: Runtime Error):
a, b, c = map(int,input().split())
if a == 7:
 if b == c ==5:
  print("Yes")
 else:
  print("No")
elif a == 5:
 if b ==7 and c == 5:
  print("Yes")
 ellif b == 5 and c ==7:
  print("Yes")
 else:
  print("No")
else:
 print("No")

Fixed submission:
ABC = list(map(int,input().split()))
ABC.sort()
if ABC[0] == 5 and ABC[1] == 5 and ABC[2] == 7:
 print("YES")
else:
 print("NO")
Buggy python submission (verdict: Runtime Error):
n,x = map(int,input().split())
import fractions
print+(3 * (n-fractions.gcd(n,x)))

Fixed submission:
n,x = map(int,input().split())
import fractions
print(3 * (n-fractions.gcd(n,x)))
Buggy python submission (verdict: Runtime Error):
class UnionFind:
  def __init__(self, n):
    self.n = [-1]*n
    self.r = [0]*n
    self.co = n

  def find(self, x):
    if self.n[x] < 0:
      return x
    else:
      return self.n[x] = self.find(self.n[x])

  def unite(self, x, y):
    x = self.find(x)
    y = self.find(y)
    if x == y:
      return
    if self.r[x] > self.r[y]:
      x, y = y, x
    self.n[y] += self.n[x]
    self.n[x] = y
    if self.r[x] == self.r[y]:
      self.r[y] += 1
    self.co -= 1

  def same(self, x, y):
    return self.find(x) == self.find(y)

  def size(self, x):
    return -self.n[self.find(x)]

  def set_count(self):
    return self.co
  
n, m = map(int, input().split())
uf = UnionFind(n)
for _ in range(m):
  a, b = map(int, input().split())
  uf.unite(a-1, b-1)
print(uf.set_count()-1)

Fixed submission:
# UnionFind
import sys
sys.setrecursionlimit(1000000)
class UnionFind:
  def __init__(self, n):
    self.n = [-1]*n
    self.r = [0]*n
    self.siz = n

  def find_root(self, x):
    if self.n[x] < 0:
      return x
    else:
      self.n[x] = self.find_root(self.n[x])
      return self.n[x]

  def unite(self, x, y):
    x = self.find_root(x)
    y = self.find_root(y)
    if x == y:
      return
    elif self.r[x] > self.r[y]:
      self.n[x] += self.n[y]
      self.n[y] = x
    else:
      self.n[y] += self.n[x]
      self.n[x] = y
      if self.r[x] == self.r[y]:
        self.r[y] += 1
    self.siz -= 1

  def root_same(self, x, y):
    return self.find_root(x) == self.find_root(y)

  def count(self, x):
    return -self.n[self.find_root(x)]

  def size(self):
    return self.siz

n,m=map(int,input().split())
u=UnionFind(n)
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  u.unite(a,b)
print(u.size()-1)
Buggy python submission (verdict: Time Limit Exceeded):
# BinaryIndexedTree
# O(logN)で累積和取得と値の更新
# add(i,x)...i番目にxを加算
# sum(i,j)...[i,j)の累積和を取得
# sum_sub(i)...[0:i]の累積和を取得
class BinaryIndexedTree:
  def __init__(self, n):
    self.bit = [0] * n

  def add(self, i, x):
    i += 1
    while i <= len(self.bit):
      self.bit[i-1] += x
      i += i & -i

  def sum_sub(self, i):
    a = 0
    i += 1
    while i:
      a += self.bit[i-1]
      i -= i & -i
    return a

  def sum(self, i, j):
    a = 0
    if j != 0:
      a += self.sum_sub(j-1)
    if i != 0:
      a -= self.sum_sub(i-1)
    return a
mod=998244353
n,k=map(int,input().split())
lr=[]
for i in range(k):
  l,r=map(int,input().split())
  lr.append((l,r))
bit=BinaryIndexedTree(2*n+7)
bit.add(0,1)
bit.add(1,-1)
for i in range(n-1):
  x=bit.sum_sub(i)%mod
  for l,r in lr:
    bit.add(i+l,x)
    bit.add(i+r+1,-x)
print(bit.sum_sub(n-1)%mod)

Fixed submission:
import sys
input=lambda:sys.stdin.readline().rstrip()

# BinaryIndexedTree
# O(logN)で累積和取得と値の更新
# add(i,x)...i番目にxを加算
# sum(i,j)...[i,j)の累積和を取得
# sum_sub(i)...[0:i]の累積和を取得
class BinaryIndexedTree:
  def __init__(self, n):
    self.bit = [0] * n

  def add(self, i, x):
    i += 1
    while i <= len(self.bit):
      self.bit[i-1] += x
      i += i & -i

  def sum_sub(self, i):
    a = 0
    i += 1
    while i:
      a += self.bit[i-1]
      i -= i & -i
    return a

  def sum(self, i, j):
    a = 0
    if j != 0:
      a += self.sum_sub(j-1)
    if i != 0:
      a -= self.sum_sub(i-1)
    return a
mod=998244353
n,k=map(int,input().split())
lr=[]
for i in range(k):
  l,r=map(int,input().split())
  lr.append((l,r))
bit=BinaryIndexedTree(2*n+7)
bit.add(0,1)
bit.add(1,-1)
for i in range(n-1):
  x=bit.sum_sub(i)%mod
  for l,r in lr:
    bit.add(i+l,x)
    bit.add(i+r+1,-x)
print(bit.sum_sub(n-1)%mod)
Buggy python submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip()

# UnionFind
class UnionFind:
    def __init__(self,n):
        self.n=n
        self.a=[-1]*n
        self.siz=n
    def root_find(self,x):
        if self.a[x]<0:
            return x
        else:
            self.a[x]=self.root_find(self.a[x])
            return self.a[x]
    def unite(self,x,y):
        x=self.root_find(x)
        y=self.root_find(y)
        if self.a[x]<self.a[y]:
            self.a[x]+=self.a[y]
            self.a[y]=x
        else:
            self.a[y]+=self.a[x]
            self.a[x]=y
    def root_same(self,x,y):
        return self.root_find(x)==self.root_find(y)

n,q=map(int,input().split())
u=UnionFind(n)
for _ in range(q):
    t,a,b=map(int,input().split())
    if t==0:
        u.unite(a,b)
    else:
        print(u.root_same(a,b)+0)

Fixed submission:
# UnionFind
import sys
sys.setrecursionlimit(10**9)
class UnionFind:
  def __init__(self, n):
    self.n = [-1]*n
    self.r = [0]*n
    self.siz = n

  def find_root(self, x):
    if self.n[x] < 0:
      return x
    else:
      self.n[x] = self.find_root(self.n[x])
      return self.n[x]

  def unite(self, x, y):
    x = self.find_root(x)
    y = self.find_root(y)
    if x == y:
      return
    elif self.r[x] > self.r[y]:
      self.n[x] += self.n[y]
      self.n[y] = x
    else:
      self.n[y] += self.n[x]
      self.n[x] = y
      if self.r[x] == self.r[y]:
        self.r[y] += 1
    self.siz -= 1

  def root_same(self, x, y):
    return self.find_root(x) == self.find_root(y)

  def count(self, x):
    return -self.n[self.find_root(x)]

  def size(self):
    return self.siz

n,q=map(int,input().split())
uf=UnionFind(n)
for _ in range(q):
  t,u,v=map(int,input().split())
  if t==0:
    uf.unite(u,v)
  else:
    print(uf.root_same(u,v)+0)
Buggy python submission (verdict: Time Limit Exceeded):
h,w,m=map(int,input().split())
hh=[[0,i]for i in range(h)]
ww=[[0,i]for i in range(w)]
hw=set()
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  hh[a][0]+=1
  ww[b][0]+=1
  hw.add((a,b))
ww.sort(reverse=1)
s=ww[0][0]
w=[]
for x,j in ww:
  if x==s:w.append(j)
ans=0
t=max(hh)[0]
for j in w:
  for x,i in sorted(hh,reverse=1):
    if x!=t:break
    if (i,j) in hw:
      ans=s+x-1
    else:
      exit(print(s+x))
print(ans)


Fixed submission:
import random
import time
start=time.time()
h,w,m=map(int,input().split())
hh=[[0,i]for i in range(h)]
ww=[[0,i]for i in range(w)]
hw=set()
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  hh[a][0]+=1
  ww[b][0]+=1
  hw.add((a,b))
ww.sort(reverse=1)
s=ww[0][0]
w=[]
for x,j in ww:
  if x==s:w.append(j)
hh.sort(reverse=1)
t=hh[0][0]
hhh=[]
for x,i in hh:
  if x==t:hhh.append((x,i))
ans=[]
random.shuffle(w)
for j in w:
  for x,i in hhh:
    if (i,j) in hw:
      ans.append(s+x-1)
    else:
      exit(print(s+x))
  if time.time()-start>=1.7:
    m=ans[0]
    if h-50000<=len(hhh)<=h-random.randint(1,30):m+=1
    exit(print(m))
print(ans[0])

Buggy python submission (verdict: Runtime Error):
import random
import time
start=time.time()
h,w,m=map(int,input().split())
hh=[[0,i]for i in range(h)]
ww=[[0,i]for i in range(w)]
hw=set()
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  hh[a][0]+=1
  ww[b][0]+=1
  hw.add((a,b))
ww.sort(reverse=1)
s=ww[0][0]
w=[]
for x,j in ww:
  if x==s:w.append(j)
hh.sort(reverse=1)
t=hh[0][0]
hhh=[]
for x,i in hh:
  if x==t:h.append((x,i))
ans=[]
random.shuffle(w)
for j in w:
  for x,i in hhh:
    if (i,j) in hw:
      ans.append(s+x-1)
    else:
      exit(print(s+x))
  if time.time()-start>=1.7:
    m=ans[0]
    if h-50000<=len(hhh)<=h-random.randint(1,30):m+=1
    exit(print(m))
print(ans[0])

Fixed submission:
import random
import time
start=time.time()
h,w,m=map(int,input().split())
hh=[[0,i]for i in range(h)]
ww=[[0,i]for i in range(w)]
hw=set()
for _ in range(m):
  a,b=map(int,input().split())
  a-=1
  b-=1
  hh[a][0]+=1
  ww[b][0]+=1
  hw.add((a,b))
ww.sort(reverse=1)
s=ww[0][0]
w=[]
for x,j in ww:
  if x==s:w.append(j)
hh.sort(reverse=1)
t=hh[0][0]
hhh=[]
for x,i in hh:
  if x==t:hhh.append((x,i))
ans=[]
random.shuffle(w)
for j in w:
  for x,i in hhh:
    if (i,j) in hw:
      ans.append(s+x-1)
    else:
      exit(print(s+x))
  if time.time()-start>=1.7:
    m=ans[0]
    if h-50000<=len(hhh)<=h-random.randint(1,30):m+=1
    exit(print(m))
print(ans[0])
Buggy python submission (verdict: Time Limit Exceeded):
def solve(n,x,k):
  ans=-10**20
  s=0
  sx=sum(x)
  for i in range(n):
    if (i+1)>k:break
    s+=x[i]
    d=((k-i-1)//n)*sx
    ans=max(ans,s+d,s)
  return ans

n,k=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[c[i]]
  j=p[i]
  while i!=j:
    x.append(c[j])
    j=p[j]
  x=x[1:]+[x[0]]
  ans=max(ans,solve(len(x),x,k))
print(ans)


Fixed submission:
def solve(n,x,k):
  ans=-10**20
  s=0
  sx=sum(x)
  for i in range(n):
    if (i+1)>k:break
    s+=x[i]
    d=((k-i-1)//n)*sx
    ans=max(ans,s+d,s)
  return ans

n,k=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[c[i]]
  j=p[i]
  while i!=j:
    x.append(c[j])
    j=p[j]
  x=x[1:]+[x[0]]
  ans=max(ans,solve(len(x),x,k))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[]
  for _ in range(k):
    i=p[i]
    x.append(c[i])
  anss=-10**20
  s=x[0]
  anss=max(anss,s)
  for i in x[1:]:
    s+=i
    anss=max(anss,s)
  ans=max(ans,anss)
print(ans)

Fixed submission:
def solve(n,x,k):
  ans=-10**20
  s=0
  sx=sum(x)
  for i in range(n):
    if (i+1)>k:break
    s+=x[i]
    d=((k-i-1)//n)*sx
    ans=max(ans,s+d,s)
  return ans

n,k=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[c[i]]
  j=p[i]
  while i!=j:
    x.append(c[j])
    j=p[j]
  x=x[1:]+[x[0]]
  ans=max(ans,solve(len(x),x,k))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[]
  for _ in range(k):
    i=p[i]
    x.append(c[i])
  anss=-10**20
  s=x[0]
  anss=max(anss,s)
  for i in x[1:]:
    s+=i
    anss=max(anss,s)
  ans=max(ans,anss)
print(ans)

Fixed submission:
def solve(n,x,k):
  ans=-10**20
  s=0
  sx=sum(x)
  for i in range(n):
    if (i+1)>k:break
    s+=x[i]
    d=((k-i-1)//n)*sx
    ans=max(ans,s+d,s)
  return ans

n,k=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
c=list(map(int,input().split()))
ans=-10**20
for i in range(n):
  x=[c[i]]
  j=p[i]
  while i!=j:
    x.append(c[j])
    j=p[j]
  x=x[1:]+[x[0]]
  ans=max(ans,solve(len(x),x,k))
print(ans)