Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n=int(raw_input())
N=range(n)
N1=N+[n]
A=[[0]*(n+1)]+[[0]+map(int,raw_input().split()) for i in N]
B=[[0 for j in N1] for i in N1]
N1=N1[1:]
for i in N1:
  x=A[i]
  if i==0: y=[0]*n
  else: y=B[i-1][:]
  s=0
  for j in N1:
    s+=x[j]
    y[j]+=s
  B[i]=y
m=-1e6
for i1 in N1:
  for j1 in N1:
    for i2 in range(i1,n+1):
      for j2 in range(j1,n+1):
        m=max(m,B[i1-1][j1-1]+B[i2][j2]-B[i1-1][j2]-B[i2][j1-1])
print m

Fixed submission:
def f(m,b):
  s=0
  for k in N[j:]:
    s+=A[k]
    b[k]=max(b[k],0)+s
    m=max(b[k],m)
  return m,b

m=-1e9
n=int(raw_input())
N=range(n)
B=[[0]*(n+1) for _ in range(n+1)]
for _ in N:
  A=map(int,raw_input().split())
  for j in N: m,B[j]=f(m,B[j])
print m

Buggy python submission (verdict: Time Limit Exceeded):
def f(i,j):
  B=[0]*n
  m=[]
  for x in range(i,n):
    a=0
    for y in range(j,n):
      a+=A[y][x]
      B[y]+=a
    m.append(max(B[j:]))
  return max(m)

n=int(raw_input())
N=range(n)
A=[map(int,raw_input().split()) for i in N]
print max([max([f(i,j) for i in N]) for j in N])

Fixed submission:
def f(m,b):
  s=0
  for k in N[j:]:
    s+=A[k]
    b[k]=max(b[k],0)+s
    m=max(b[k],m)
  return m,b

m=-1e9
n=int(raw_input())
N=range(n)
B=[[0]*(n+1) for _ in range(n+1)]
for _ in N:
  A=map(int,raw_input().split())
  for j in N: m,B[j]=f(m,B[j])
print m

Buggy python submission (verdict: Time Limit Exceeded):
def g(j,i):
  a=B[j-1][i-1]
  m=[max([B[y][x]+a-B[y][i-1]-B[j-1][x] for y in range(j,n+1)]) for x in range(i,n+1)]
  return max(m)
n=int(raw_input())
N=range(n)
B=[[0]*(n+1) for _ in range(n+1)]
for j in N:
  A=map(int,raw_input().split())
  s=0
  for i in N:
    s+=A[i]
    B[j+1][i+1]=B[j][i+1]+s
N=N[1:]+[n]
m=[max([g(j,i) for i in N]) for j in N]
print max(m)

Fixed submission:
def f(m,b):
  s=0
  for k in N[j:]:
    s+=A[k]
    b[k]=max(b[k],0)+s
    m=max(b[k],m)
  return m,b

m=-1e9
n=int(raw_input())
N=range(n)
B=[[0]*(n+1) for _ in range(n+1)]
for _ in N:
  A=map(int,raw_input().split())
  for j in N: m,B[j]=f(m,B[j])
print m

Buggy python submission (verdict: Time Limit Exceeded):
def g(j,i):
  a=B[j-1][i-1]
  b1=B[j-1]
  m=[]
  for y in range(j,n+1):
    b=B[y]
    m.append(max([b[x]+a-b[i-1]-b1[x] for x in range(i,n+1)]))
  return max(m)
n=int(raw_input())
N=range(n)
B=[[0]*(n+1) for _ in range(n+1)]
for j in N:
  A=map(int,raw_input().split())
  s=0
  b=B[j]
  for i in N:
    s+=A[i]
    B[j+1][i+1]=b[i+1]+s
N=N[1:]+[n]
m=[max([g(j,i) for i in N]) for j in N]
print max(m)

