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: Accepted)

#include<iostream>
#include<cassert>
#include<ctime>
#include<climits>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include<list>
#include<stack>
#include<sstream>
#include<string>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(),n.end()
#define pb push_back
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define INF (1<<27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<P,P> PP;
int C[6] = {1,5,10,50,100,500};
int Mine[667000],Mine2[667000];
int RR[7],Ps;
int enu[7];
 
 
int getChangeBy2(int sum){// tenin
    int ret = 0;
     
   
     
    assert(sum >= 0);
    for(int i=5;i>=0;i--){
      if(sum >= C[i]){
    ret += sum/C[i];
    sum -= sum/C[i]*C[i];  
      }
    }
    return ret;
  }
  
  
  int getChange2(int sum){
  
    rep(i,6)RR[i] = 0;
    int ret = 0;
    for(int i=5;i>=0;i--){
      if(sum >= C[i] && enu[i] != 0){
    ret += min(sum/C[i],enu[i]);
    RR[i] = min(sum/C[i],enu[i]);
    sum -= min(sum/C[i]*C[i],enu[i]*C[i]);
  
      }
    }
    return ret;
  }
 
void CreateMines(){
  rep(i,667000)Mine[i] = Mine2[i] = 0;
 
  for(int i=1;i<=66500;i++){
    
    
    int B = getChangeBy2(i);
    Mine2[i] = B;
  }
 
}
 
class AOJ1037{
private:
  int P,total;
  int *N;
  int *R;
  //int *Mines;
  //int *Mine2;
public:
  AOJ1037(int P,int *lis):P(P){
    N = new int[7];
    R = new int[7];
    //Mines = new int[667000];
    // Mine2 = new int[667000];
    total = 0;
    rep(i,6)*(N+i) = *(lis+i);
    rep(i,6)*(R+i) = 0;
    //rep(i,667000)Mines[i] = Mine2[i] = -1;
  }
  
  ~AOJ1037(){
    delete [] N;
    delete [] R;
    //delete [] Mines;
  }
  
  int getChangeBy(int sum){// tenin
    int ret = 0;
     
    sum -= P;
     
    assert(sum >= 0);
    for(int i=5;i>=0;i--){
      if(sum >= C[i]){
    ret += sum/C[i];
    sum -= sum/C[i]*C[i];  
      }
    }
    return ret;
  }
  
  
  int getChange(int sum){
  
    rep(i,6)*(R+i) = 0;
    int ret = 0;
    for(int i=5;i>=0;i--){
      if(sum >= C[i] && N[i] != 0){
    ret += min(sum/C[i],N[i]);
    *(R+i) = min(sum/C[i],N[i]);
    sum -= min(sum/C[i]*C[i],N[i]*C[i]);
  
      }
    }
    return ret;
  }
  
  
  
  
  
  
  
  void run(){
    int limit = 0;
    int MINE = INT_MAX;
    rep(i,6)limit += C[i]*N[i];
   
  
    for(int i = P;i<=limit;i++){
      int bb;
      bb = getChange(i);
  
      if(bb >= MINE)continue;
      int pay = 0;
      rep(j,6)pay += (*(R+j))*C[j];
      //cout << "pay = "<< pay << endl;
      int A;
       A = Mine2[pay-P];
      //if(i == 1000)cout << "A= "<< A << ", B = "<< B << endl;
      if(MINE > A+bb){
    MINE = min(MINE,A+bb);
  
    if(MINE == 1)break;
      }    
    }
    cout << MINE << endl;
    
  } 
  
};
  
  
  
int main(){
  CreateMines();
  while(true){
    int p;
    cin >> p;
if(p == 0)break;
    int sim = p;
    Ps = p;
    rep(i,6){
      cin >> enu[i];
      sim += enu[i];
    }
    
    
    AOJ1037 ans = AOJ1037(p,enu);
    ans.run();
  }
return 0;
}