Problem:
The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.  According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Incorrect submission (verdict: Wrong Answer):
# -*- coding: utf-8 -*-

N, Y = map(int, input().split())
result_10000, result_5000, result_1000 = -1, -1, -1
found = False



for num_10000 in reversed(range(0, Y//10000 + 1)):
    y_10000 = Y - 10000 * num_10000
    n_10000 = N - num_10000
    if y_10000 == 0 and n_10000 == 0:
        print(10000, y_10000, n_10000)
        result_10000, result_5000, result_1000 = num_10000, 0, 0
        found = True
        break
    else:
        for num_5000 in reversed(range(0, y_10000//5000 + 1)):
            y_5000 = y_10000 - 5000 * num_5000
            n_5000 = n_10000 - num_5000
            if y_5000 == 0 and n_5000 == 0 and num_10000 + num_5000 == N:
                print(5000, y_5000, n_5000)
                result_10000, result_5000, result_1000 = num_10000, num_5000, 0
                found = True
                break
            else:
                for num_1000 in reversed(range(0, y_5000//1000 + 1)):
                    y_1000 = y_5000 - 1000 * num_1000
                    n_1000 = n_5000 - num_1000
                    if y_1000 == 0 and n_1000 == 0 and num_10000 + num_5000 + num_1000 == N:
                        print(1000, y_1000, n_1000)
                        result_10000, result_5000, result_1000 = num_10000, num_5000, num_1000
                        found = True
                        break
            if found:
                break
    if found:
        break
    
print(result_10000, result_5000, result_1000)



Fixed, accepted submission:
# -*- coding: utf-8 -*-

N, Y = map(int, input().split())
result_10000, result_5000, result_1000 = -1, -1, -1
found = False



for num_10000 in reversed(range(0, Y//10000 + 1)):
    y_10000 = Y - 10000 * num_10000
    n_10000 = N - num_10000
    if y_10000 == 0 and n_10000 == 0:
        result_10000, result_5000, result_1000 = num_10000, 0, 0
        found = True
        break
    else:
        for num_5000 in reversed(range(0, y_10000//5000 + 1)):
            y_5000 = y_10000 - 5000 * num_5000
            n_5000 = n_10000 - num_5000
            if y_5000 < 0 or n_5000 < 0:
                break
            if y_5000 == 0 and n_5000 == 0 and num_10000 + num_5000 == N:
                result_10000, result_5000, result_1000 = num_10000, num_5000, 0
                found = True
                break
            else:
                
                num_1000 = y_5000 // 1000
                y_1000 = y_5000 - 1000 * num_1000
                n_1000 = n_5000 - num_1000
                if y_1000 == 0 and n_1000 == 0 and num_10000 + num_5000 + num_1000 == N:
                    result_10000, result_5000, result_1000 = num_10000, num_5000, num_1000
                    found = True
                    break
            if found:
                break
        if found:
            break
    
print(result_10000, result_5000, result_1000)