Fixed submission:
def f(m,b):
  s=0
  for k in N[j:]:
    s+=A[k]
    b[k]=max(b[k],0)+s
    m=max(b[k],m)
  return m,b

m=-1e9
n=int(raw_input())
N=range(n)
B=[[0]*(n+1) for _ in range(n+1)]
for _ in N:
  A=map(int,raw_input().split())
  for j in N: m,B[j]=f(m,B[j])
print m
Buggy python submission (verdict: Time Limit Exceeded):
def f(): return map(int,raw_input().split())
n,q=f()
A=[0]*(n+1)
for _ in range(q):
  a,b=f()
  A[a]+=b
  c=max(A)
  print A.index(c),c

Fixed submission:
def f(): return map(int,raw_input().split())
n,q=f()
a0,v0=0,0
A=[0]*(n+1)
for _ in range(q):
  a,v=f()
  A[a]+=v
  if a==a0 and v<0:
    v0=max(A)
    a0=A.index(v0)
  elif A[a]>v0: a0,v0=a,A[a]
  elif A[a]==v0 and a<a0: a0=a
  print a0,v0

Buggy python submission (verdict: Time Limit Exceeded):
def f(): return map(int,raw_input().split())
n,q=f()
A=[0]*(n+1)
a0,b0=0,0
for _ in range(q):
  a,b=f()
  A[a]+=b
  if A[a]>b0: a0,b0=a,A[a]
  else:
    b0=max(A)
    a0=A.index(b0)
  print a0,b0

Fixed submission:
def f(): return map(int,raw_input().split())
n,q=f()
a0,v0=0,0
A=[0]*(n+1)
for _ in range(q):
  a,v=f()
  A[a]+=v
  if a==a0 and v<0:
    v0=max(A)
    a0=A.index(v0)
  elif A[a]>v0: a0,v0=a,A[a]
  elif A[a]==v0 and a<a0: a0=a
  print a0,v0

Buggy python submission (verdict: Time Limit Exceeded):
def f(): return map(int,raw_input().split())
n,q=f()
A=[[0,-i] for i in range(n+1)]
for _ in range(q):
  a,b=f()
  A[a][0]+=b
  m0,m1=sorted(A,key=lambda A:[A[0],A[1]])[-1]
  print -m1,m0

Fixed submission:
def f(): return map(int,raw_input().split())
n,q=f()
a0,v0=0,0
A=[0]*(n+1)
for _ in range(q):
  a,v=f()
  A[a]+=v
  if a==a0 and v<0:
    v0=max(A)
    a0=A.index(v0)
  elif A[a]>v0: a0,v0=a,A[a]
  elif A[a]==v0 and a<a0: a0=a
  print a0,v0
Buggy python submission (verdict: Runtime Error):
while 1:
  n=input()
  if n==0: break
  D={}
  A=[]
  for _ in range(n):
    a,b,c=map(int, raw_input().split(' '))
    if a in D: D[a]+=b*c
    else:
      D[a]=b*c
      A.append(a)
  B=[i for i in A: if D[i]>=1e6]
  if B=[]: B=["NA"]
  for e in B: print e

Fixed submission:
while 1:
  n=input()
  if n==0: break
  D={}
  A=[]
  for _ in [0]*n:
    a,b,c=map(int, raw_input().split())
    if D.has_key(a): D[a]+=b*c
    else:
      D[a]=b*c
      A.append(a)
  B=[i for i in A if D[i]>=1e6]
  if B==[]: B=["NA"]
  for e in B: print e
Buggy python submission (verdict: Runtime Error):
def f():
  A=[raw_input() for _ in range(h)]
  x,y=0,0
  while 1:
    A[y]=A[y][0:x]+'x'+A[y][x+1:]
    if s==">": x+=1
    elif s=="<": x-=1
    elif s=="v": y+=1
    elif s=="^"3: y-=1
    elif s==".": return " ".join(map(str,[x,y]))
    else: return "LOOP"
  
