Buggy python submission (verdict: Runtime Error):
n, y = map(int,raw_input().split())
y/=1000

h = {0:10, 1:5, 2:1}

mem = {}
ans = [-1,-1,-1]
def dfs(i, n, y, h,res,ans ):
	if (i,n,y) in mem:
		return mem[(i,n,y)]

	if y < 0:

		mem[(i,n,y)] = False
		return False
	if y == 0:
		if n == 0:
			mem[(i,n,y)] = True
			
			if ans[0] == -1: 
				for i in range(len(res)):
					ans[i] = res[i]
			return True
		mem[(i,n,y)] = False
		return False
	if i == 2:
		if n == y:
			res[2]+= y
			mem[(i,n,y)] = True
			

			if ans[0] == -1: 
				for i in range(len(res)):
					ans[i] = res[i]

			return True
		return False
	res[0] +=1
	if dfs(i, n - 1, y - h[i], h,res, ans):
		mem[(i,n,y)] = True

		if ans[0] == -1: 
			for i in range(len(res)):
				ans[i] = res[i]
		return True
	res[0] -= 1

	if dfs(i+1, n ,y, h,res, ans):
		mem[(i,n,y)] = True
		
		if not(ans): 
			ans = [e for  e in res]#res[::]
		return True
	mem[(i,n,y)] = False
	return False

res = [0,0,0]
if dfs(0, n, y, h, res, ans):
	print ' '.join(map(str,ans))
else:
	print ' '.join(map(str,[-1,-1,-1]))

Fixed submission:
n, y = map(int,raw_input().split())
y/=1000


def g(y,n):
	for a in range(y/10 + 1):
		b,c = f(y - a * 10, n - a)
		if b != -1:
			return [a,b,c]
	return [-1,-1,-1]
def f(y,n):
	if y < 0 or n < 0:
		return [-1,-1]
	if y == 0:
		if n == 0:

			return [0,0]
		return [-1,-1]

	for b in range(y/5 + 1):
		if y - b * 5 == n - b and n - b >= 0:
			return [b,n-b]
	return [-1,-1]

print ' '.join(map(str, g(y,n)))

Buggy python submission (verdict: Runtime Error):
n, y = map(int,raw_input().split())
y/=1000

h = {0:10, 1:5, 2:1}

mem = {}
def dfs(i, n, y, h,res ):
	if (i,n,y) in mem:
		return mem[(i,n,y)]

	if y < 0:

		mem[(i,n,y)] = False
		return False
	if y == 0:
		if n == 0:
			mem[(i,n,y)] = True
			return True
		mem[(i,n,y)] = False
		return False
	if i == 2:
		if n == y:
			res[2]+= y
			mem[(i,n,y)] = True
			return True
		return False
	res[0] +=1
	if dfs(i, n - 1, y - h[i], h,res):
		mem[(i,n,y)] = True
		return True
	res[0] -= 1

	if dfs(i+1, n ,y, h,res):
		mem[(i,n,y)] = True
		return True
	mem[(i,n,y)] = False
	return False

res = [0,0,0]
if dfs(0, n, y, h, res):
	print ' '.join(map(str,res))
else:
	print ' '.join(map(str,[-1,-1,-1]))

Fixed submission:
n, y = map(int,raw_input().split())
y/=1000


def g(y,n):
	for a in range(y/10 + 1):
		b,c = f(y - a * 10, n - a)
		if b != -1:
			return [a,b,c]
	return [-1,-1,-1]
def f(y,n):
	if y < 0 or n < 0:
		return [-1,-1]
	if y == 0:
		if n == 0:

			return [0,0]
		return [-1,-1]

	for b in range(y/5 + 1):
		if y - b * 5 == n - b and n - b >= 0:
			return [b,n-b]
	return [-1,-1]

print ' '.join(map(str, g(y,n)))

Buggy python submission (verdict: Runtime Error):
n, y = map(int,raw_input().split())
y/=1000

h = {0:10, 1:5, 2:1}

def dfs(i, n, y, h,res ):
	if y < 0:
		return False
	if y == 0:
		if n == 0:
			return True

		return False
	if i == 2:
		if n == y:
			res[2]+= y
			return True
		return False
	res[0] +=1
	if dfs(i, n - 1, y - h[i], h,res):
		return True
	res[0] -= 1

	if dfs(i+1, n ,y, h,res):
		return True
	return False

res = [0,0,0]
if dfs(0, n, y, h, res):
	print y, res[0] * 10 + res[1]*5 + res[2]
	print res
else:
	print [-1,-1,-1]

Fixed submission:
n, y = map(int,raw_input().split())
y/=1000


def g(y,n):
	for a in range(y/10 + 1):
		b,c = f(y - a * 10, n - a)
		if b != -1:
			return [a,b,c]
	return [-1,-1,-1]
def f(y,n):
	if y < 0 or n < 0:
		return [-1,-1]
	if y == 0:
		if n == 0:

			return [0,0]
		return [-1,-1]

	for b in range(y/5 + 1):
		if y - b * 5 == n - b and n - b >= 0:
			return [b,n-b]
	return [-1,-1]

print ' '.join(map(str, g(y,n)))