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 N1 N5 N10 N50 N100 N500

Ni 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 ≤ Ni ≤ 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 in C (judge verdict: Time Limit Exceeded)

/*
  AOJ 1028
  Title:ICPC: Ideal Coin Payment and Change
  @kankichi573
  14/02/21 TLE
*/
#include <stdio.h>

#define min(x,y) (((x)<(y))?(x):(y))

int p,n1,n5,n10,n50,n100,n500;

int change_cnt(int change)
{
  int ret;

  ret = change/100;
  change %= 100;
  ret += change>50?1:0;
  change %= 50;
  ret += change/10;
  change %= 10;
  ret += change>5?1:0;
  change %= 5;
  ret += change;
  return(ret);
}
int ceiling(int n,int m)
{
  if(0 == n % m)
    return(n/m);
  else
    return(n/m + 1);
}
solve()
{
  int i,j,k,l,m,n;
  int l1,l5,l10,l50,l100,l500;
  int min_ = 999;
  int cnt,harai[6],yoryo[6];


  yoryo[4]=n1;
  yoryo[3]=n1+n5*5;
  yoryo[2]=yoryo[3]+n10*10;
  yoryo[1]=yoryo[2]+n50*50;
  yoryo[0]=yoryo[1]+n100*100;
  l500=ceiling(p,500);
  //printf("l500=%d n500=%d min=%d\n",l500,n500,min(n500,l500));

  for(n=min(n500,l500);n>=0;n--)
    {
      harai[0]=500*n;
      if(harai[0]+yoryo[0]<p)
	break;
      for(m=min(n100,10);m>=0;m--)
	{
	  harai[1]=harai[0]+100*m;
	  if(harai[0]+yoryo[0]<p)
	    break;
	  for(l=min(n50,20);l>=0;l--)
	    {
	      harai[2]=harai[1]+50*l;
	      if(harai[2]+yoryo[2]<p)
		break;
	      for(k=min(n10,100);k>=0;k--)
		{
		  harai[3]=harai[2]+10*k;
		  if(harai[3]+yoryo[3]<p)
		    break;
		  for(j=min(n5,200);j>=0;j--)
		    {
		      harai[4]=harai[3]+5*j;
		      if(harai[4]+yoryo[4] < p)
		      	break;
		      for(i=min(n1,1000);i>=0;i--)
			{
			  harai[5]=harai[4]+i;
			  //printf("harai=%d\n",harai);		    
			  if(harai[5] < p)
			    break;
			  cnt=i+j+k+l+m+n+change_cnt(harai[5]-p);
			  if(cnt<min_)
			    {
			      min_=cnt;
#ifdef DEBUG			      
			      printf("harai=%d cnt=%d change=%d cnt=%d\n",
			          harai,cnt,harai-p,change_cnt(harai-p)); 
#endif
			    }
			}
		    }
		}
	    }
	}
    }
  return(min_);
}


main()
{
  while(scanf("%d %d %d %d %d %d %d",&p,&n1,&n5,&n10,&n50,&n100,&n500)
	&& (p || n1 || n5 || n10 || n50 || n100 || n500 ))
    {
      printf("%d\n",solve());
    }

  return(0);
}