while 1:
  h,w=map(int,raw_input().split())
  if h==w==0: break
  print f()

Fixed submission:
while 1:
  h,w=map(int,raw_input().split())
  if h==w==0: break
  A=[list(raw_input()) for _ in range(h)]
  x,y=0,0
  while 1:
    s=A[y][x]
    A[y][x]='x'
    if s==">": x+=1
    elif s=="<": x-=1
    elif s=="v": y+=1
    elif s=="^": y-=1
    elif s==".": B=[x,y]; break
    else: B=["LOOP"]; break
  for e in B: print e,
  print
Buggy python submission (verdict: Runtime Error):
d={}
for s in sys.stdin:
  w,p=s.split()
  if not w in d: d[w]=[]
  d[w].append(int(p))
for w in sorted(d.keys()):
  print w,"\n"," ".join(map(str,sorted(d[w])))

Fixed submission:
import sys

d={}
for s in sys.stdin:
  w,p=s.split()
  if w in d: d[w].append(int(p))
  else: d[w]=[int(p)]
  
for w,x in sorted(d.items()):
  print w
  print " ".join(map(str,sorted(x)))

Buggy python submission (verdict: Runtime Error):
d={}
for s in sys.stdin:
  w,p=s.split()
  if not w in d: d[w]=[]
  d[w].append(int(p))
for w in sorted(d.keys()):
  print w,"\n"
  print " ".join(map(str,sorted(d[w])))

Fixed submission:
import sys

d={}
for s in sys.stdin:
  w,p=s.split()
  if w in d: d[w].append(int(p))
  else: d[w]=[int(p)]
  
for w,x in sorted(d.items()):
  print w
  print " ".join(map(str,sorted(x)))

Buggy python submission (verdict: Runtime Error):
d={}
for s in sys.stdin:
  w,p=s.split()
  if w in d: d[w].append(int(p))
  else: d[w]=int(p)
  
for w in sorted(d.keys()):
  print w,"\n"
  print " ".join(map(str,sorted(d[w])))

Fixed submission:
import sys

d={}
for s in sys.stdin:
  w,p=s.split()
  if w in d: d[w].append(int(p))
  else: d[w]=[int(p)]
  
for w,x in sorted(d.items()):
  print w
  print " ".join(map(str,sorted(x)))
Buggy python submission (verdict: Runtime Error):
def f(s):
  while 1:
    b=s.find(")")
    if b<0: return g(s)
    a=s[:b].rfind("(")
    s1=s[a:b+1]
    s=s.replace(s1,f(s1[1:-1]))

def g(s):
  a=""
  f=1
  x=[]
  def F(a,b):
    x.append(a)
    x.append(b)
    return 1,""
  for c in s:
    if c=="=": continue
    elif c in "*/": f,a=F(a,c)
    elif c in "+-":
      if f==1: f,a=0,c
      else: f,a=F(a,c)
    else: f,a=0,a+c
  x.append(a)

  s=x[:]
  x=[]
  f=""
  for e in s:
    if e in "+-": x.append(e)
    elif e in "*/": f=e
    else:
      a=int(e)
      if f=="*": a*=x.pop()
      elif f=="/": a=x.pop()/a
      x.append(a)
      f==""

  s=x[:]
  x=[]
  f=""
  for e in s:
    if e=="+" or e=="-": f=e
    else:
      a=int(e)
      if f=="+": a+=x.pop()
      elif f=="-": a=x.pop()-a
      x.append(a)
      f==""
  return str(x[0])

n=input()
for _ in [0]*n: 
  s=raw_input()
  print f(s)

Fixed submission:
def f(s):
  while 1:
    b=s.find(")")
    if b<0: return g(s)
    a=s[:b].rfind("(")
    s=s.replace(s[a:b+1],f(s[a+1:b]))

