Problem p03128 (python)

Fixed submission:
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]

c = list(map(int,input().split()))

c.sort()
cc = [[c[i],k[c[i]-1]] for i in range(b)]
ccc = [[c[i],k[c[i]-1]] for i in range(b)]
dp = [-1 for i in range(a+1)]
dp[0] = 0
cc.sort(key = lambda x:x[1])
for j in range(b):
	for i in range(a+1):
		if i-cc[j][1] >= 0 and dp[i-cc[j][1]] >= 0:
			dp[i] = max(dp[i-cc[j][1]]+1,dp[i])
ccc.reverse()
k = a
co = ''
for i in range(dp[a]):
	for j in range(b):
		if k-ccc[j][1] >= 0 and dp[k-ccc[j][1]] == dp[k] - 1:
			co += str(ccc[j][0])
			k -= ccc[j][1]
			break
print(co)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]

c = list(map(int,input().split()))

c.sort()
cc = [[c[i],k[c[i]-1]] for i in range(b)]
ccc = [[c[i],k[c[i]-1]] for i in range(b)]
dp = [-1 for i in range(a+1)]
dp[0] = 0
cc.sort(key = lambda x:x[1])
for j in range(b):
	for i in range(a+1):
		if i-cc[j][1] >= 0 and dp[i-cc[j][1]] >= 0:
			dp[i] = max(dp[i-cc[j][1]]+1,dp[i])
ccc.reverse()
k = a
co = ''
for i in range(dp[a]):
	for j in range(b):
		if dp[k-ccc[j][1]] == dp[k] - 1 and k-ccc[j][1] >= 0:
			co += str(ccc[j][0])
			k -= ccc[j][1]
			break
print(co)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]

c = list(map(int,input().split()))

c.sort()
cc = [[c[i],k[c[i]-1]] for i in range(b)]
ccc = [[c[i],k[c[i]-1]] for i in range(b)]
dp = [-1 for i in range(a+1)]
cc.sort(key = lambda x:x[1])
for j in range(b):
	for i in range(a+1):
		if dp[i-cc[j][1]] >= 0:
			dp[i] = max(dp[i-cc[j][1]]+1,dp[i])

k = a
co = ''
for i in range(dp[a]):
	for j in range(b):
		if dp[k-ccc[j][1]] == dp[k] and k-ccc[j][1] >= 0:
			co += str(ccc[j][0])
			k -= ccc[j][1]
			break
print(co)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]

c = list(map(int,input().split()))

c.sort()
c.reverse()
cc = [[c[i],k[c[i]-1]] for i in range(b)]
ccc = [[c[i],k[c[i]-1]] for i in range(b)]
dp = [-1 for i in range(a+1)]
dp[0] = 0
cc.sort(key = lambda x:x[1])
for j in range(b):
	for i in range(a+1):
		if cc[j][1] <= i:
			dp[i] = max(dp[i-cc[j][1]]+1,1,dp[i])

k = a
co = ''
for i in range(dp[a]+2):
	for j in range(b):
		if dp[k-ccc[j][1]] == dp[k]-1:
			co = co + str(ccc[j][0])
			k -= ccc[j][1]
			break
print(co)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]

c = list(map(int,input().split()))

c.sort()
cc = [[c[i],k[c[i]-1]] for i in range(b)]
ccc = [[c[i],k[c[i]-1]] for i in range(b)]
dp = [0 for i in range(a+1)]
cc.sort(key = lambda x:x[1])
for j in range(b):
	for i in range(a+1):
		if cc[j][1] <= i:
			dp[i] = max(dp[i-cc[j][1]]+1,1,dp[i])

k = a
co = ''
for i in range(dp[a]+2):
	for j in range(b):
		if dp[k-ccc[j][1]] == dp[k]-1:
			co = str(ccc[j][0]) + co
			k -= ccc[j][1]
			break
print(co)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]

c = list(map(int,input().split()))

c.sort()
cc = [[c[i],k[c[i]-1]] for i in range(b)]
ccc = [[c[i],k[c[i]-1]] for i in range(b)]
dp = [-1 for i in range(a+1)]
cc.sort(key = lambda x:x[1])
for j in range(b):
	for i in range(a+1):
		if cc[j][1] <= i:
			dp[i] = max(dp[i-cc[j][1]]+1,1,dp[i])

