Buggy python submission (verdict: Runtime Error):
def calc(c1,c2,c3,c4):
    return c1*10 + c2*50 + c3*100 + c4*500

def coindiff(p,c1,c2,c3,c4):
    n = c1+c2+c3+c4
    over = calc(c1,c2,c3,c4) - p
    n -= (over%50) // 10
    n -= (over%100) // 50
    n -= (over%500) // 100
    n -= over // 500
    return n

while True:
    N = int(input())
    if N == 0: break
    c1,c2,c3,c4 = map(int,input().split())
    s1 = (N//10) % 5
    maxdiff = -9999999
    ans = None
    for n1 in range(s1,c1+1,5):
        for n2 in range(c2+1):
            for n3 in range(c3+1):
                rem = (N - n1*10 - n2*50 - n3*100)
                n4 = max(0, (rem-1)//500 + 1)
                d = coindiff(N,n1,n2,n3,n4)
                if d > maxdiff:
                    maxdiff = d
                    ans = {10:n1,50:n2,100:n3,500:n4}
    for k,v in sorted(ans.items()):
        print('{0} {1}'.format(k, v))
    print('')

Fixed submission:
first = True
while True:
    P = int(input())
    if P== 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    v = c1*10 + c2*50 + c3*100 + c4*500
    n = c1 + c2 + c3 + c4
    ans = {}
    rem = v - P
    ans[10] = c1 - (rem//10) % 5
    ans[50] = c2 - (rem//50) % 2
    ans[100] = c3 - (rem//100) % 5
    ans[500] = c4 - rem//500
    for k,v in sorted(ans.items()):
        if v <= 0: continue
        print('{0} {1}'.format(k,v))

Buggy python submission (verdict: Runtime Error):
def calc(c1,c2,c3,c4):
    return c1*10 + c2*50 + c3*100 + c4*500

def coindiff(p,c1,c2,c3,c4):
    n = c1+c2+c3+c4
    over = calc(c1,c2,c3,c4) - p
    n -= (over%50) // 10
    n -= (over%100) // 50
    n -= (over%500) // 100
    n -= over // 500
    return n

while True:
    N = int(input())
    if N == 0: break
    c1,c2,c3,c4 = map(int,input().split())
    s1 = (N//10) % 5
    maxdiff = -9999999
    ans = None
    for n1 in range(s1,c1+1,5):
        for n2 in range(c2+1):
            for n3 in range(c3+1):
                rem = (N - n1*10 - n2*50 - n3*100)
                n4 = max(0, (rem-1)//500 + 1)
                d = coindiff(N,n1,n2,n3,n4)
                if d > maxdiff:
                    maxdiff = d
                    ans = {10:n1,50:n2,100:n3,500:n4}
    for k,v in sorted(ans.items()):
        if v == 0: continue
        print('{0} {1}'.format(k, v))
    print('')

Fixed submission:
first = True
while True:
    P = int(input())
    if P== 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    v = c1*10 + c2*50 + c3*100 + c4*500
    n = c1 + c2 + c3 + c4
    ans = {}
    rem = v - P
    ans[10] = c1 - (rem//10) % 5
    ans[50] = c2 - (rem//50) % 2
    ans[100] = c3 - (rem//100) % 5
    ans[500] = c4 - rem//500
    for k,v in sorted(ans.items()):
        if v <= 0: continue
        print('{0} {1}'.format(k,v))

Buggy python submission (verdict: Runtime Error):
def calc(c1,c2,c3,c4):
    return c1*10 + c2*50 + c3*100 + c4*500

def coindiff(p,c1,c2,c3,c4):
    n = c1+c2+c3+c4
    over = calc(c1,c2,c3,c4) - p
    n -= (over%50) // 10
    n -= (over%100) // 50
    n -= (over%500) // 100
    n -= over // 500
    return n

while True:
    N = int(input())
    if N == 0: break
    c1,c2,c3,c4 = map(int,input().split())
    s1 = (N//10) % 5
    maxdiff = -9999999
    ans = None
    for n1 in range(s1,c1+1,5):
        for n2 in range(c2+1):
            for n3 in range(c3+1):
                rem = (N - n1*10 - n2*50 - n3*100)
                n4 = max(0, (rem-1)//500 + 1)
                d = coindiff(N,n1,n2,n3,n4)
                if d > maxdiff:
                    maxdiff = d
                    ans = {10:n1,50:n2,100:n3,500:n4}
    for k,v in sorted(ans.items()):
        if v == 0: continue
        print('{0} {1}'.format(k, v))
    print('')

Fixed submission:
first = True
while True:
    P = int(input())
    if P== 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    v = c1*10 + c2*50 + c3*100 + c4*500
    n = c1 + c2 + c3 + c4
    ans = {}
    rem = v - P
    ans[10] = c1 - (rem//10) % 5
    ans[50] = c2 - (rem//50) % 2
    ans[100] = c3 - (rem//100) % 5
    ans[500] = c4 - rem//500
    for k,v in sorted(ans.items()):
        if v <= 0: continue
        print('{0} {1}'.format(k,v))

Buggy python submission (verdict: Runtime Error):
def calc(c1,c2,c3,c4):
    return c1*10 + c2*50 + c3*100 + c4*500

def coindiff(p,c1,c2,c3,c4):
    n = c1+c2+c3+c4
    over = calc(c1,c2,c3,c4) - p
    n -= (over%50) // 10
    n -= (over%100) // 50
    n -= (over%500) // 100
    n -= over // 500
    return n

first = True
while True:
    N = int(input())
    if N == 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    s1 = (N//10) % 5
    maxdiff = -9999999
    ans = None
    for n1 in range(s1,c1+1,5):
        for n2 in range(c2+1):
            for n3 in range(c3+1):
                rem = (N - n1*10 - n2*50 - n3*100)
                n4 = max(0, (rem-1)//500 + 1)
                d = coindiff(N,n1,n2,n3,n4)
                if d > maxdiff:
                    maxdiff = d
                    ans = {10:n1,50:n2,100:n3,500:n4}
    for k,v in sorted(ans.items()):
        if v == 0: continue
        print('{0} {1}'.format(k, v))

Fixed submission:
first = True
while True:
    P = int(input())
    if P== 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    v = c1*10 + c2*50 + c3*100 + c4*500
    n = c1 + c2 + c3 + c4
    ans = {}
    rem = v - P
    ans[10] = c1 - (rem//10) % 5
    ans[50] = c2 - (rem//50) % 2
    ans[100] = c3 - (rem//100) % 5
    ans[500] = c4 - rem//500
    for k,v in sorted(ans.items()):
        if v <= 0: continue
        print('{0} {1}'.format(k,v))

Buggy python submission (verdict: Runtime Error):
def calc(c1,c2,c3,c4):
    return c1*10 + c2*50 + c3*100 + c4*500

def coindiff(p,c1,c2,c3,c4):
    n = c1+c2+c3+c4
    over = calc(c1,c2,c3,c4) - p
    n -= (over%50) // 10
    n -= (over%100) // 50
    n -= (over%500) // 100
    n -= over // 500
    return n

first = True
while True:
    N = int(input())
    if N == 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    s1 = (N//10) % 5
    maxdiff = -9999999
    ans = None
    for n1 in range(s1,c1+1,5):
        for n2 in range(c2+1):
            rem2 = N - n1*10 - n2*50
            s3 = ((rem2-1)//100 + 1) % 5
            for n3 in range(s3,c3+1,5):
                rem3 = rem2 - n3*100
                n4 = max(0, (rem3-1)//500 + 1)
                d = coindiff(N,n1,n2,n3,n4)
                if d > maxdiff:
                    maxdiff = d
                    ans = {10:n1,50:n2,100:n3,500:n4}
    for k,v in sorted(ans.items()):
        if v == 0: continue
        print('{0} {1}'.format(k, v))

Fixed submission:
first = True
while True:
    P = int(input())
    if P== 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    v = c1*10 + c2*50 + c3*100 + c4*500
    n = c1 + c2 + c3 + c4
    ans = {}
    rem = v - P
    ans[10] = c1 - (rem//10) % 5
    ans[50] = c2 - (rem//50) % 2
    ans[100] = c3 - (rem//100) % 5
    ans[500] = c4 - rem//500
    for k,v in sorted(ans.items()):
        if v <= 0: continue
        print('{0} {1}'.format(k,v))

Buggy python submission (verdict: Runtime Error):
def calc(c1,c2,c3,c4):
    return c1*10 + c2*50 + c3*100 + c4*500

def coindiff(p,c1,c2,c3,c4):
    n = c1+c2+c3+c4
    over = calc(c1,c2,c3,c4) - p
    n -= (over%50) // 10
    n -= (over%100) // 50
    n -= (over%500) // 100
    n -= over // 500
    return n

first = True
while True:
    N = int(input())
    if N == 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    s1 = (N//10) % 5
    maxdiff = -9999999
    ans = None
    for n1 in range(s1,c1+1,5):
        for n2 in range(c2+1):
            rem2 = N - n1*10 - n2*50
            s3 = ((rem2-1)//100 + 1) % 5
            for n3 in range(s3,c3+1,5):
                rem3 = rem2 - n3*100
                n4 = max(0, (rem3-1)//500 + 1)
                d = coindiff(N,n1,n2,n3,n4)
                if d > maxdiff:
                    maxdiff = d
                    ans = {10:n1,50:n2,100:n3,500:n4}
    for k,v in sorted(ans.items()):
        if v == 0: continue
        print('{0} {1}'.format(k, v))

Fixed submission:
first = True
while True:
    P = int(input())
    if P== 0: break
    if first:
        first = False
    else:
        print('')
    c1,c2,c3,c4 = map(int,input().split())
    v = c1*10 + c2*50 + c3*100 + c4*500
    n = c1 + c2 + c3 + c4
    ans = {}
    rem = v - P
    ans[10] = c1 - (rem//10) % 5
    ans[50] = c2 - (rem//50) % 2
    ans[100] = c3 - (rem//100) % 5
    ans[500] = c4 - rem//500
    for k,v in sorted(ans.items()):
        if v <= 0: continue
        print('{0} {1}'.format(k,v))