def g(s):
  a=""
  f=1
  x=[]
  def F(a,b):
    x.append(a)
    x.append(b)
    return 1,""
  for c in s:
    if c=="=": continue
    elif c in "*/": f,a=F(a,c)
    elif c in "+-":
      if f==1: f,a=0,c
      else: f,a=F(a,c)
    else: f,a=0,a+c
  x.append(a)

  s=x[:]
  x=[]
  f=""
  for e in s:
    if e in "+-": x.append(e)
    elif e in "*/": f=e
    else:
      a=int(e)
      if f=="*": a*=x.pop()
      elif f=="/": a=int(1.0*x.pop()/a)
      x.append(a)
      f=""
  x="".join(map(str,x))
  return str(eval(x))

n=input()
for _ in [0]*n:
  print f(raw_input())
Buggy python submission (verdict: Runtime Error):
X="X"
N="0123456789"
for s in sys.stdin:
  i=s.find("=")
  j=s.find("+")
  k=s[0]==X or s[i+1]==X or s[j+1]==X
  f=0
  for c in N[k:]:
    a=s.replace(X,c)
    if eval(a[:i])==eval(a[i+1:]): f=1; break
  print ["NA",c][f]

Fixed submission:
import sys

for s0 in sys.stdin:
  s=s0[:-1]
  d,c=s.split("=")
  a,b=d.split("+")
  f=any([e!=e[0]=="X" for e in [a,b,c]])

  m="NA"
  s=d+"=="+c
  for e in "0123456789"[f:]:
    if eval(s.replace("X",e)): m=e; break
  print m
Buggy python submission (verdict: Runtime Error):
import sys

def f1(s): return "".join([d0[c] for c in s])

d0={"M":"10010","N":"10011"}
a="ABCD";
for i in range(4): d0[a[i]]=format(i,"02b")
a="EFGHIJKL"
for i in range(8): d0[a[i]]=format(i,"03b")
L0="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
L1=" ',-.?"
L2=" .,-'?"

d={}
a="J,EE,EH,MF,GF,EF,IJ,NG,BB,AB,K,BF,NH,GE,BD,NE,BC,"\
  "AI,NF,NK,AJ,L,NL,CA,AK,AL,NI,NJ,EG,MG,MH,ME".split(",")
for i,e in enumerate(a): d[f1(e)]=(L1+L0)[i]
for s0 in sys.stdin:
  s1="".join([format((L0+L2).index(c),"05b") for c in s0.strip()])
  s=""
  x=""
  for c in s1:
    try: x+=d[s]; s=""
    except: pass
    s+=c
  if s1="": print
  else: print x

Fixed submission:
import sys

def f1(s): return "".join([d0[c] for c in s])

d0={"M":"10010","N":"10011"}
a="ABCD";
for i in range(4): d0[a[i]]=format(i,"02b")
a="EFGHIJKL"
for i in range(8): d0[a[i]]=format(i,"03b")
L0="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
L1=" ',-.?"+L0
L2=L0+" .,-'?"

dc={}
a="J,EE,EH,MF,GF,EF,IJ,NG,BB,AB,K,BF,NH,GE,BD,NE,BC,"\
  "AI,NF,NK,AJ,L,NL,CA,AK,AL,NI,NJ,EG,MG,MH,ME".split(",")
for i,e in enumerate(a): dc[f1(e)]=L1[i]
ec={}
for i in range(32): ec[L2[i]]=format(i,"05b")
for s0 in sys.stdin:
  s1="".join([ec[c] for c in s0.strip("\n")])
  s=""
  x=""
  for c in s1:
    s+=c
    if s in dc: x+=dc[s]; s=""
  print x
Buggy python submission (verdict: Time Limit Exceeded):
import sys,itertools
for s in sys.stdin:
  a=map(int,s.split(" "))
  w=a[0]
  n=len(a)-1
  A=itertools.permutations(a[1:],n)
  s0=1e9
  for e in A:
    s=e[0]+e[-1]
    for i in range(n-1): s+=2*(e[i]*e[i+1])**.5
    if s<s0: s0=s
    if s0<=w: break
  print ["OK","NA"][s0>w]

