Problem p02683 (python)

Fixed submission:
import sys
n,m,x=map(int,input().split())
ac=[[0]*(m+1) for i in range(n)]
a=[[0]*m for i in range(0,n)]
c=[0 for i in range(n)]
for i in range(n):
  ac[i]=list(map(int,input().split()))
  c[i]=ac[i][0]
  a[i]=ac[i][1:m+1]#sliceは上限+１を:の右に代入
cheap=[]
kaukadouka=[0 for i in range(n)]

def kau(i):
  global a,x,kaukadouka,cheap,c
  rikaido=[0 for i in range(m)]#初期化
  for j in range(i+1,n):
    kaukadouka[j]=0
  if i>=n:
      sys.exit()
  kaukadouka[i]=1
  for j in range(n):
    for k in range(m):
      rikaido[k]+=kaukadouka[j]*a[j][k]
  value=0
  if min(rikaido)>=x:
    for j in range(n):
      value+=c[j]*kaukadouka[j]
    cheap.append(value)
  if i<n-1:
    kau(i+1)
    kawanai(i+1)
    
def kawanai(i):
  global kaukadouka
  for j in range(i,n):
    kaukadouka[j]=0

  if i>=n:
    sys.exit()
  #print('i=',i,'のとき買わないで')
  #print('->',kaukadouka)

  if i<n-1:
    kau(i+1)
    kawanai(i+1)

kau(0)
kawanai(0)
if len(cheap)>0:
	#print(cheap)
	print(min(cheap))
else:
  print('-1')

Buggy submission (verdict: Runtime Error):
import sys
n,m,x=map(int,input().split())
ac=[[0]*(m+1) for i in range(n)]
a=[[0]*m for i in range(0,n)]
c=[0 for i in range(n)]
for i in range(n):
  ac[i]=list(map(int,input().split()))
  c[i]=ac[i][0]
  a[i]=ac[i][1:m+1]#sliceは上限+１を:の右に代入
cheap=[]
ccc=[]
finished=[]
candi=[]
neighbor=[[] for i in range(2**(n+1)-1)]
neighbor[0]=[1,2]
for i in range(2**(n+1)-1):
  if i<=2**n-2 and i>0:
    neighbor[i].append(2*i+1)
    neighbor[i].append(2*i+2)
    if i%2==0:
      neighbor[i].append((i-2)//2)
    else:
      neighbor[i].append((i-1)//2)
  if i>2**n-2:
    if i%2==0:
      neighbor[i].append((i-2)//2)
    else:
      neighbor[i].append((i-1)//2)
#print(neighbor)     
#ある一つの参考書をを買うことを一つのノード、買わないことを一つのノードで表す。
#このネットワークでは木の深さが参考書番号に対応する。
#深さの浅いものを小さく、同じ深さのものを左から小さい順にノード番号を振る
#i->ノード番号(not 参考書番号)
def dfs(i):
  #print(i)
  check=i in finished
  kaukadouka=[]
  cost=0
  rikaido=[0 for i in range(m)]
  if check==0:
    finished.append(i)
    for j in range(len(neighbor[i])):
      y=neighbor[i][j] in finished
      z=neighbor[i][j] in candi
      if y==0 and z==0:
        candi.append(neighbor[i][j])
    if i>2**n-2:
      o=i
      t=n-1
      while o>0:
        kaukadouka.append(o%2)
        p=min(neighbor[o])
        cost+=c[t]*(o%2)
        for h in range(m):
          rikaido[h]+=a[t][h]*(o%2)
        o=p
        t-=1
      if min(rikaido)>=x:
        cheap.append(cost)
      ccc.append(i)
    if len(candi)>0:
      r=candi.pop()
      dfs(r)
  else:
    r=candi.pop()
    dfs(r)
dfs(0)
#print(cheap)
#print(ccc)
if len(cheap)>0:
  print(min(cheap))
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
import sys
n,m,x=map(int,input().split())
ac=[[0]*(m+1) for i in range(n)]
a=[[0]*m for i in range(0,n)]
c=[0 for i in range(n)]
for i in range(n):
  ac[i]=list(map(int,input().split()))
  c[i]=ac[i][0]
  a[i]=ac[i][1:m+1]#sliceは上限+１を:の右に代入
cheap=[]
ccc=[]
finished=[]
candi=[]
neighbor=[[] for i in range(2**(n+1)-1)]
neighbor[0]=[1,2]
for i in range(2**(n+1)-1):
  if i<=2**n-2 and i>0:
    neighbor[i].append(2*i+1)
    neighbor[i].append(2*i+2)
    if i%2==0:
      neighbor[i].append((i-2)//2)
    else:
      neighbor[i].append((i-1)//2)
  if i>2**n-2:
    if i%2==0:
      neighbor[i].append((i-2)//2)
    else:
      neighbor[i].append((i-1)//2)
#print(neighbor)     
#ある一つの参考書をを買うことを一つのノード、買わないことを一つのノードで表す。
#このネットワークでは木の深さが参考書番号に対応する。
#深さの浅いものを小さく、同じ深さのものを左から小さい順にノード番号を振る
#i->ノード番号(not 参考書番号)
def dfs(i):
  #print(i)
  check=i in finished
  kaukadouka=[]
  cost=0
  rikaido=[0 for i in range(m)]
  if check==0:
    finished.append(i)
    for j in range(len(neighbor[i])):
      y=neighbor[i][j] in finished
      z=neighbor[i][j] in candi
      if y==0 and z==0:
        candi.append(neighbor[i][j])
    if i>2**n-2:
      o=i
      t=n-1
      while o>0:
        kaukadouka.append(o%2)
        p=min(neighbor[o])
        cost+=c[t]*(o%2)
        for h in range(m):
          rikaido[h]+=a[t][h]*(o%2)
        o=p
        t-=1
      if min(rikaido)>=x:
        cheap.append(cost)
      ccc.append(i)
    if len(candi)>0:
      r=candi.pop()
      dfs(r)
  else:
    r=candi.pop()
    dfs(r)
dfs(0)
#print(cheap)
#print(ccc)
if len(cheap)>0:
  print(min(cheap))
else:
  print(-1)