Problem p03201 (python)

Fixed submission:
import math
import bisect
n=int(input())
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(1,50):
  b.append(2**i)
ct=0
check=[1]*n
for i in range(n-1,-1,-1):
  if a[i]==1:
    break
  if check[i]==1 and a[i] not in b:
    check[i]=0
    x=math.floor(math.log2(a[i]))
    x=b[x]-a[i]
    y=bisect.bisect_left(a,x)
    z=bisect.bisect_right(a,x)
    t=bisect.bisect_left(check[y:z],1)+y
    if t!=z:
      check[t]=0
      ct+=1

ctb=0
w=1
for i in range(n):
  if a[i] in b or a[i]==1:
    if a[i]==w:
      if check[i]==1:
        ctb+=1
    else:
      ct+=ctb//2
      w=a[i]
      if check[i]==1:
        ctb=1
      else:
        ctb=0
  else:
    ct+=ctb//2
    ctb=0
ct+=ctb//2

print(ct)

Buggy submission (verdict: Runtime Error):
import math
import bisect
n=int(input())
a=list(map(int,input().split()))
a.sort()
b=[]
for i in range(1,50):
  b.append(2**i)
ct=0
check=[1]*n
for i in range(n-1,-1,-1):
  if a[i]==1:
    break
  if check[i]==1 and a[i] not in b:
    check[i]=0
    x=math.floor(math.log2(a[i]))
    x=b[x]-a[i]
    y=bisect.bisect_left(a,x)
    z=bisect.bisect_right(a,x)
    t=bisect.bisect_left(check[y:z],1)+y
    if t!=z:
      check[t]=0
      ct+=1

ctb=0
w=1
for i in range(30):
  if a[i] in b or a[i]==1:
    if a[i]==w:
      if check[i]==1:
        ctb+=1
    else:
      ct+=ctb//2
      w=a[i]
      if check[i]==1:
        ctb=1
      else:
        ctb=0
  else:
    ct+=ctb//2
    ctb=0
ct+=ctb//2

print(ct)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ct=0
 
M=max(a)
min=min(a)

if m>=10**8:
  print(0)
else:
  for i in range(100):
    if 2**i>M:
      x=i
      break
  L=[]
  for j in range(2**x-1):
    L.append(0)
  
  for j2 in range(n):
    L[a[j2]-1]+=1
  
  for k in range(x,1,-1):
    l1=L[0:2**(k-1)-1]
    l2=L[2**(k-1):2**k]
    l2.reverse()
    ct+=int(L[2**(k-1)-1]/2)
    for m in range(len(l1)):
      if l1[m]>=1 and l2[m]>=1:
        ct+=min(l1[m],l2[m])
        l1[m]-=min(l1[m],l2[m])
        l2[m]-=min(l1[m],l2[m])
    L=l1
  ct+=int(L[0]/2)
  print(ct)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ct=0
list=[]

if n==1:print(0)
else:
  for g in range(50):
    list.append(2**g)
  
  M=max(a)
  
  if M>=10**7:
    while M>=10**7:
      for h in range(len(a)):
        if M<10**7:
          break
        elif M+a[h] in list:
          ct+=1
          a.remove(M)
          a.remove(a[h])
          break
        else:
          a.remove(M)
        M=max(a)
        
  
  
  
  for i in range(50):
    if 2**i>M:
      x=i
      break
  L=[]
  for j in range(2**x-1):
    L.append(0)
  
  for j2 in range(len(a)):
    L[a[j2]-1]+=1
  
  for k in range(x,1,-1):
    l1=L[0:2**(k-1)-1]
    l2=L[2**(k-1):2**k]
    l2.reverse()
    ct+=int(L[2**(k-1)-1]/2)
    for m in range(len(l1)):
      if l1[m]>=1 and l2[m]>=1:
        ct+=min(l1[m],l2[m])
        l1[m]-=min(l1[m],l2[m])
        l2[m]-=min(l1[m],l2[m])
    L=l1
  ct+=int(L[0]/2)
  print(ct)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ct=0
list=[]

if n==1:print(0)
else:
  for g in range(50):
    list.append(2**g)
  
  M=max(a)
  
  if M>=10**6:
    while M>=10**6:
      for h in range(len(a)):
        if M<10**6:
          break
        elif M+a[h] in list:
          ct+=1
          a.remove(M)
          a.remove(a[h])
          break
        else:
          a.remove(M)
        M=max(a)
        
  
  
  
  for i in range(50):
    if 2**i>M:
      x=i
      break
  L=[]
  for j in range(2**x-1):
    L.append(0)
  
  for j2 in range(len(a)):
    L[a[j2]-1]+=1
  
  for k in range(x,1,-1):
    l1=L[0:2**(k-1)-1]
    l2=L[2**(k-1):2**k]
    l2.reverse()
    ct+=int(L[2**(k-1)-1]/2)
    for m in range(len(l1)):
      if l1[m]>=1 and l2[m]>=1:
        ct+=min(l1[m],l2[m])
        l1[m]-=min(l1[m],l2[m])
        l2[m]-=min(l1[m],l2[m])
    L=l1
  ct+=int(L[0]/2)
  print(ct)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ct=0

M=max(a)
for i in range(25):
  if 2**i>M:
    x=i
    break
L=[]
for j in range(2**x-1):
  L.append(0)

for j2 in range(n):
  L[a[j2]-1]+=1

for k in range(x,1,-1):
  l1=L[0:2**(k-1)-1]
  l2=L[2**(k-1):2**k]
  l2.reverse()
  if L[2**(k-1)-1]>=2:
    ct+=int(L[2**(k-1)-1]/2)
  for m in range(len(l1)):
    if l1[m]>=1 and l2[m]>=1:
      ct+=min(l1[m],l2[m])
      l1[m]-=min(l1[m],l2[m])
      l2[m]-=min(l1[m],l2[m])
  L=l1
ct+=int(L[0]/2)
print(ct)

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ct=0

M=max(a)
for i in range(25):
  if 2**i>M:
    x=i
    break
L=[]
for j in range(2**x-1):
  L.append(0)

for j2 in range(n):
  L[a[j2]-1]+=1

for k in range(x,1,-1):
  l1=L[0:2**(k-1)-1]
  l2=L[2**(k-1):2**k]
  l2.reverse()
  if L[2**(k-1)-1]%2==0:
    ct+=int(L[2**(k-1)-1]/2)
  for m in range(len(l1)):
    if l1[m]==l2[m]:
      ct+=l1[m]
      l1[m]=0
      l2[m]=0
  L=l1
ct+=int(L[0]/2)
print(ct)