Fixed submission:
import sys
for s in sys.stdin:
  a=map(int,s.split(" "))
  w=a[0]
  a=sorted(a[1:])
  A=[]
  while a:
    A+=[a.pop(0)]
    if a==[]: break
    A=[a.pop()]+A
    if a==[]: break
    A+=[a.pop()]
    if a==[]: break
    A=[a.pop(0)]+A
  a=A[0]+A[-1]
  for i in range(len(A)-1): a+=2*(A[i]*A[i+1])**.5
  print ["OK","NA"][a>w]
Buggy python submission (verdict: Runtime Error):
M=[0,1,-1,0,0,1,1,2,3,3,4,4]
def L(y,m):
  if m<=2: y-=1
  return y/4-y/100+y/400+M[m]

while 1:
  x=map(int,raw_input().split(" "))
  if any([e<0 for e in x]): break
  a,b,c,d,e,f=x
  ans=(d-a)*365+(e-b)*30+f-c+L(d,e)-L(a,b)  
  print ans

Fixed submission:
import datetime as D
while 1:
  a,b,c,d,e,f=map(int,raw_input().split())
  try: print (D.date(d,e,f)-D.date(a,b,c)).days
  except: break

Buggy python submission (verdict: Runtime Error):
M=[0,1,-1,0,0,1,1,2,3,3,4,4]
def L(y,m):
  if m<=2: y-=1
  return y/4-y/100+y/400+M[m]

while 1:
  a,b,c,d,e,f=x=map(int,raw_input().split(" "))
  if any([i<0 for i in x]): break
  ans=(d-a)*365+(e-b)*30+f-c+L(d,e)-L(a,b)
  print ans

Fixed submission:
import datetime as D
while 1:
  a,b,c,d,e,f=map(int,raw_input().split())
  try: print (D.date(d,e,f)-D.date(a,b,c)).days
  except: break
Buggy python submission (verdict: Runtime Error):
def f(A): return A.count(a)>1
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    p0=y/3*3
    for x in N:
      a=A[x]
      if any(map(f,[M[y],M1[x],M2[p0+x/3]])): F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,A))])
  if n>1: print
  n-=1

Fixed submission:
def f(A): return A.count(a)>1
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    p0=y/3*3
    for x in N:
      a=M[y][x]
      if any(map(f,[M[y],M1[x],M2[p0+x/3]])): F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,M[y]))])
  if n>1: print
  n-=1

Buggy python submission (verdict: Runtime Error):
def f(A): return A.count(a)>1
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    p0=y/3*3
    for x in N:
      a=M[y][x]
      if any(map(f,[M[y],M1[x],M2[p0+x/3]])): F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,A))])
  if n>1: print
  n-=1

Fixed submission:
def f(A): return A.count(a)>1
R=[0,3,6]
N=range(9)
Z=[0]*9

n=input()
while n:
  F=[[" " for _ in Z] for _ in Z]
  M=[map(int,raw_input().split()) for _ in Z]
  M1=[[M[y][x] for y in N] for x in N]
  M2=[M[y][x:x+3]+M[y+1][x:x+3]+M[y+2][x:x+3] for y in R for x in R]
  for y in N:
    p0=y/3*3
    for x in N:
      a=M[y][x]
      if any(map(f,[M[y],M1[x],M2[p0+x/3]])): F[y][x]="*"
    print "".join([a+b for a,b in zip(F[y],map(str,M[y]))])
  if n>1: print
  n-=1
Buggy python submission (verdict: Runtime Error):
import sys
d={}
B1=['* =',' *=']
B2=[' ****','* ***','** **','*** *','**** ']
f=0
for i in range(9): d[str(i)]=B1[i/5]+B2[i%5]
for s in sys.stdin:
  A=[d[e] for e in ('0'*5+s[:-1])[-5:]]
  if f: print
  for y in range(8): print "".join([A[x][y] for x in range(len(A))])
  f=1

