Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def binarySearch(x, S):
    mid = len(S) / 2
    if mid < 2:
        return x in S
    if S[mid] == x:
        return True
    elif S[mid] > x:
        left = S[:mid]
        return binarySearch(x, left)
    else:
        right = S[mid + 1:]
        return binarySearch(x, right)

n = int(raw_input())
S = map(int, raw_input().split())
q = int(raw_input())
T = map(int, raw_input().split())
count = 0
for x in T:
    if binarySearch(x, S):
        count += 1
print count

Fixed submission:
def binarySearch(x, S):
    left = 0
    right = len(S)
    while left < right:
        mid = (left + right) / 2
        if x == S[mid]:
            return mid
        elif x < S[mid]:
            right = mid
        else:
            left = mid + 1
    return -1

n = int(raw_input())
S = map(int, raw_input().split())
q = int(raw_input())
T = map(int, raw_input().split())
count = 0
for x in T:
    if binarySearch(x, S) >= 0:
        count += 1
print count

Buggy python submission (verdict: Time Limit Exceeded):
def binarySearch(x, S):
    mid = len(S) / 2
    if mid < 2:
        return x in S
    if S[mid] == x:
        return True
    elif S[mid] > x:
        return binarySearch(x, S[:mid])
    else:
        return binarySearch(x, S[mid+1:])

n = int(raw_input())
S = map(int, raw_input().split())
q = int(raw_input())
T = map(int, raw_input().split())
count = 0
for x in T:
    if binarySearch(x, S):
        count += 1
print count

Fixed submission:
def binarySearch(x, S):
    left = 0
    right = len(S)
    while left < right:
        mid = (left + right) / 2
        if x == S[mid]:
            return mid
        elif x < S[mid]:
            right = mid
        else:
            left = mid + 1
    return -1

n = int(raw_input())
S = map(int, raw_input().split())
q = int(raw_input())
T = map(int, raw_input().split())
count = 0
for x in T:
    if binarySearch(x, S) >= 0:
        count += 1
print count
Buggy python submission (verdict: Time Limit Exceeded):
def need(A, p):
    if max(A) > p:
        return 100001
    count = 0
    track = 0
    for x in A:
        if count + x <= p:
            count += x
        else:
            track += 1
            count = x
    if count > 0:
        return track + 1
    else:
        return track

n, k = map(int, raw_input().split())
W = [int(raw_input()) for i in xrange(n)]

left = 1
right = sum(W) + 1
while left < right:
    mid = (left + right) / 2
    track = need(W, mid)
    if k == track:
        while k == need(W, mid-1):
            mid -= 1
        print mid
        quit()
    elif k > track:
        right = mid
    else:
        left = mid + 1
if left == right:
    print left

Fixed submission:
def maxStack(k, p, W):
    nw = 0
    count = 0
    t = 1
    for x in W:
        if x > p:
            return count
        if nw + x <= p:
            nw += x
            count += 1
        else:
            if t == k:
                return count
            else:
                t += 1
                nw = x
                count += 1
    return count


n, k = map(int, raw_input().split())
W = [int(raw_input()) for i in xrange(n)]

left = 1
right = sum(W)
while left < right:
    mid = (left + right) / 2
    v = maxStack(k, mid, W)
    if v >= n:
        right = mid
    else:
        left = mid + 1
print left

Buggy python submission (verdict: Time Limit Exceeded):
def maxStack(k, p, W):
    nw = 0
    count = 0
    t = 1
    for x in W:
        if x > p:
            return count
        if nw + x <= p:
            nw += x
            count += 1
        else:
            if t == k:
                return count
            else:
                t += 1
                nw = x
                count += 1
    return count


n, k = map(int, raw_input().split())
W = [int(raw_input()) for i in xrange(n)]

left = 1
right = sum(W)
while left < right:
    mid = (left + right) / 2
    v = maxStack(k, mid, W)
    if v == n:
        while n == maxStack(k, mid-1, W):
            mid -= 1
        print mid
        quit()
    elif v > n:
        right = mid
    else:
        left = mid + 1
print left

