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

Submitted solutions:

code_snippet: /*
 * Author: NomadThanatos
 * Created Time:  2011/8/31 13:51:49
 * File Name: J.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;

#define out(v) cerr << #v << ": " << (v) << endl
#define SZ(v) ((int)(v).size())

const int MAXINT = -1u>>1;
const int base[] = {1,5,10,50,100,500};

int num[6];

int calcGive(int mo) {
    int res = 0;
    for(int i = 5 ; i >= 0 ; i--) {
        int toGive = mo / base[i];
        if (toGive > num[i]) {
            toGive = num[i];
        }
        res += toGive;
        mo -= toGive * base[i];
    }
    if (mo) return -1;
    return res;
}

int calcReturn(int mo) {
    int res = 0;
    for(int i = 5 ; i >= 0 ; i--) {
        res += mo / base[i];
        mo %= base[i];
    }
    return res;
}

int main() {
    int sum,P;
    while(scanf("%d",&P) == 1) {
        sum = 0;
        for(int i = 0 ; i < 6 ; i++) {
            scanf("%d",num + i);
            sum += num[i] * base[i];
        }
        if (P == 0) break;
        int minm = MAXINT;
        //printf("%d %d",P,sum);
        for(int p = P ; p < sum + 1 ; p++) {
            int give = calcGive(p);
            int ret = calcReturn(p - P);
            if (give != -1) {
                minm = min(minm,give + ret);
            }
        }
        printf("%d\n",minm);
    }
    return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 3504
final_rank: 12

code_snippet: #include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<iostream>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<climits>
#include<complex>
#define sz(x) (int)((x).size())
#define rep(i,n) for (int i=0;i<n;i++)
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define clr(x) memset((x),0,sizeof(x))
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
int a[111111],b[111111];
int n;
int c[10];
int dp[800000];
int v[6]={1,5,10,50,100,500};
int greedy(int x)
{
	int s=0;
	int i;
	for (i=5;i>=0;i--)
	{
		while (x>=v[i])
		{
			s+=x/v[i];
			x-=(x/v[i])*v[i];
		}
	}
	return s;
}
int main()
{
	int i,j,k,p;
	while (scanf("%d",&p)!=EOF)
	{
		for (i=0;i<6;i++)scanf("%d",c+i);
		if (p==0)break;
		n=0;
		for (i=0;i<6;i++)
		{
			int t=c[i],x=1,ac=1;
			if (!t)continue;
			while (1)
			{
				a[n]=x;
				b[n++]=x*v[i];
				if (ac==t)break;
				x<<=1;
				ac+=x;
				if (ac>t)
				{
					ac-=x;
					a[n]=t-ac;
					b[n++]=(t-ac)*v[i];
					break;
				}
			}
		}
		/*
		printf("%d\n",n);
		for (i=0;i<n;i++)
		{
			printf("%d %d\n",a[i],b[i]);
		}
		*/
		int l=min(799999,p*2);
		for (i=0;i<=l;i++)dp[i]=1<<29;
		dp[0]=0;
		for (i=0;i<n;i++)
		{
			for (j=l;j>=b[i];j--)
			{
				dp[j]=min(dp[j],dp[j-b[i]]+a[i]);
			}
		}
		int ans=1<<29;
		for (i=p;i<=l;i++)
		{
			ans=min(ans,dp[i]+greedy(i-p));
		}
		printf("%d\n",ans);
	}
	return 0;
}
accuracy: 1
time_ms: 920
memory_kb: 6688
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;

const ll T = 10000;
ll C[6] = {1, 5, 10, 50, 100, 500};
ll P;
ll N[6];
ll dp[2][T];

ll calc(ll v) {
  ll ans = 0;
  for (ll i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  while (cin >> P) {
    for(ll i = 0; i < 6; i++) 
      cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    ll cnt = 0;
    for (ll i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    for(ll i = 0; i < 6; i++) {
      for(ll j = 0; j < C[i]; j++) {
        queue<pll> deq;
        for (ll k = j; k < T; k += C[i]) {
          ll w = T - k / C[i];
          dp[(i + 1) & 1][k % T] = min(dp[(i + 1) & 1][k % T], dp[i & 1][k % T]);
          while (!deq.empty() && deq.front().second + C[i] * N[i] < k)
            deq.pop();
          if (!deq.empty())
            dp[(i + 1) & 1][k % T] = min(dp[(i + 1) & 1][k % T], deq.front().first - w);
          if (!deq.empty() && deq.front().second + N[i] * C[i] <= k)
            deq.pop();
          deq.push(pll(dp[i & 1][k % T] + w, k));
        }
      }
    }
    ll mi = 1001001001;
    for (ll i = P; i < T; i++) {
      mi = min(mi, dp[0][i % T] + calc(i - P));
    }
    cout << mi + cnt << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3264
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 5000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= T / 4 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3148
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3264
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3316
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3268
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3392
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3192
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 5000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3232
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using vpint = vector<pint>;

const int MAX_C = 6;
const int INF = 1001001001;
const int MAX_T = 10000;
const int MAX_N = 10000;

int C[MAX_C] = {1, 5, 10, 50, 100, 500};
int P;
int N[MAX_C];
int dp[2][MAX_T];

int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  while (cin >> P) {
    for (int i = 0; i < MAX_C; i++) {
      cin >> N[i];
    }
    if (P == 0) {
      break;
    }

    fill_n(*dp, 2 * MAX_T, INF);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i] > 0) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i] > 0) {
        break;
      }
    }
    dp[0][0] = 0;
    for (int i = 0; i < MAX_C; i++) {
      int cur = i & 1, nxt = (i + 1) & 1;
      for (int j = 0; j < C[i]; j++) {
        deque<pint> deq;
        for (int k = j; k < MAX_T; k += C[i]) {
          int w = MAX_T - k / C[i];
          dp[nxt][k] = min(dp[nxt][k], dp[cur][k]);
          while (!deq.empty() && deq.front().second + C[i] * N[i] < k) {
            deq.pop_front();
          }
          if (!deq.empty()) {
            dp[nxt][k] = min(dp[nxt][k], deq.front().first - w);
          }
          if (!deq.empty() && deq.front().second + N[i] * C[i] <= k) {
            deq.pop_front();
          }
          deq.emplace_back(dp[cur][k] + w, k);
        }
      }
    }
    int mi = INF;
    for (int i = P; i < MAX_T; i++) {
      mi = min(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << '\n';
  }

  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3232
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  while (cin >> P && P) {
    rep(i, 6) cin >> N[i];
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3312
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];
const int T = 10000;
int dp[2][T];
int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;
    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }
    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3640
final_rank: 11