Fixed submission:
import sys
d={}
B1=['* =',' *=']
B2=[' ****','* ***','** **','*** *','**** ']
f=0
for i in range(10): d[str(i)]=B1[i/5]+B2[i%5]
for s in sys.stdin:
  A=[d[e] for e in ('0'*5+s[:-1])[-5:]]
  if f: print
  for y in range(8): print "".join([A[x][y] for x in range(len(A))])
  f=1
Buggy python submission (verdict: Runtime Error):
import math as M
def R(A): return (A[0]**2+A[1]**2)**.5
def I(i): return [map(int,raw_input().split()) for _ in [0]*i]
def f(e1):
  tx,ty,sx,sy=e1
  x=[]
  st=[tx-sx,ty-sy]; rst=R(st)
  for e2 in WP:
    wx,wy,r=e2
    wt=[tx-wx,ty-wy]; rwt=R(wt)
    sw=[wx-sx,wy-sy]; rsw=R(sw)
    F=[rwt<r,rsw<r]
    if F==[1,0] or F==[0,1]: return 0
    elif F==[0,0]:
      a=M.pi/2-M.acos(r/rsw)
      b=M.acos(round((sw[0]*st[0]+sw[1]*st[1])/rsw/rst,4))
      if a+1e-6>b and rst**2+1e-6>rsw**2-r**2: return 0
  return 1

while 1:
  n=input()
  if n==0: break
  WP=I(n)
  P=I(input())
  for e in P: print ["Safe","Danger"][f(e)]

Fixed submission:
n=input()
for _ in [0]*n:
  s=raw_input()
  A=s[0]
  for i in range(0,len(s),3):
    if s[i:i+3]==(A[-1]+"->"): A+=s[i+3]
    elif s[i:i+3]==(A[0]+"<-"): A=s[i+3]+A
  print "".join(A)
Buggy python submission (verdict: Runtime Error):
for _ in [0]*input():
  n = input()
  A = [list(' '*n) for _ in [0]*n]
  for i in range((n+3)/4):
    i2 = i*2
    if i2>0: A[n-i2][i2]='@'
    for j in range(i2, n-i2):
      A[j][i2]='@' #左下→左上
      A[i2][j]='@' #左上→右上
      if j<n-i2-1: A[j][n-i2-1]='@' #右上→右下
      if j>i2+1: A[n-i2-1][j]='@' #右下→左下下
  for e in A: print "".join(e)
  print

Fixed submission:
s="#"
for c in range(input()):
  n = input()
  A = [list(' '*n) for _ in [0]*n]
  for i in range((n+3)/4):
    i2 = i*2
    if i2>0: A[n-i2][i2]=s
    for j in range(i2, n-i2):
      A[j][i2]=s
      A[i2][j]=s
      if j<n-i2-1: A[j][n-i2-1]=s
      if j>i2+1: A[n-i2-1][j]=s
  if c: print
  for e in A: print "".join(e)
Buggy python submission (verdict: Time Limit Exceeded):
def solve(Card):
  if len(Card)==1: return 0
  X = []
  for i in range(len(Card)-1):
    A = Card[:]
    tmp = A[i]+A[i+1]
    cost = reduce(lambda a,b: a*b, tmp)
    tmp[1:3] = []
    A[i:i+2] = [tmp]
    X.append(cost+solve(A))
  return min(X)
    
n = int(raw_input())
Card = [map(int,raw_input().split()) for _ in [0]*n]
print solve(Card)