Fixed submission:
def maxStack(k, p, W):
    nw = 0
    count = 0
    t = 1
    for x in W:
        if x > p:
            return count
        if nw + x <= p:
            nw += x
            count += 1
        else:
            if t == k:
                return count
            else:
                t += 1
                nw = x
                count += 1
    return count


n, k = map(int, raw_input().split())
W = [int(raw_input()) for i in xrange(n)]

left = 1
right = sum(W)
while left < right:
    mid = (left + right) / 2
    v = maxStack(k, mid, W)
    if v >= n:
        right = mid
    else:
        left = mid + 1
print left
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3

class UnionFindTree:
    def __init__(self, x):
        self.parent = self
        self.id = x

    def find(self):
        if self.id == self.parent.id:
            return self
        else:
            return self.parent.find()


# main
n, q = map(int, input().split())
nodes = [UnionFindTree(i) for i in range(n)]
for i in range(q):
    com, x, y = map(int, input().split())
    x_root = nodes[x].find()
    y_root = nodes[y].find()
    if com == 0:
        # union
        y_root.parent = x_root
    else:
        # same
        same = 1 if x_root.id == y_root.id else 0
        print(same)
            

Fixed submission:
#!/usr/bin/python3

class UnionFindTree:
    def __init__(self, x):
        self.parent = self
        self.id = x
        self.rank = 0

    def find(self):
        if self.id == self.parent.id:
            return self
        else:
            return self.parent.find()


# main
n, q = map(int, input().split())
nodes = [UnionFindTree(i) for i in range(n)]
for i in range(q):
    com, x, y = map(int, input().split())
    x_root = nodes[x].find()
    y_root = nodes[y].find()
    if com == 0:
        # union
        if x_root.rank > y_root.rank:
            y_root.parent = x_root
        elif y_root.rank > x_root.rank:
            x_root.parent = y_root
        elif x_root.id != y_root.id:
            y_root.parent = x_root
            x_root.rank += 1
    else:
        # same
        same = 1 if x_root.id == y_root.id else 0
        print(same)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3
inf = float('inf')

def inf_matrix(n):
    return [[inf for i in range(n)] for j in range(n)]

def sssp(r, w):
    V = len(w)
    result = inf_matrix(V)
    rest = list(range(V))
    rest.remove(r)
    var = [r]
    result[r][r] = 0

    while len(rest) > 0:
        costs = []
        for v in var:
            for res in rest:
                costs.append((result[r][v] + w[v][res], res))
        min_w, min_r = min(costs)
        if min_w == inf:
            break
        rest.remove(min_r)
        var.append(min_r)
        result[r][min_r] = min_w

    return result


def main():
    V, E, r = map(int, input().split())
    w = inf_matrix(V)
    for e in range(E):
        s, t, d = map(int, input().split())
        w[s][t] = d

    result = sssp(r, w)
    for i in range(V):
        if result[r][i] == inf:
            print('INF')
        else:
            print(result[r][i])

main()

Fixed submission:
#!/usr/bin/python3
from collections import defaultdict
from heapq import heappush, heappop

inf = float('inf')

def inf_matrix(n):
    return [[inf for i in range(n)] for j in range(n)]

def dijkstra(r, w, V):
    result = [inf] * V
    q = []
    heappush(q, (0, r))
    result[r] = 0
    while len(q) > 0:
        min_w, min_next = heappop(q)
        for weight, nxt in w[min_next]:
            if result[nxt] > min_w + weight:
                result[nxt] = min_w + weight
                heappush(q, (min_w + weight, nxt))

    return result


def main():
    V, E, r = map(int, input().split())
    w = defaultdict(list)
    for e in range(E):
        s, t, d = map(int, input().split())
        w[s].append((d, t))

    result = dijkstra(r, w, V)
    for i in range(V):
        if result[i] == inf:
            print('INF')
        else:
            print(result[i])

main()
Buggy python submission (verdict: Runtime Error):
n = int(raw_input())
arr = map(int, raw_input().split(' '))