k = a
co = ''
for i in range(dp[a]):
	for j in range(b):
		if dp[k-ccc[j][1]] == dp[k]-1:
			co = str(ccc[j][0]) + co
			k -= ccc[j][1]
			break
print(co)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]
c = list(map(int,input().split()))
cc = [[0,0] for i in range(b)]
ccc = [[0,0] for i in range(b)]
c.sort(reverse = True)
for i in range(b):
	cc[i] = [c[i],k[c[i]-1]]
	ccc[i]= [c[i],k[c[i]-1]]

cc.sort(key = lambda x:x[1])
ccc.sort(reverse = True)
p = a // cc[0][1]
q = a % cc[0][1]
co = [cc[0][0] for i in range(p)]
for i in range(p):
	for j in range(b):
		if ccc[j][0] > cc[0][0] and q >= ccc[j][1] - cc[0][1]:
			co[i] = ccc[j][0]
			q -= ccc[j][1] - cc[0][1]
			break
	if q == 0:
		break

for i in range(p):
	for j in range(b):
		if q >= cc[j][1] - k[co[-(i+1)]-1] and cc[j][1] > k[co[-(i+1)]-1]:
			q -= cc[j][1] - k[co[-(i+1)]-1]
			co[-(i+1)] = cc[j][0]
	if q == 0:
		break
if q != 0:
	for m in range(a):
		if c[0][1] != c[m][1]:
			p = a // cc[m][1]
			q = a % cc[m][1]
			co = [cc[m][0] for j in range(p)]
			for i in range(p):
				for j in range(b):
					if ccc[j][0] > cc[0][0] and q >= ccc[j][1] - cc[0][1]:
						co[i] = ccc[j][0]
						q -= ccc[j][1] - cc[0][1]
					break
				if q == 0:
					break

			for i in range(p):
				for j in range(b):
					if q >= cc[j][1] - k[co[-(i+1)]-1] and cc[j][1] > k[co[-(i+1)]-1]:
						q -= cc[j][1] - k[co[-(i+1)]-1]
						co[-(i+1)] = cc[j][0]
				if q == 0:
					break

			break


co.sort(reverse = True)

co = map(str,co)
aaa = ''.join(co)
print(aaa)

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
k = [2,5,5,4,5,6,3,7,6]
c = list(map(int,input().split()))
cc = [[0,0] for i in range(b)]
ccc = [[0,0] for i in range(b)]
c.sort(reverse = True)
for i in range(b):
	cc[i] = [c[i],k[c[i]-1]]
	ccc[i]= [c[i],k[c[i]-1]]

cc.sort(key = lambda x:x[1])
ccc.sort(reverse = True)
p = a // cc[0][1]
q = a % cc[0][1]
co = [cc[0][0] for i in range(p)]
for i in range(p):
	for j in range(b):
		if ccc[j][0] > cc[0][0] and q >= ccc[j][1] - cc[0][1]:
			co[i] = ccc[j][0]
			q -= ccc[j][1] - cc[0][1]
			break
	if q == 0:
		break

for i in range(p):
	for j in range(b):
		if q >= cc[j][1] - k[co[-(i+1)]-1] and cc[j][1] > k[co[-(i+1)]-1]:
			q -= cc[j][1] - k[co[-(i+1)]-1]
			co[-(i+1)] = cc[j][0]
	if q == 0:
		break
if q != 0:
	for i in range(a):
		if c[0][1] != c[i][1]:
			p = a // cc[i][1]
			q = a % cc[i][1]
			co = [cc[i][0] for j in range(p)]
			for i in range(p):
				for j in range(b):
					if ccc[j][0] > cc[0][0] and q >= ccc[j][1] - cc[0][1]:
						co[i] = ccc[j][0]
						q -= ccc[j][1] - cc[0][1]
					break
				if q == 0:
					break

			for i in range(p):
				for j in range(b):
					if q >= cc[j][1] - k[co[-(i+1)]-1] and cc[j][1] > k[co[-(i+1)]-1]:
						q -= cc[j][1] - k[co[-(i+1)]-1]
						co[-(i+1)] = cc[j][0]
				if q == 0:
					break

			break


co.sort(reverse = True)

co = map(str,co)
aaa = ''.join(co)
print(aaa)