Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n = int(input())
  flag = 0
  while n > 0:
      if n % 10 == 7:
          flag += 1
          n /= 10
  if flag > 0:
      print('Yes')
  else:
      print('No')
main()

Fixed submission:
def main():
  n = input()
  
  if n[0] == '7' or n[1] == '7' or n[2] == '7':
      print('Yes')
  else:
      print('No')
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n = int(input())
  flag = 0
  while n > 0:
      if n % 10 == 7:
          flag += 1
          n /= 10
  if flag > 0:
      print('Yes')
  else:
      print('No')
main()

Fixed submission:
def main():
  n = input()
  
  if n[0] == '7' or n[1] == '7' or n[2] == '7':
      print('Yes')
  else:
      print('No')
main()
Buggy python submission (verdict: Runtime Error):

def main():
 a,b,c = map(int,input().split())
 rhs = c - a - b
 lhs = 4 * a * b
 if rhs > 0  and lhs ** 2 < rhs:
  print('Yes')
 else:
  print('No')
main(

Fixed submission:

def main():
 a,b,c = map(int,input().split())
 rhs = c - a - b
 lhs = 4 * a * b
 if rhs > 0  and lhs < rhs ** 2:
  print('Yes')
 else:
  print('No')
main()

Buggy python submission (verdict: Runtime Error):
import decimal
def main():
 a,b,c = map(decimal.Decimal,input().split())
 if a ** 0.5 + b ** 0.5 < c ** 0.5:
  print('Yes')
 else:
  print('No')
main()

Fixed submission:

def main():
 a,b,c = map(int,input().split())
 rhs = c - a - b
 lhs = 4 * a * b
 if rhs > 0  and lhs < rhs ** 2:
  print('Yes')
 else:
  print('No')
main()
Buggy python submission (verdict: Runtime Error):
def main():
 n, r = map(int,input().split())
 if n >= 10:
     print(r)
 else:
     print(r + 100* (10 - k))
main()

Fixed submission:
def main():
 n, r = map(int,input().split())
 if n >= 10:
     print(r)
 else:
     print(r + 100* (10 - n))
main()
Buggy python submission (verdict: Runtime Error):
def main():
 s,t = input().split()
 a,b = int(input().split())
 u = input()
 if s == u:
     a -= 1
 else:
     b -= 1
 print(a,b)
main()

Fixed submission:
def main():
 s,t = input().split()
 a,b = map(int,input().split())
 u = input()
 if s == u:
     a -= 1
 else:
     b -= 1
 print(a,b)
main()

Buggy python submission (verdict: Runtime Error):
def main():
 s,t = input().split()
 a,b = int(input().split())
 u = input()
 if s == u:
     a -= 1
 else:
     b -= 1
main()

Fixed submission:
def main():
 s,t = input().split()
 a,b = map(int,input().split())
 u = input()
 if s == u:
     a -= 1
 else:
     b -= 1
 print(a,b)
main()
Buggy python submission (verdict: Runtime Error):
def main():
	a,b,c = map(int,input().split())
	if a + b + c >= 22:
    	print('bust')
	else:
    	print('win')
main()

Fixed submission:
def main():
  a,b,c = map(int,input().split())
  if a + b + c >= 22:
      print('bust')
  else:
      print('win')
main()
Buggy python submission (verdict: Runtime Error):
def main():
 s = input()
 if s[0] == s[4] and s[1] == s[2] and s[2] != s[3]:
     print('Yes')
 else:
     print('No')
main()

Fixed submission:
def main():
 s = sorted(input())
 if s[0] == s[1] and s[3] == s[2] and s[1] != s[2]:
     print('Yes')
 else:
     print('No')
main()
Buggy python submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    s = list(input())
    s[k - 1] = chr(ord(s[k]) + 32)
    t = ''
    for i in s:
        t = t + i
    print(t)
main()

Fixed submission:
def main():
    n,k = map(int,input().split())
    s = list(input())
    s[k - 1] = chr(ord(s[k - 1]) + 32)
    t = ''
    for i in s:
        t = t + i
    print(t)
main()

Buggy python submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    s = list(input())
    s[k] = chr(ord(s[k]) + 32)
    t = ''
    for i in s:
        t = t + i
    print(t)
main()

Fixed submission:
def main():
    n,k = map(int,input().split())
    s = list(input())
    s[k - 1] = chr(ord(s[k - 1]) + 32)
    t = ''
    for i in s:
        t = t + i
    print(t)
main()
Buggy python submission (verdict: Runtime Error):
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in range(t + 0.51, 1):
        if i % a == 0:
            ans += b
    print(ans)
main()

Fixed submission:
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in range(1,t + 1, 1):
        if i % a == 0:
            ans += b
    print(ans)
main()

Buggy python submission (verdict: Runtime Error):
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in (t + 1):
        if i % a == 0:
            ans += b
    print(ans)
main()

Fixed submission:
def main():
    a,b,t = map(int,input().split())
    ans = 0
    for i in range(1,t + 1, 1):
        if i % a == 0:
            ans += b
    print(ans)
main()
Buggy python submission (verdict: Runtime Error):
def main():
    n = int(input())
    a = list([(int(input())) % 2 for i in range(n)])
    su = sum(a)
    xors = 0
    for i in(a):
        xors ^= i
    if(xors != 0 || sum == n):
        print('first')
    else:
        print('second')

main()

Fixed submission:
def main():
    n = int(input())
    a = list([(int(input())) % 2 for i in range(n)])
    ans = sum(a)
    if(ans == 0):
        print('second')
    else:
        print('first')

main()

Buggy python submission (verdict: Runtime Error):
def main():
    n = int(input())
    a = [int(input) for i in range(n)]
    ans = sum(a)
    if(ans % 2 == 0):
        print('second')
    else:
        print('first')

main()

Fixed submission:
def main():
    n = int(input())
    a = list([(int(input())) % 2 for i in range(n)])
    ans = sum(a)
    if(ans == 0):
        print('second')
    else:
        print('first')

main()
Buggy python submission (verdict: Runtime Error):
def main():
 D = int(input().split())
 if D == 25:
     print('Christmas')
 elif D == 24:
     print('Christmas Eve')
 elif D == 23:
     print('Christmas Eve Eve')
 else:
     print('Christmas Eve Eve')
main()

Fixed submission:
def main():
 D = int(input())
 if D == 25:
     print('Christmas')
 elif D == 24:
     print('Christmas Eve')
 elif D == 23:
     print('Christmas Eve Eve')
 else:
     print('Christmas Eve Eve Eve')
main()
Buggy python submission (verdict: Runtime Error):
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or abs(a - b) + abs(b - c) <= d:
main()

Fixed submission:
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or (abs(a - b) <= d and abs(b - c) <= d):
     print('Yes')
 else:
     print('No')
main()

Buggy python submission (verdict: Runtime Error):
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or abs(a - b) + abs(b - c) <= d
main()

Fixed submission:
def main():
 a,b,c,d = map(int,input().split())
 if abs(a - c) <= d or (abs(a - b) <= d and abs(b - c) <= d):
     print('Yes')
 else:
     print('No')
main()
Buggy python submission (verdict: Runtime Error):
def main():
  n = int(input())
  s = [input() for i in range(n)]
  nums = [1000 for i in range(26)]
  for i in range(26):
      for j in range(n):
          nums[i] = min(nums[i],s[j].count((chr)(97 + i)))

  for i in range(n):
    while nums[i] > 0:
        print((chr)(97 + i),end = "")
        nums[i] -= 1
  print("")

main()

Fixed submission:
def main():
  n = int(input())
  s = [input() for i in range(n)]
  nums = [1000 for i in range(26)]
  for i in range(26):
      for j in range(n):
          nums[i] = min(nums[i],s[j].count((chr)(97 + i)))

  for i in range(26):
    while nums[i] > 0:
        print((chr)(97 + i),end = "")
        nums[i] -= 1
  print("")

main()
Buggy python submission (verdict: Runtime Error):


i = list(map(int, input().split())) 
j = list(map(int, input().split())) 

lis=[]
for i in range (i[0]):
    tendata=[i+1,j[i],0]
    lis.append(tendata)

for i in range(i[1]):
    miti = list(map(int, input().split()))
    if ((lis[miti[0]-1][1]) > (lis[miti[1]][1])):
        lis[miti[0]-1][2] = (lis[miti[0]-1][2]+100)
        lis[miti[1]-1][2] = (lis[miti[1]-1][2]+2)

    elif ((lis[miti[0]-1][1]) < (lis[miti[1]][1])):
        lis[miti[1]-1][2] = (lis[miti[1]-1][2]+100)
        lis[miti[0]-1][2] = (lis[miti[0]-1][2]+2)


print(lis)

Fixed submission:


i = list(map(int, input().split())) 
j = list(map(int, input().split())) 

lis=[]
ans = 0
for a in range (i[0]):
    tendata=[a+1,j[a],0]
    lis.append(tendata)

for b in range(i[1]):
    miti = list(map(int, input().split()))
    if ((lis[miti[0]-1][1]) > (lis[miti[1]-1][1])):
        lis[miti[1]-1][2] = 1
    
    elif ((lis[miti[0]-1][1]) == (lis[miti[1]-1][1])):
        lis[miti[1]-1][2] = 1
        lis[miti[0]-1][2] = 1

    else:
        lis[miti[0]-1][2] = 1

for c in  lis:
    if not (c[2] == 1 ):
        ans = ans+1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
MOD=998244353
N,K=map(int,input().split())

lrlist=[]
for _ in range(K):
  l,r=map(int,input().split())
  lrlist.append((l,r))
#print(lrlist)

#A1～Aiまでの和 O(logN)
def BIT_query(idx):
  res_sum = 0
  while idx > 0:
    res_sum += BIT[idx]
    idx -= idx&(-idx)
  return res_sum
#Ai += x O(logN)
def BIT_update(idx,x):
  while idx <= N:
    BIT[idx] += x
    idx += idx&(-idx)
  return  

#初期化
BIT=[0]*(N+1)
BIT_update(1,1)

for i in range(2,N+1):
  for l,r in lrlist:
    left=i-r
    right=i-l    
    if right<1:
      continue
    elif left<1:
      left=1
    
    sum_intv=BIT_query(right)-BIT_query(left-1)
    BIT_update(i,sum_intv)
    
print((BIT_query(N)-BIT_query(N-1))%MOD)

Fixed submission:
MOD=998244353
N,K=map(int,input().split())

lrlist=[]
for _ in range(K):
  l,r=map(int,input().split())
  lrlist.append((l,r))
#print(lrlist)

class SegTree:
  #init(init_val, ide_ele): 配列init_valで初期化 O(N)
  def __init__(self, init_val, segfunc, ide_ele):
    #init_val: 配列の初期値
    #segfunc: 区間にしたい操作
    #ide_ele: 単位元
    #n: 要素数
    #num: n以上の最小の2のべき乗
    #tree: セグメント木(1-index)
    n = len(init_val)
    self.segfunc = segfunc
    self.ide_ele = ide_ele
    self.num = 1 << (n - 1).bit_length()
    self.tree = [ide_ele] * 2 * self.num
    # 配列の値を葉にセット
    for i in range(n):
      self.tree[self.num + i] = init_val[i]
    # 構築していく
    for i in range(self.num - 1, 0, -1):
      self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

  #update(k, x): k番目の値をxに更新 O(logN)
  def update(self, k, x):
    #k番目の値をxに更新
    #k: index(0-index)
    #x: update value
    k += self.num
    self.tree[k] = x
    while k > 1:
      self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
      k >>= 1

  #query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
  def query(self, l, r):
    #[l, r)のsegfuncしたものを得る
    #l: index(0-index)
    #r: index(0-index)
    res = self.ide_ele
    l += self.num
    r += self.num
    while l < r:
      if l & 1:
        res = self.segfunc(res, self.tree[l])
        l += 1
      if r & 1:
        res = self.segfunc(res, self.tree[r - 1])
      l >>= 1
      r >>= 1
    return res
  
#segfunc
def segfunc(x,y):
  return x+y #区間和
#ide_ele
ide_ele=0 #区間和

#初期化
seg=SegTree([0]*(N+1), segfunc, ide_ele)
seg.update(1,1)

for i in range(2,N+1):
  for l,r in lrlist:
    left=i-r
    right=i-l    
    if right<1:
      continue
    elif left<1:
      left=1
    
    current=seg.query(i,i+1)
    sum_intv=seg.query(left,right+1)
    seg.update(i,(current+sum_intv)%MOD)
    #print(i,left,right,current,sum_intv)
    
print(seg.query(N,N+1))
Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7
import sys
import math
import random
N=int(input())
alist=list(map(int,input().split()))
#alist.sort()
random.shuffle(alist)

all_GCD=alist[0]
for i in range(1,N):
  all_GCD=math.gcd(all_GCD,alist[i])
  if all_GCD==1:
    break
else:
  print("not coprime")
  sys.exit(0)
  
lcm=1
prod=1
for i in range(N):
  lcm=lcm*alist[i]//math.gcd(lcm,alist[i])
  prod*=alist[i]
  if lcm!=prod:    
    print("setwise coprime")
    sys.exit(0)
    
print("pairwise coprime")

Fixed submission:
import sys
import math
N=int(input())
alist=list(map(int,input().split()))

all_GCD=alist[0]
for i in range(1,N):
  all_GCD=math.gcd(all_GCD,alist[i])
  if all_GCD==1:
    break
else:
  print("not coprime")
  sys.exit(0)

max_a=max(alist)  
dlist=[i for i in range(max_a+1)]
for i in range(2,int(max_a**0.5)+1):
  if dlist[i]<i:
    continue
  else:
    for j in range(i**2,max_a+1,i):
      dlist[j]=i  
#print(dlist)

fact_set=set()
for i in range(N):
  ai=alist[i]
  facts=set()
  while ai>1:
    facts.add(dlist[ai])
    ai//=dlist[ai]    
  #print(facts)
  
  for f in facts:
    if f in fact_set:
      print("setwise coprime")
      sys.exit(0)
    else:
      fact_set.add(f)
else:
  print("pairwise coprime")

Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7
import sys
import math
N=int(input())
alist=list(map(int,input().split()))
alist.sort()

all_GCD=alist[0]
for i in range(1,N):
  all_GCD=math.gcd(all_GCD,alist[i])
  if all_GCD==1:
    break
else:
  print("not coprime")
  sys.exit(0)
  
lcm=1
prod=1
for i in range(N):
  lcm=lcm*alist[i]*math.gcd(lcm,alist[i])
  prod*=alist[i]
  if lcm!=prod:    
    print("setwise coprime")
    sys.exit(0)
    
print("pairwise coprime")

Fixed submission:
import sys
import math
N=int(input())
alist=list(map(int,input().split()))

all_GCD=alist[0]
for i in range(1,N):
  all_GCD=math.gcd(all_GCD,alist[i])
  if all_GCD==1:
    break
else:
  print("not coprime")
  sys.exit(0)

max_a=max(alist)  
dlist=[i for i in range(max_a+1)]
for i in range(2,int(max_a**0.5)+1):
  if dlist[i]<i:
    continue
  else:
    for j in range(i**2,max_a+1,i):
      dlist[j]=i  
#print(dlist)

fact_set=set()
for i in range(N):
  ai=alist[i]
  facts=set()
  while ai>1:
    facts.add(dlist[ai])
    ai//=dlist[ai]    
  #print(facts)
  
  for f in facts:
    if f in fact_set:
      print("setwise coprime")
      sys.exit(0)
    else:
      fact_set.add(f)
else:
  print("pairwise coprime")
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

def func(x,y,z):
  return x*x+y*y+z*z+x*y+y*z+z*x

for i in range(1,N+1):
  answer_set=set()
  for x in range(int(i**0.5)+1):
    for y in range(1,x+1):
      if func(x,y,1)>i:
        break
      for z in range(1,y+1):
        if func(x,y,z)==i:
          answer_set.add((x,y,z))
          answer_set.add((x,z,y))
          answer_set.add((y,x,z))
          answer_set.add((y,z,x))
          answer_set.add((z,y,x))
          answer_set.add((z,x,y))
        elif func(x,y,z)>i:
          break
          
  print(len(answer_set))

Fixed submission:
N=int(input())

def func(x,y,z):
  return x*x+y*y+z*z+x*y+y*z+z*x

for i in range(1,N+1):
  answer_set=set()
  for x in range(int(i**0.5)+1):
    for y in range(1,x+1):      
      for z in range(1,y+1):
        if func(x,y,z)==i:
          answer_set.add((x,y,z))
          answer_set.add((x,z,y))
          answer_set.add((y,x,z))
          answer_set.add((y,z,x))
          answer_set.add((z,y,x))
          answer_set.add((z,x,y))
        elif func(x,y,z)>i:
          break
          
  print(len(answer_set))
Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7
K=int(input())
S=list(input())
len_S=len(S)

def invmod(a):
  return pow(a,MOD-2,MOD)
fact_dic={0:1}
fact_inv_dic={0:1}
fact_mod=1
for i in range(1,K+len_S+1):
  fact_mod=(fact_mod*i)%MOD
  fact_dic[i]=fact_mod
  fact_inv_dic[i]=invmod(fact_mod)
def comb_mod_table(n,r):
  if 0<=r<=n:
    return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r]
  else:
    return 0
  