p = 1
print ' '.join(map(str, arr))
while True:
    for j in range(0,p):
        if arr[p] < arr[j]:
            arr[p],arr[j] = arr[j],arr[p]
    p += 1
    print ' '.join(map(str, arr))
    if p >= len(arr):
        break

Fixed submission:
def sort(a):
    n = len(a)
    for i in range(1,n):
        for j in range(i):
            p = i-j
            if a[p] < a[p-1]:
                a[p], a[p-1] = a[p-1], a[p]
        print ' '.join(map(str, a))

n = int(raw_input())
a = map(int, raw_input().split(' '))
print ' '.join(map(str, a))
sort(a)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(raw_input())
a = []
for i in range(n):
    a.append(int(raw_input()))

primes = [True]*n
for d in range(2,10001):
    for i in range(n):
        if d!=a[i] and a[i]%d==0:
            primes[i] = False

print sum(primes)

Fixed submission:
def prime(num):
    if num == 0:
        return False
    elif num == 1:
        return False
    elif num == 2:
        return True
    i = 2
    while True:
        if i**2 > num:
            break
        if num%i==0:
            return False
        i += 1
    return True
n = int(raw_input())

nums = []
for _ in range(n):
    a = int(raw_input())
    nums.append(a)

ret = 0
for num in nums:
    if prime(num):
        ret += 1
print ret

Buggy python submission (verdict: Time Limit Exceeded):
n = int(raw_input())
a = []
for i in range(n):
    a.append(int(raw_input()))

nprimes = 0
a = sorted(a)
while True:
    for d in range(2,10001):
        if d==a[0]:
            nprimes+=1
            del a[0]
            break
        elif a[0]%d==0:
            del a[0]
            break
    if len(a) <= 0:
        break

print nprimes

Fixed submission:
def prime(num):
    if num == 0:
        return False
    elif num == 1:
        return False
    elif num == 2:
        return True
    i = 2
    while True:
        if i**2 > num:
            break
        if num%i==0:
            return False
        i += 1
    return True
n = int(raw_input())

nums = []
for _ in range(n):
    a = int(raw_input())
    nums.append(a)

ret = 0
for num in nums:
    if prime(num):
        ret += 1
print ret
Buggy python submission (verdict: Runtime Error):
n = int(raw_input())
S = map(int, raw_input().split(' '))
q = int(raw_input())
T = map(int, raw_input().split(' '))

def search(S,k):
    for s in S:
        if s == k:
            return True
    return False

c = 0
for t in T:
    if search(S,t): c += 1

print c

Fixed submission:
def search(S,t):
    for s in S:
        if t == s:
            return True
    return False

n = int(raw_input())
S = map(int,raw_input().split(' '))
q = int(raw_input())
T = map(int,raw_input().split(' '))


ret = 0
for t in T:
    if search(S,t):
        ret += 1
print ret
Buggy python submission (verdict: Time Limit Exceeded):
n = int(raw_input())
S = map(int, raw_input().strip().split(' '))
q = int(raw_input())
T = map(int, raw_input().strip().split(' '))

def binary_search(S,k):
    if len(S) == 1 and S[0] != k: return False
    p = len(S)/2
    if S[p] == k: return True
    else:
        return binary_search(S[:p],k) | binary_search(S[p:],k)

c = 0
for t in T:
    if binary_search(S,t): c += 1
print c

Fixed submission:
def search(a, x):
    n = len(a)
    hi = n-1
    lo = 0
    while hi >= lo:
        med = (hi+lo)/2
        if a[med] == x:
            return True
        elif a[med] > x:
            hi = med-1
        else:
            lo = med+1
    return False

n = int(raw_input())
S = map(int, raw_input().split(' '))
q = int(raw_input())
T = map(int, raw_input().split(' '))

found = 0
for x in T:
    if search(S, x):
        found += 1

print found

Buggy python submission (verdict: Time Limit Exceeded):
n = int(raw_input())
S = map(int, raw_input().strip().split(' '))
q = int(raw_input())
T = map(int, raw_input().strip().split(' '))