Fixed submission:
n = int(raw_input())
C = [[] for _ in [0]*n]
W = {}
for i in range(n):
  C[i] = map(int, raw_input().split())
  W[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    W[(j, a)] = min([C[j][0] * C[k][1] * C[k+1][0] * C[a][1] + W[(j, k)] + W[(k+1, a)] for k in range(j, a)])
print W[0, n-1]

Buggy python submission (verdict: Time Limit Exceeded):
def solve(Card):
  if len(Card)==1: return 0
  X = []
  for i in range(len(Card)-1):
    A = Card[:]
    tmp = tuple(A[i]+A[i+1])
    if tmp in d: cost = d[tmp]
    else:
      cost = reduce(lambda a,b: a*b, tmp)
      d[tmp] = cost
    tmp = list(tmp)
    tmp[1:3] = []
    A[i:i+2] = [tmp]
    X.append(cost+solve(A))
  return min(X)
    
d={}
n = int(raw_input())
Card = [map(int,raw_input().split()) for _ in [0]*n]
print solve(Card)

Fixed submission:
n = int(raw_input())
C = [[] for _ in [0]*n]
W = {}
for i in range(n):
  C[i] = map(int, raw_input().split())
  W[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    W[(j, a)] = min([C[j][0] * C[k][1] * C[k+1][0] * C[a][1] + W[(j, k)] + W[(k+1, a)] for k in range(j, a)])
print W[0, n-1]

Buggy python submission (verdict: Time Limit Exceeded):
def solve(Card):
  n = len(Card)
  if n==2: return 0
  x=[]
  for i in range(0,n-2,2):
    tmp = Card[:]
    T = tuple(Card[i:i+4])
    if T not in d: d[T] = reduce(lambda a,b: a*b, T)
    tmp[i+1:i+3]=[]
    x.append(d[T]+solve(tmp))
  return min(x)
    
d={}
n = int(raw_input())
Card=[]
for _ in [0]*n: Card += map(int,raw_input().split())
print solve(Card)

Fixed submission:
n = int(raw_input())
C = [[] for _ in [0]*n]
W = {}
for i in range(n):
  C[i] = map(int, raw_input().split())
  W[(i, i)] = 0
for i in range(1, n):
  for j in range(0, n-i):
    a = j+i
    W[(j, a)] = min([C[j][0] * C[k][1] * C[k+1][0] * C[a][1] + W[(j, k)] + W[(k+1, a)] for k in range(j, a)])
print W[0, n-1]
Buggy python submission (verdict: Runtime Error):
R = range(100)
M = [[2,5,2,2,2][i%5] for i in R]
Out = [-1] *100
P = [-1]*100
S = [-1]*17
Q = []
W = [0]*100

def stand(I, s):
  n = M[I]
  P[I] = -1
  S[s:s+n] = [-1] * n
  Out[I] = -1
  return 1

def sit(I, s):
  n = M[I]
  P[I] = s
  S[s:s+n] = [I] * n
  Out.[I] = t+ 17*(I%2)+3*(I%3)+19
  W[I] = t - I*5
  return

def check(I):
  n = M[I]
  for s in range(17-n):
    if S[s:s+n] == [-1]*n: return s
  return -1

t = 0
c = 0
while t<100:
  while t in Out:
    I = Out.index(t)
    c += stand(I, P[I])

  if t<500 and t%5==0: Q.append(t/5)
 
  while len(Q)>0:
    s = check(Q[0])
    if s==-1: break
    sit(Q.pop(0), s)
  t += 1

while 1:
  try: print W[int(raw_input())]
  except: break

Fixed submission:
S = [0]*17
Q = []
W = [0]*100
 
def check(I):
  n = [2,5][I%5==1]
  for s in range(17-n+1):
    if S[s:s+n] == [0]*n: return s
  return -1

t = 0
c = 0
while c<100:
  for i in range(S.count(t)): S[S.index(t)]=0
  if t<500 and t%5==0: Q.append(t/5)
  
  while len(Q)>0:
    s = check(Q[0])
    if s==-1: break
    I = Q.pop(0)
    n = [2,5][I%5==1]
    S[s:s+n] = [t+ 17*(I%2) + 3*(I%3) + 19] * n
    W[I] = t - I*5
    c += 1
  t += 1
 
while 1:
  try: print W[int(raw_input())]
  except: break
Buggy python submission (verdict: Runtime Error):
def In(): return map(int,raw_input().split())
def D(a,b): return ((a[0]-b[0])**2+(a[1]-b[1])**2)**.5
def side(a, b, c):
  f = (c[1]-a[1])*(b[0]-a[0])-(b[1]-a[1])*(c[0]-a[0])
  if f>0: return 1
  elif f<0: return -1
  else return 0
def isIn(x,p0,p1,p2):
  f1 = side(p0,p1,x)
  f2 = side(p1,p2,x)
  f3 = side(p2,p0,x)
  return (f1>=0 and f2>=0 and f3>=0) or (f1<=0 and f2<=0 and f3<=0)
def S(a,b,c): return abs(a[0]*(b[1]-c[1])+b[0]*(c[1]-a[1])+c[0]*(a[1]-b[1]))/2.0
def H(P1,P2,P3): return 2*S(P1,P2,P3)/D(P2,P3)
def check(P4,r,P1,P2,P3):
  if all([D(P4,P1)<=r, D(P4,P2)<=r, D(P4,P3)<=r]): return 2
  h1 = H(P4,P1,P2)
  h2 = H(P4,P2,P3)
  h3 = H(P4,P3,P1)
  if isIn(P4,P1,P2,P3):
    if all([h1>=r, h2>=r, h3>=r]): return 1
  else:
    if all([h1>r, h2>r, h3>r]): return 0
  return 3
  
while 1:
  P1 = In()
  if P1[0]==P1[1]==0: break
  P2 = In()
  P3 = In()
  P4 = In()
  r = int(raw_input())
  print 'dabc'[check(P4,r,P1,P2,P3)]

Fixed submission:
def In(): return map(int, raw_input().split())
def D(a,b): return ((a[0]-b[0])**2+(a[1]-b[1])**2)**.5
def f(P,O): return P[0]-O[0], P[1]-O[1]
def dot(a,b): return a[0]*b[0]+a[1]*b[1]
def cross(a,b): return a[0]*b[1]-a[1]*b[0]

def ab2c(a,b,c):
  if dot(f(c,a),f(b,a))<0: return D(a,c)
  if dot(f(c,b),f(a,b))<0: return D(b,c)
  return abs(cross(f(b,a),f(c,a)))/D(b,a)

def isIn(P,O,P1,P2):
  a11,a21 = f(P1,O)
  a12,a22 = f(P2,O)
  x0,y0 = f(P,O)
  det = 1.0*a11*a22-a12*a21
  x = (a22*x0-a12*y0)/det
  y = (-a21*x0+a11*y0)/det
  return 0<=x<=1 and 0<=y<=1 and 0<=x+y<=1

def check(P4,r,P1,P2,P3):
  if max([D(P4,P1),D(P4,P2),D(P4,P3)])<=r: return 2
  h1 = ab2c(P1,P2,P4)
  h2 = ab2c(P2,P3,P4)
  h3 = ab2c(P3,P1,P4)
  if isIn(P4,P1,P2,P3):
    if min([h1,h2,h3])>=r: return 1
  elif min([h1,h2,h3])>r: return 0
  return 3

while 1:
  P1 = In()
  if P1[0]== P1[1]==0: break
  P2 = In()
  P3 = In()
  P4 = In()
  r = int(raw_input())
  print 'dabc'[check(P4,r,P1,P2,P3)]
Buggy python submission (verdict: Runtime Error):
d = {}
while 1:
  n = int(raw_input())
  if n==0: break
  c = 0
  while n != 1
    if n % 2 ==0: n/=2
    else: n = n*3+1
    c += 1
  print c

Fixed submission:
while 1:
  n = input()
  if n==0: break
  c=0
  while n>1:
    n = n*3+1 if n%2 else n/2
    c += 1
  print c