answer=0
for i in range(K+1):
  answer+=comb_mod_table(K+len_S-i-1,len_S-1)*pow(25,K-i,MOD)*pow(26,i,MOD)
  answer%=MOD
  
print(answer)

Fixed submission:
MOD=10**9+7
K=int(input())
S=list(input())
len_S=len(S)

def invmod(a):
  return pow(a,MOD-2,MOD)
fact_dic={0:1}
fact_inv_dic={0:1}
fact_mod=1
for i in range(1,K+len_S+1):
  fact_mod=(fact_mod*i)%MOD
  fact_dic[i]=fact_mod
  fact_inv_dic[i]=invmod(fact_mod)
def comb_mod_table(n,r):
  if 0<=r<=n:
    return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r]
  else:
    return 0
  
answer=pow(26,K+len_S,MOD)
pow25=pow(25,K+len_S,MOD)
inv25=invmod(25)
for i in range(len_S):
  answer-=comb_mod_table(K+len_S,i)*pow25
  pow25=pow25*inv25%MOD
  answer%=MOD
  
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7
K=int(input())
S=list(input())
len_S=len(S)

def invmod(a):
  return pow(a,MOD-2,MOD)
fact_dic={0:1}
fact_inv_dic={0:1}
fact_mod=1
for i in range(1,K+len_S+1):
  fact_mod=(fact_mod*i)%MOD
  fact_dic[i]=fact_mod
  fact_inv_dic[i]=invmod(fact_mod)