def binary_search(S,k):
    if len(S) == 1 and S[0] != k: return False
    p = len(S)/2
    if S[p] == k: return True
    elif S[p] > k:
        return binary_search(S[:p],k)
    else:
        return binary_search(S[p:],k)

c = 0
for t in T:
    if binary_search(S,t): c += 1
print c

Fixed submission:
def search(a, x):
    n = len(a)
    hi = n-1
    lo = 0
    while hi >= lo:
        med = (hi+lo)/2
        if a[med] == x:
            return True
        elif a[med] > x:
            hi = med-1
        else:
            lo = med+1
    return False

n = int(raw_input())
S = map(int, raw_input().split(' '))
q = int(raw_input())
T = map(int, raw_input().split(' '))

found = 0
for x in T:
    if search(S, x):
        found += 1

print found

Buggy python submission (verdict: Time Limit Exceeded):
n = int(raw_input())
S = map(int, raw_input().strip().split(' '))
q = int(raw_input())
T = map(int, raw_input().strip().split(' '))

def binary_search(S,k):
    if len(S) == 0: return False
    if len(S) == 1 and S[0] != k: return False
    p = len(S)/2
    if S[p] == k: return True
    elif S[p] > k:
        return binary_search(S[:p],k)
    else:
        return binary_search(S[p:],k)

c = 0
for t in T:
    if binary_search(S,t): c += 1
print c

Fixed submission:
def search(a, x):
    n = len(a)
    hi = n-1
    lo = 0
    while hi >= lo:
        med = (hi+lo)/2
        if a[med] == x:
            return True
        elif a[med] > x:
            hi = med-1
        else:
            lo = med+1
    return False

n = int(raw_input())
S = map(int, raw_input().split(' '))
q = int(raw_input())
T = map(int, raw_input().split(' '))

found = 0
for x in T:
    if search(S, x):
        found += 1

print found
Buggy python submission (verdict: Runtime Error):
n = int(raw_input().strip())
commands = []
for i in range(n):
    commands.append(raw_input().strip())


d = [False]*16777220

def hash(s):
    ret = 0
    i = 0
    for dg in s:
        if dg == 'A':
            ret += 1 * 10**i
        elif dg == 'C':
            ret += 2 * 10**i
        elif dg == 'G':
            ret += 3 * 10**i
        elif dg == 'T':
            ret += 4 * 10**i
        i += 1
    return ret

def insert(d,s):
    h = hash(s)
    d[h] = True

def find(d,s):
    h = hash(s)
    if d[h]: print 'yes'
    else: print 'no'

for c in commands:
    if c[0] == 'i':
        insert(d,c[7:])
    else:
        find(d,c[5:])

Fixed submission:
n = int(raw_input().strip())
commands = []
for i in range(n):
    commands.append(raw_input().strip())


d = [False]*67108870

def hash(s):
    ret = 0
    i = 0
    for dg in s:
        if dg == 'A':
            ret += 0 * 4**i
        elif dg == 'C':
            ret += 1 * 4**i
        elif dg == 'G':
            ret += 2 * 4**i
        elif dg == 'T':
            ret += 3 * 4**i
        i += 1
    return ret

def insert(d,s):
    h = hash(s)
    d[h] = True

def find(d,s):
    h = hash(s)
    if d[h]: print 'yes'
    else: print 'no'

for c in commands:
    c += 'C'
    if c[0] == 'i':
        insert(d,c[7:])
    else:
        find(d,c[5:])
Buggy python submission (verdict: Runtime Error):
import math

n = int(raw_input())

th = math.pi * 60.0 / 180.0

def split(x,y):
    s = ((2*x[0]+y[0])/3, (2*x[1]+y[1])/3)
    t = ((x[0]+2*y[0])/3, (x[1]+2*y[1])/3)
    u = ((t[0]-s[0])*math.cos(th) - (t[1]-s[1])*math.sin(th) + s[0], (t[0]-s[0])*math.sin(th) + (t[1]-s[1])*math.cos(th) + s[1])
    return (s,u,t)


