Problem:
Problem J: ICPC: Ideal Coin Payment and Change Taro, a boy who hates any inefficiencies, pays coins so that the number of coins to be returned as change is minimized in order to do smoothly when he buys something. One day, however, he doubt if this way is really efficient.  When he pays more number of coins, a clerk consumes longer time to find the total value.  Maybe he should pay with least possible number of coins. Thinking for a while, he has decided to take the middle course.  So he tries to minimize total number of paid coins and returned coins as change. Now he is going to buy a product of P yen having several coins.  Since he is not good at calculation, please write a program that computes the minimal number of coins. You may assume following things:  There are 6 kinds of coins, 1 yen, 5 yen, 10 yen, 50 yen, 100 yen and 500 yen.  The total value of coins he has is at least P yen.  A clerk will return the change with least number of coins.  Input Input file contains several data sets.
One data set has following format:  P  N 1  N 5  N 10  N 50  N 100  N 500   N i is an integer and is the number of coins of i yen that he have. The end of input is denoted by a case where P = 0.  You should output nothing for this data set. Output Output total number of coins that are paid and are returned. Constraints  Judge data contains at most 100 data sets.  0 ≤ N i ≤ 1000  Sample Input 123 3 0 2 0 1 1
999 9 9 9 9 9 9
0 0 0 0 0 0 0 Output for the Sample Input 6
3

Solution (Python):



c = (500, 100, 50, 10, 5, 1)

def pay(p):
	cnt = 0
	for i in range(6):
		if c[i] <= p:
			k = p//c[i]
			if k > n[i]: k = n[i]
			cnt += k
			p -= c[i]*k
	return -1 if p > 0 else cnt

k = [0]*1001
for p in range(1, 1001):
	cnt, q = 0, p
	for j in range(6):
		if c[j] <= q:
			cnt += q//c[j]
			q %= c[j]
	k[p] = cnt;

while True:
	a = list(map(int, input().split()))
	p = a[0]
	if p == 0: break
	del a[0]
	n = a[::-1]
	ans = 0x7fffffff
	for i in range(p, p+1001):
		j = pay(i)
		if j < 0: continue
		ans = min(ans, j + k[i-p])
	print(ans)

Solution (Python):
price = [1, 5, 10, 50, 100, 500]

while True:
    values = list(map(int, input().split()))
    p, n = values[0], values[1:]
    ans = 1e100
    if p == 0:
        break

    p_sum = sum(map(lambda s: s[0] * s[1], zip(price, n)))

    for change in range(1000):
        total = p + change
        pay = [0] * 6
        for i in reversed(range(6)):
            if total >= price[i]:
                pay[i] = min(n[i], int(total / price[i]))
                total -= pay[i] * price[i]

        if total > 0:
            break

        coins = sum(pay)

        _change = change
        for i in reversed(range(6)):
            if _change >= price[i]:
                coins += int(_change / price[i])
                _change %= price[i]

        ans = min(ans, coins)

    print(ans)

Solution (Python):



import sys

coin = [1, 5, 10, 50, 100, 500]

def charge(remain):
	if remain == 0:
		return [0] * 6
	ch = [0] * 6
	index = 5
	while remain > 0:
		while remain >= coin[index]:
			ch[index] += 1
			remain -= coin[index]
		index -= 1
	return ch

def compute(p, n):
	candidates = []
	for r in range(500):
		pay = [0] * 6
		q = p + r
		for i in range(5, -1, -1):
			m = q / coin[i]
			pay[i] = min(m, n[i])
			q -= pay[i] * coin[i]
		if q == 0:
			candidates.append(sum(pay) + sum(charge(r)))
	return min(candidates)
	
if __name__ == '__main__':
	for input in sys.stdin:
		input = map(int, input.split())
		p, n = input[0], input[1:]
		if p == 0:
			break
		print compute(p, n)

Solution (Python):
while True:
    p, n1, n5, n10, n50, n100, n500 = map(int, input().split())
    if p == 0:break
    values = (500, 100, 50, 10, 5, 1)
    values_cnt = (n500, n100, n50, n10, n5, n1)
    INF = 10 ** 20


    def return_cnt(x):
        ret = 0
        for value in values:
            ret += x // value
            x %= value
        return ret


    def make_price(x):
        ret = 0
        for value, cnt in zip(values, values_cnt):
            available_cnt = x // value
            ret += min(available_cnt, cnt)
            x -= value * min(available_cnt, cnt)
        if x == 0:return ret
        return INF


    print(min([make_price(i) + return_cnt(i - p) for i in range(p, p + 500)]))