def comb_mod_table(n,r):
  if 0<=r<=n:
    return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r]
  else:
    return 0
  
answer=pow(26,K+len_S,MOD)
for i in range(len_S):
  answer-=comb_mod_table(K+len_S,i)*pow(25,K+len_S-i,MOD)
  answer%=MOD
  
print(answer)

Fixed submission:
MOD=10**9+7
K=int(input())
S=list(input())
len_S=len(S)

def invmod(a):
  return pow(a,MOD-2,MOD)
fact_dic={0:1}
fact_inv_dic={0:1}
fact_mod=1
for i in range(1,K+len_S+1):
  fact_mod=(fact_mod*i)%MOD
  fact_dic[i]=fact_mod
  fact_inv_dic[i]=invmod(fact_mod)
def comb_mod_table(n,r):
  if 0<=r<=n:
    return fact_dic[n]*fact_inv_dic[r]*fact_inv_dic[n-r]
  else:
    return 0
  
answer=pow(26,K+len_S,MOD)
pow25=pow(25,K+len_S,MOD)
inv25=invmod(25)
for i in range(len_S):
  answer-=comb_mod_table(K+len_S,i)*pow25
  pow25=pow25*inv25%MOD
  answer%=MOD
  
print(answer)
Buggy python submission (verdict: Runtime Error):
As,Bs=input().split()
A=int(As)
B=float(Bs)
B100=int((Bs+0.001)*100)
print(A*B100//100)

Fixed submission:
As,Bs=input().split()
A=int(As)
Bi,Bf=map(int,Bs.split("."))

print(A*Bi+A*Bf//100)
Buggy python submission (verdict: Time Limit Exceeded):
MOD=998244353
N,S=map(int,input().split())
alist=list(map(int,input().split()))

dp=[[[0]*(S+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0]=1

for i in range(1,N+1):
  for j in range(N+1):
    for k in range(S+1):
      dp[i][j][k]=dp[i-1][j][k]
      if k-alist[i-1]>=0:
        dp[i][j][k]+=dp[i-1][j-1][k-alist[i-1]]
      dp[i][j][k]%=MOD
#print(dp[-1])

answer=0
for j in range(N+1):
  answer+=dp[-1][j][S]*pow(2,N-j,MOD)
  answer%=MOD
  
print(answer)

Fixed submission:
MOD=998244353
N,S=map(int,input().split())
alist=list(map(int,input().split()))

dp=[[0]*(S+1) for _ in range(N+1)]
dp[0][0]=1

for i in range(1,N+1):
  for k in range(S+1):
    dp[i][k]=2*dp[i-1][k]
    if k-alist[i-1]>=0:
      dp[i][k]+=dp[i-1][k-alist[i-1]]
    dp[i][k]%=MOD
#print(dp[-1])

print(dp[N][S])
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
plist=list(map(int,input().split()))

mat=[[True]*N for _ in range(N)]
answer=0
for p in plist:
  i=(p-1)//N
  j=(p-1)%N
  #print(i,j)
  
  queue=[(i,j)]
  visited=[[False]*N for _ in range(N)]
  reach=False
  d=0
  while queue:
    new_queue=set()
    
    while queue:
      x,y=queue.pop()
      if x==0 or x==N-1 or y==0 or y==N-1:
        reach=True
        break
        
      visited[x][y]=True
      if not visited[x-1][y]:
        if mat[x-1][y]:
          new_queue.add((x-1,y))
        else:
          queue.append((x-1,y))
      if not visited[x+1][y]:
        if mat[x+1][y]:
          new_queue.add((x+1,y))
        else:
          queue.append((x+1,y))
      if not visited[x][y-1]:
        if mat[x][y-1]:
          new_queue.add((x,y-1))
        else:
          queue.append((x,y-1))
      if not visited[x][y+1]:
        if mat[x][y+1]:
          new_queue.add((x,y+1))
        else:
          queue.append((x,y+1))
          
    if reach:
      break
      
    queue=list(new_queue)
    d+=1
    
  #print(i,j,d)
  answer+=d
  mat[i][j]=False
  
print(answer)

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

sit=[[1]*N for _ in range(N)]
dist=[[min(i,j,N-1-i,N-1-j) for j in range(N)] for i in range(N)]

answer=0
for p in plist:
  i=(p-1)//N
  j=(p-1)%N
  #print(i,j)
  sit[i][j]=0
  answer+=dist[i][j]
  
  queue=[(i,j,dist[i][j])]
  d=0
  while queue:
    x,y,d=queue.pop()
    if x>0 and dist[x-1][y]>d:
      dist[x-1][y]=d
      queue.append((x-1,y,d+sit[x-1][y]))
    if x<N-1 and dist[x+1][y]>d:
      dist[x+1][y]=d
      queue.append((x+1,y,d+sit[x+1][y]))    
    if y>0 and dist[x][y-1]>d:
      dist[x][y-1]=d
      queue.append((x,y-1,d+sit[x][y-1]))
    if y<N-1 and dist[x][y+1]>d:
      dist[x][y+1]=d
      queue.append((x,y+1,d+sit[x][y+1]))
      
print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
alist=list(map(int,input().split()))

a2list=[]
for i in range(N):
  a2list.append((alist[i],i))
a2list.sort(reverse=True)
#print(a2list)

dp=[]
for i in range(N+1):
  dp.append([0]*(N+1))
#print(dp)

for i in range(1,N+1):
  aa,ii=a2list[i-1]
  for j1 in range(i+1):
    j2=i-j1
    if j1==0:
      dp[j1][j2]=dp[j1][j2-1]+aa*abs((N-1)-(j2-1)-ii)
    elif j2==0:
      dp[j1][j2]=dp[j1-1][j2]+aa*abs(ii-(j1-1))
    else:
      dp[j1][j2]=max(dp[j1][j2-1]+aa*abs((N-1)-(j2-1)-ii),dp[j1-1][j2]+aa*abs(ii-(j1-1)))

#print(dp)
answer=0
for i in range(N):
  answer=max(answer,dp[i][N-i])
  #print(dp[i][N-i])
print(answer)

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

a2list=[]
for i in range(N):
  a2list.append((alist[i],i))
a2list.sort(reverse=True)
#print(a2list)

dp=[]
for i in range(N+1):
  dp.append([0]*(N+1))
#print(dp)

for i in range(1,N+1):
  aa,ii=a2list[i-1]
  for j1 in range(i+1):
    j2=i-j1
    if j1==0:
      dp[j1][j2]=dp[j1][j2-1]+aa*abs((N-1)-(j2-1)-ii)
    elif j2==0:
      dp[j1][j2]=dp[j1-1][j2]+aa*abs(ii-(j1-1))
    else:
      dp[j1][j2]=max(dp[j1][j2-1]+aa*abs((N-1)-(j2-1)-ii),dp[j1-1][j2]+aa*abs(ii-(j1-1)))

#print(dp)
answer=0
for i in range(N):
  answer=max(answer,dp[i][N-i])
  #print(dp[i][N-i])
print(answer)