s = (0.,0.)
t = (100.,0.)
points = []
points.append(s)
points.append(t)
for i in range(n):
    new_points = []
    j = 0
    while True:
        s,u,t = split(points[j],points[j+1])
        new_points.extend((points[j],s,u,t,points[j+1]))
        j += 1
        if j+1 >= len(points): break
    points = new_points
for p in new_points:
    print p[0],p[1]

Fixed submission:
import math

n = int(raw_input())

th = math.pi * 60.0 / 180.0

def split(x,y):
    s = ((2*x[0]+y[0])/3, (2*x[1]+y[1])/3)
    t = ((x[0]+2*y[0])/3, (x[1]+2*y[1])/3)
    u = ((t[0]-s[0])*math.cos(th) - (t[1]-s[1])*math.sin(th) + s[0], (t[0]-s[0])*math.sin(th) + (t[1]-s[1])*math.cos(th) + s[1])
    return (s,u,t)


s = (0.,0.)
t = (100.,0.)
points = []
points.append(s)
points.append(t)
for i in range(n):
    new_points = []
    j = 0
    while True:
        s,u,t = split(points[j],points[j+1])
        new_points.extend((points[j],s,u,t))
        j += 1
        if j+1 >= len(points): break
    new_points.append(points[j])
    points = new_points
for p in points:
    print p[0],p[1]
Buggy python submission (verdict: Runtime Error):
def postorder(n,preorder,inorder):
    if len(preorder) > 1:
        left_inorder = []
        right_inorder = []
        flag = False
        for i in range(len(inorder)):
            if flag == False:
                if n == inorder[i]:
                    flag = True
                else:
                    left_inorder.append(inorder[i])
            else:
                right_inorder.append(inorder[i])
        left_preorder = []
        right_preorder = []
        flag = False
        for i in range(1,len(preorder)):
            if flag == False:
                left_preorder.append(preorder[i])
                if len(left_preorder) == len(left_inorder):
                    flag = True
            else:
                right_preorder.append(preorder[i])
        postorder(left_preorder[0],left_preorder,left_inorder)
        postorder(right_preorder[0],right_preorder,right_inorder)
    if len(preorder) >= 0:
        print n,

n = int(raw_input())
preorder = map(int,raw_input().split(' '))
inorder = map(int,raw_input().split(' '))

postorder(preorder[0],preorder,inorder)

Fixed submission:
def postorder(preorder,inorder):
    if len(preorder) > 1:
        n = preorder[0]
        left_inorder = []
        right_inorder = []
        flag = False
        for i in range(len(inorder)):
            if flag == False:
                if n == inorder[i]:
                    flag = True
                else:
                    left_inorder.append(inorder[i])
            else:
                right_inorder.append(inorder[i])
        left_preorder = []
        right_preorder = []
        flag = False
        for i in range(1,len(preorder)):
            if flag == False:
                if len(left_preorder) == len(left_inorder):
                    flag = True
                else:
                    left_preorder.append(preorder[i])
            if flag == True:
                right_preorder.append(preorder[i])
        #print left_preorder,left_inorder
        #print right_preorder,right_inorder
        #print
        postorder(left_preorder,left_inorder)
        postorder(right_preorder,right_inorder)
    if len(preorder) > 0:
        print preorder[0],

n = int(raw_input())
preorder = map(int,raw_input().split(' '))
inorder = map(int,raw_input().split(' '))

postorder(preorder,inorder)
Buggy python submission (verdict: Runtime Error):
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
print fib(n)

Fixed submission:
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [0 for row in range(n+1)]
print fib(n)

Buggy python submission (verdict: Runtime Error):
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = []
print fib(n)

Fixed submission:
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [0 for row in range(n+1)]
print fib(n)

Buggy python submission (verdict: Runtime Error):
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = []*44
print fib(n)

Fixed submission:
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [0 for row in range(n+1)]
print fib(n)

Buggy python submission (verdict: Runtime Error):
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [44]
print fib(n)

Fixed submission:
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [0 for row in range(n+1)]
print fib(n)

Buggy python submission (verdict: Runtime Error):
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [45]
print fib(n)

Fixed submission:
def fib(n):
  if F[n] != 0:
    return F[n]
  elif n==0 or n==1:
    F[n] = 1
  else:
    F[n] = fib(n-1) + fib(n-2)

  return F[n]



