Problem p02683 (python)

Fixed submission:
n, m, x = list(map(int, input().split()))

a = []
for i in range(n):
    b = list(map(int, input().split()))
    a.append(b)


def calcPoint(point, n):
    for i in range(m):
        point[i] += a[n][i+1]
    return point


def judgePoint(point):
    for i in range(m):
        if point[i] < x:
            return False
    return True


ans = []
for i in range(2**n):  # patarn数
    buy = []
    for j in range(n):  # n bit
        if((i >> j) & 1):  # iをj bit 右にずらして1ならば
            buy.append(1)
        else:
            buy.append(0)
    price = 0
    point = [0]*m
    for i in range(n):
        if buy[i] == 1:
            calcPoint(point, i)
            price += a[i][0]
    if judgePoint(point):
        ans.append(price)
if len(ans) == 0:
    print(-1)
else:
    print(min(ans))

Buggy submission (verdict: Runtime Error):

from fractions import gcd
from functools import reduce
# from collections import deque
# from math import factorial
import collections
import math
import sys
sys.setrecursionlimit(2000000)
# import itertools
# import statistics
# import numpy as np
# n = int(input())
# a, b, c, k = list(map(int, input().split()))
# a = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
# n = int(input())
# n, m = list(map(int, input().split()))
n, m, x = list(map(int, input().split()))
# a = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
# n = int(input())
# n, m = list(map(int, input().split()))
a = []
for i in range(n):
    b = list(map(int, input().split()))
    t = sum(b)-b[0]
    # print(t)
    b.append(b[0]/t)
    a.append(b)

a_sorted = sorted(a, key=lambda x: x[-1])
# print(a_sorted)

money = 0
point = [0]*m
over = [0]*m
for i in range(n):
    fin = True
    money += a_sorted[i][0]
    for j in range(m):
        point[j] += a_sorted[i][j+1]
    for j in range(m):
        if point[j] < x:
            fin = False
        else:
            over[j] = point[j]-x
    if fin:
        k = []
        for i in range(n):
            kouho = True
            for j in range(m):
                if a_sorted[i][j+1] > over[j]:
                    kouho = False
            if kouho:
                k.append(i)
        k.sort(reverse=True)
        if len(k) != 0:
            for i in range(len(k)):
                for j in range(m):
                    point[j] -= a_sorted[i][j+1]
                    if point[j] < x:
                        print(money)
                        exit()
                money -= a_sorted[i][0]
        # print(over)
        for j in range(m):
            print(money)
            exit()
    # print(check)
print(-1)

Buggy submission (verdict: Runtime Error):

from fractions import gcd
from functools import reduce
# from collections import deque
# from math import factorial
import collections
import math
import sys
sys.setrecursionlimit(2000000)
n, m, x = list(map(int, input().split()))
# a = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
#n = int(input())
# n, m = list(map(int, input().split()))
a = []
for i in range(n):
    b = list(map(int, input().split()))
    t = sum(b)-b[0]
    #print(t)
    b.append(b[0]/t)
    a.append(b)

a_sorted = sorted(a, key=lambda x: x[-1])

money = 0
point = [0]*m
for i in range(n):
    fin = True
    money += a_sorted[i][0]
    for j in range(m):
        #print(money)
        point[j] += a_sorted[i][j+1]
        # print(a_sorted[i][0])
    #print(point, end=" ")
    #print(money)
    for j in range(m):
        if point[j] < x:
            fin = False
    if fin:
        print(money)
        exit()

    # print(check)
print(-1)