n = input()
F = [0 for row in range(n+1)]
print fib(n)
Buggy python submission (verdict: Runtime Error):
n = input()
r = [0 for row in range(100)]
c = [0 for row in range(100)]
m = [[0 for col in range(n)] for row in range(n)]
for i in range(n-1):
  p[i], p[i+1] = map(int, raw_input().split())

for l in range(2, n):
  for i in range(n-1):
    j = i + l - 1
    m[i][j] = 100000000000
    for k in range(j-1):
      if m[i][j]>m[i][k]+m[i][k]+m[k+1][j]+p[i-1]*p[k]*[j]:
        m[i][j] = m[i][k]+m[i][k]+m[k+1][j]+p[i-1]*p[k]*[j]

print m[1][n]

Fixed submission:
n = input()
p = [0 for row in range(101)]
m = [[0 for col in range(n+1)] for row in range(n+1)]
for i in range(1, n+1):
  p[i-1], p[i] = map(int, raw_input().split())

for l in range(2, n+1):
  for i in range(1, n-l+2):
    j = i + l - 1
    m[i][j] = 10000000000000000
    for k in range(i, j):
      if m[i][j]>m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]:
        m[i][j] = m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]

print m[1][n]
Buggy python submission (verdict: Runtime Error):
n = input()
a = []
for i in range(n):
  M.append(map(int, raw_input().split()))
for i in range(n):
  for j in range(n):
    if M[i][j]==-1:
      M[i][j] = 1000000000
d = [1000000000 for i range(n)]
p = [-1 for i in range(n)]
color = [0 for i in range(n)]

d[0] = 0

while 1:
  minv = 1000000000
  u = -1
  for i in range(n):
    if minv>d[i] and color[i]!=2
      u = i
      minv = d[i]
  if u = -1:
    break
  color[u] = 2
  for v in range(n):
    if color[v]!=2 and M[u][v]!=1000000000:
      if d[v]>M[u][v]:
        d[v] = M[u][v]
        p[v] = u
        color[v] = 1

sum = 0
for i in range(n):
  if p[i]!=-1:
    sum += M[i][p[1]]
print sum

Fixed submission:
n = input()
M = []
inf = 1000000000
for i in range(n):
  M.append(map(int, raw_input().split()))
for i in range(n):
  for j in range(n):
    if M[i][j]==-1:
      M[i][j] = inf
d = [inf for i in range(n)]
p = [-1 for i in range(n)]
color = [0 for i in range(n)]

d[0] = 0

while 1:
  minv = inf
  u = -1
  for i in range(n):
    if minv>d[i] and color[i]!=2:
      u = i
      minv = d[i]
  if u == -1:
    break
  color[u] = 2
  for v in range(n):
    if color[v]!=2 and M[u][v]!=inf:
      if d[v]>M[u][v]:
        d[v] = M[u][v]
        p[v] = u
        color[v] = 1

sum = 0
for i in range(n):
  if p[i]!=-1:
    sum += M[i][p[i]]
print sum
Buggy python submission (verdict: Runtime Error):
n = input()

S = [int(x) for x in raw_input().split]

q = input()

T = [int(x) for x in raw_input().split]

C = 0

for i in S:
 for j in T:
  if i==j:
   C += 1

print C

Fixed submission:
n = input()

S = [int(x) for x in raw_input().split()]

q = input()

T = [int(x) for x in raw_input().split()]

C = 0

for i in T:
 for j in S:
  if i==j:
   C += 1
   break

print C

Buggy python submission (verdict: Runtime Error):
n = input()

S = [int(x) for x in raw_input().split()]

q = input()

T = [int(x) for x in raw_input().split()]

C = 0

for i in S:
 for j in T:
  if i==j:
   C += 1
   break:

print C

Fixed submission:
n = input()

S = [int(x) for x in raw_input().split()]

q = input()

T = [int(x) for x in raw_input().split()]

C = 0

for i in T:
 for j in S:
  if i==j:
   C += 1
   break

print C