Problem:
Problem B:
Make Purse Light
Mr. Bill は店で買い物をしている。 彼の財布にはいくらかの硬貨（10 円玉、50 円玉、100 円玉、500 円玉）が入っているが、彼は今この小銭をできるだけ消費しようとしている。 つまり、適切な枚数の硬貨によって品物の代金を払うことで、釣り銭を受け取った後における硬貨の合計枚数を最小にしようとしている。
幸いなことに、この店の店員はとても律儀かつ親切であるため、釣り銭は常に最適な方法で渡される。 したがって、例えば 1 枚の 500 円玉の代わりに 5 枚の 100 円玉が渡されるようなことはない。 また、例えば 10 円玉を 5 枚出して、50 円玉を釣り銭として受け取ることもできる。 ただし、出した硬貨と同じ種類の硬貨が釣り銭として戻ってくるような払いかたをしてはいけない。 例えば、10 円玉を支払いの際に出したにも関わらず、別の 10 円玉が釣り銭として戻されるようでは、完全に意味のないやりとりが発生してしまうからである。
ところが Mr. Bill は計算が苦手なため、実際に何枚の硬貨を使用すればよいかを彼自身で求めることができなかった。 そこで、彼はあなたに助けを求めてきた。 あなたの仕事は、彼の財布の中にある硬貨の枚数と支払い代金をもとに、使用すべき硬貨の種類と枚数を求めるプログラムを書くことである。なお、店員はお釣りに紙幣を使用することはない。
Input
入力にはいくつかのテストケースが含まれる。
それぞれのテストケースは 2 行から構成される。 1 行目には Mr. Bill の支払い金額を円単位で表した 1 つの整数が含まれている。 2 行目には 4 つの整数が含まれており、それらは順に財布の中にある 10 円玉、50 円玉、100 円玉、500 円玉の枚数をそれぞれ表す。
支払い金額は常に 10 円単位である。 すなわち、支払い金額の一円の位は常に 0 である。 また財布の中に、同じ種類の硬貨は最大で 20 枚までしかないと仮定してよい。 支払いが不可能なケースが入力中に与えられることはない。
入力の終了は単一の 0 を含む行によって表される。
Output
それぞれのテストケースについて、Mr. Bill が使用すべき硬貨の種類と枚数を出力せよ。
出力の各行には 2 つの整数
c
i
、
k
i
が含まれる。これは支払いの際に
c
i
円玉を
k
i
枚使用することを意味している。
			複数種類の硬貨を使用する場合は、
c
i
の小さいほうから順に、必要な行数だけ出力を行うこと。 下記の出力例を参考にせよ。
なお、出力には余計なスペースを含めてはならない。 連続するテストケースの間は空行で区切ること。
Sample Input
160
1 1 2 0
160
1 0 2 10
0
Output for the Sample Input
10 1
50 1
100 1

10 1
100 2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    int n;
    int q = 0;
    while(cin >> n, n) {
        if(q != 0) {
            cout << endl;
        }
        q++;
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        int cnt = 1e9;
        int r1 = 0, r2 = 0, r3 = 0, r4 = 0;
        for(int i=0; i<=a; ++i) {
            for(int j=0; j<=b; ++j) {
                for(int k=0; k<=c; ++k) {
                    for(int l=0; l<=d; ++l) {
                        int total = 10 * i + 50 * j + 100 * k + 500 * l;
                        if(total < n) {
                            continue;
                        }
                        int m = total - n;
                        if(m / 500 > 0 && l != 0) {
                            continue;
                        }
                        int t = m / 500;
                        m %= 500;
                        if(m / 100 > 0 && k != 0) {
                            continue;
                        }
                        t += m / 100;
                        m %= 100;
                        if(m / 50 > 0 && j != 0) {
                            continue;
                        }
                        t += m / 50;
                        m %= 50;
                        if(m / 10 > 0 && i != 0) {
                            continue;
                        }
                        t += m / 10;
                        t = t + (a - i) + (b - j) + (c - k) + (d - l);
                        if(t <= cnt) {
                            r1 = i;
                            r2 = j;
                            r3 = k;
                            r4 = l;
                            cnt = t;
                        }
                    }
                }
            }
        }
        if(r1 > 0) {
            cout << 10 << ' ' << r1 << endl;
        }
        if(r2 > 0) {
            cout << 50 << ' ' << r2 << endl;
        }
        if(r3 > 0 ) {
            cout << 100 << ' ' << r3 << endl;
        }
        if(r4 > 0) {
            cout << 500 << ' ' << r4 << endl;
        }
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3352
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

vector<char> isprime;
void init() {
    isprime.assign(1000005, 1);
    isprime[0] = 0;
    isprime[1] = 0;
    for(int i = 2; i < 1000005; i++) {
        if(isprime[i]) {
            for(int j = i * 2; j < 1000005; j += i) {
                isprime[j] = 0;
            }
        }
    }
}

ll n, a, b, c, d;
void input() { cin >> n >> a >> b >> c >> d; }

void solve() {
    auto otsuri = [](ll a) {
        vector<ll> ret(4, 0);
        while(a >= 500) {
            ret[3]++;
            a -= 500;
        }
        while(a >= 100) {
            ret[2]++;
            a -= 100;
        }
        while(a >= 50) {
            ret[1]++;
            a -= 50;
        }
        while(a >= 10) {
            ret[0]++;
            a -= 10;
        }

        return ret;
    };

    vector<ll> best;
    ll ans = 100;

    vector<ll> v(4, 0), dep = {a, b, c, d};
    auto dfs = [&](auto self, int now) {
        if(now == 4) {
            ll sum = v[0]*10+v[1]*50+v[2]*100+v[3]*500;
            if(sum < n)
                return;
            vector<ll> w = otsuri(sum - n);
            ll cnt = 0;
            for(int i = 0; i < 4; i++) {
                if(v[i] && w[i])
                    return;
                cnt += dep[i] - v[i] + w[i];
            }
            if(ans > cnt) {
                best = v;
                ans = cnt;
            }
        } else {
            for(int i = 0; i <= dep[now]; i++) {
                v[now] = i;
                self(self, now + 1);
            }
        }
    };
    dfs(dfs, 0);
    if(best[0])
        cout << "10 " << best[0] << endl;
    if(best[1])
        cout << "50 " << best[1] << endl;
    if(best[2])
        cout << "100 " << best[2] << endl;
    if(best[3])
        cout << "500 " << best[3] << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    init();
    input();
    while(1) {  
        solve();
        input();
        if(n == 0)
            break;
        cout << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 610
memory_kb: 3812
final_rank: 17

code_snippet: // AOJ 2007 - Make Purse Light
// JAG Practice Contest for Japan Domestic 2005 - Problem B

#include <bits/stdc++.h>
using namespace std;

vector<int> c{10, 50, 100, 500};

int main() {
  for(int i=0; ; ++i) {
    int p; cin >> p;
    if(p == 0) break;
    if(i > 0) cout << endl;
    vector<int> h(4);
    for(int i=0; i<4; ++i) cin >> h[i];
    int least = 2e9;
    vector<int> k(4), l(4);
    for(k[0]=0; k[0]<=h[0]; ++k[0]) {
      for(k[1]=0; k[1]<=h[1]; ++k[1]) {
        for(k[2]=0; k[2]<=h[2]; ++k[2]) {
          for(k[3]=0; k[3]<=h[3]; ++k[3]) {
            int q = 0, cur = 0;
            for(int i=0; i<4; ++i) {
              q += c[i] * k[i];
              cur += h[i] - k[i];
            }
            if(q < p) continue;
            q -= p;
            for(int i=3; i>=0; --i) {
              cur += q / c[i];
              q %= c[i];
            }
            if(cur < least) {
              least = cur;
              l = k;
            }
          }
        }
      }
    }
    for(int i=0; i<4; ++i) {
      if(l[i]) cout << c[i] << ' ' << l[i] << endl;
    }
  }
}
accuracy: 1
time_ms: 180
memory_kb: 3456
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__)
#define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name
#define REP1(n) for(ll i=0;i<(n);i++)
#define REP2(i, n) for (ll i=0;i<(n);i++)
#define REP3(i, a, n) for (ll i=a;i<(n);i++)
#define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__)
#define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name
#define RREP1(n) for(ll i=(n)-1;i>=0;i--)
#define RREP2(i, n) for(ll i=(n)-1;i>=0;i--)
#define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--)
#define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b))
#define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__)
#define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a))
#define len(n) (long long)(n).size()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using vvs = vector<vs>;
using vvvs = vector<vvs>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vc = vector<char>;
using vvc = vector<vc>;
using vvvc = vector<vvc>;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

ll intpow(ll a,ll b){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
        }
        a *= a;
        b /= 2;
    }
    return ans;
}
ll modpow(ll a,ll b,ll c){
    ll ans = 1;
    while (b){
        if (b & 1){
            ans *= a;
            ans %= c;
        }
        a *= a;
        a %= c;
        b /= 2;
    }
    return ans;
}

template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHA(...) char __VA_ARGS__; input(__VA_ARGS__)
#define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];}
#define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VI(name,length) vi name(length);rep(i,length){cin >> name[i];}
#define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];}
#define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VC(name,length) vc name(length);rep(i,length){cin >> name[i];}
#define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define VS(name,length) vs name(length);rep(i,length){cin >> name[i];}
#define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];}
#define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];}
#define PLL(name) pll name;cin>>name.first>>name.second;
#define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;}

void print(){cout << "\n";}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
    os << "[";
    for (size_t i = 0; i < vec.size(); ++i) {
        os << vec[i];
        if (i + 1 < vec.size()) os << ", ";
    }
    os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) {
    os << "[";
    for (size_t j = 0; j < a.size(); ++j) {
        os << "(" << a[j].first << ", " << a[j].second << ")";
		if (j + 1 < a.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) {
	os << "[";
    for (size_t i = 0; i < mat.size(); ++i) {
		os << "[";
        for (size_t j = 0; j < mat[i].size(); ++j) {
            os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")";
			if (j + 1 < mat[i].size()) os << ", ";
        }
		os << "]";
		if (i + 1 < mat.size()) os << ", ";
    }
	os << "]";
    return os;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
    os << "{";
    bool first = true;
    for (const auto& x : s) {
        if (!first) os << ", ";
        os << x;
        first = false;
    }
    os << "}";
    return os;
}

template <typename K, typename V>
std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) {
    os << "{";
    bool first = true;
    for (const auto& [key, val] : m) {
        if (!first) os << ", ";
        os << key << ": " << val;
        first = false;
    }
    os << "}";
    return os;
}

template<class T, class... Ts>
void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}

void write(){cout << "\n";}
template<class T, class... Ts>
void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';}
void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}}
void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}
void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}}
void write(pll x){cout << x.first << ' ' << x.second << '\n';}
void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}}
void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}}

template <typename T>
T sum(const std::vector<T>& v) {
    return std::accumulate(v.begin(), v.end(), T(0));
}

template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; }
template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }

long long mod_mul(long long a, long long b, long long mod) {
    return (__int128)a * b % mod;
}

long long mod_pow(long long a, long long b, long long mod) {
    long long result = 1;
    a %= mod;
    while (b > 0) {
        if (b & 1) result = mod_mul(result, a, mod);
        a = mod_mul(a, a, mod);
        b >>= 1;
    }
    return result;
}

bool MillerRabin(ll x){
    
    if(x <= 1){return false;}
    if(x == 2){return true;}
    if((x & 1) == 0){return false;}
    vll test;
    if(x < (1 << 30)){
        test = {2, 7, 61};
    } 
    else{
        test = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
    }
    ll s = 0;
    ll d = x - 1;
    while((d & 1) == 0){
        s++;
        d >>= 1;
    }
    for(ll i:test){
        if(x <= i){return true;}
        ll y = mod_pow(i,d,x);
        if(y == 1 || y == x - 1){continue;}
        ll c = 0;
        for(int j=0;j<s-1;j++){
            y = mod_mul(y,y,x);
            if(y == x - 1){
                c = 1;
                break;
            }
        }
        if(c == 0){
            return false;
        }
    }
    return true;
}

int main(){
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
    ll t = 0;

    while(1){
        LL(x);
        if(x == 0){break;}
        if(t == 1){print();}
        t = 1;
        VLL(a,4);
        ll ans = 1LL<<60;
        vll c;
        rep(i,a[0]+1){
            rep(j,a[1]+1){
                rep(k,a[2]+1){
                    rep(l,a[3]+1){
                        vll b = a;
                        b[0] -= i;
                        b[1] -= j;
                        b[2] -= k;
                        b[3] -= l;
                        ll v = 10 * i + 50 * j + 100 * k + 500 * l;
                        ll u = v - x;
                        if(u < 0){continue;}
                        ll cnt = 0;
                        while(u >= 500){
                            cnt++;
                            u -= 500;
                        }
                        while(u >= 100){
                            cnt++;
                            u -= 100;
                        }
                        while(u >= 50){
                            cnt++;
                            u -= 50;
                        }
                        while(u >= 10){
                            cnt++;
                            u -= 10;
                        }
                        if(ans > sum(b) + cnt){
                            ans = sum(b) + cnt;
                            c = b;
                            //print(b,v);
                        }
                        
                    }
                }
            }
        }
        vll d = {10,50,100,500};
        rep(i,4){
            c[i] = a[i] - c[i];
            if(c[i] >= 1){
                print(d[i],c[i]);
            }
        }

    }
    
}
accuracy: 1
time_ms: 520
memory_kb: 3456
final_rank: 15

code_snippet: #include <iostream> 
using namespace std;

int main(){
    int N,A,B,C,D,a,b,c,d,s,cha,r;
    int t=0;
    while(cin>>N>>A>>B>>C>>D && N>0){
        if(t==1){
            cout<<endl;
        }
        t=1;
        s=81;
        for (int i=0; i<=A; i++){
            for (int j=0; j<=B; j++){
                for (int k=0; k<=C; k++){
                    for (int l=0; l<=D; l++){
                        cha =(10*i)+(50*j)+(100*k)+(500*l)-N;
                        if(cha>=0){
                            r=0;
                            r += cha/500;
                            cha = cha % 500;
                            r += cha/100;
                            cha = cha % 100;

                            r += cha/50;
                            cha = cha % 50;
                            r += cha/10;
                            if(A+B+C+D-i-j-k-l+r < s){
                                s=A+B+C+D-i-j-k-l+r;
                                a=i;
                                b=j;
                                c=k;
                                d=l;
                            }
                        }
                    }
                }
            }
        }
        if(a>0){
            cout<<"10 "<<a<<endl;
        }
        if(b>0){
            cout<<"50 "<<b<<endl;
        }
        if(c>0){
            cout<<"100 "<<c<<endl;
        }
        if(d>0){
            cout<<"500 "<<d<<endl;
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 3356
final_rank: 11

code_snippet: #include <iostream>
#include <vector>

using namespace std;

void solve(int m) {
    vector<int> c(4);
    cin >> c[0] >> c[1] >> c[2] >> c[3];
    const vector<int> v = {10, 50, 100, 500};
    int mn = 10000;
    vector<int> ans(4);
    vector<int> var(4);
    for (var[0] = 0; var[0] <= c[0]; var[0]++) {
        for (var[1] = 0; var[1] <= c[1]; var[1]++) {
            for (var[2] = 0; var[2] <= c[2]; var[2]++) {
                for (var[3] = 0; var[3] <= c[3]; var[3]++) {
                    int p = 0;
                    for (int q = 0; q < 4; q++) p += var[q] * v[q];
                    if (p < m) continue;
                    int o = p - m;
                    vector<int> ret(4);
                    bool ok = true;
                    for (int q = 3; q >= 0; q--) {
                        if (o / v[q] > 0 && var[q] > 0) ok = false;
                        ret[q] = o / v[q] + c[q] - var[q];
                        o %= v[q];
                    }
                    if (ok && mn > ret[0] + ret[1] + ret[2] + ret[3]) {
                        mn = ret[0] + ret[1] + ret[2] + ret[3];
                        for (int q = 0; q < 4; q++) {
                            ans[q] = var[q];
                        }
                    }
                }
            }
        }
    }
    for (int i = 0; i < 4; i++) {
        if (ans[i]) {
            cout << v[i] << " " << ans[i] << endl;
        }
    }
}

int main() {
    int m;
    cin >> m;
    solve(m);
    cin >> m;
    while (m) {
        cout << endl;
        solve(m);
        cin >> m;
    }
}
accuracy: 1
time_ms: 580
memory_kb: 3052
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)
#define RREP(i,n) RFOR(i,0,n)
#define sz(A) (ll)(A.size())
#define ALL(A) A.begin(),A.end()
#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())
#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())
#define COU(A,x) (UB(A,x)-LB(A,x))
#define F first
#define S second
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
const ll mod=998244353;
template<const long long int mod=998244353>
struct modint{
  using mint=modint<mod>;
  long long int x;
  modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}
  long long int val(){return x;}
  mint&operator=(const mint&a){x=a.x;return *this;}
  mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}
  mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}
  mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}
  friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}
  friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}
  friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}
  mint operator-()const{return mint(0)-*this;}
  mint pow(long long int n){
    if(!n)return 1;
    mint a=1;
    mint _x=x;
    while(n){
      if(n&1)a*=_x;
      _x=_x*_x;n>>=1;
    }
    return a;
  }
  mint inv(){return pow(mod-2);}
  mint&operator/=(mint&a){return *this*=a.inv();}
  friend mint operator/(const mint&a,mint b){return mint(a)/=b;}
};
using mint=modint<998244353>;
int main(){
    bool c=0;
    while(1){
        ll N;cin>>N;
        if(!N)return 0;
        if(c)cout<<endl;
        c=1;
        ll a,b,c,d;cin>>a>>b>>c>>d;
        vi ans(4),X={10,50,100,500};
        ll score=1e18;
        auto f=[](ll a){
            ll res=0;
            res+=a/500;a%=500;
            res+=a/100;a%=100;
            res+=a/50;a%=50;
            res+=a/10;
            return res;
        };
        REP(i,a+1)REP(j,b+1)REP(k,c+1)REP(l,d+1)if(10*i+50*j+100*k+500*l>=N){
            ll t=N-10*i-50*j-100*k-500*l;
            if(chmin(score,f(-t)+a-i+b-j+c-k+d-l))ans={i,j,k,l};
        }
        REP(i,4)if(ans[i])cout<<X[i]<<" "<<ans[i]<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3324
final_rank: 10

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

template <unsigned mod = 1000000007> struct fp {
    unsigned v;
    static constexpr int get_mod() {
        return mod;
    }
    constexpr unsigned inv() const {
        assert(v != 0);
        int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;
        while (y > 0) {
            t = x / y;
            x -= t * y, p -= t * q;
            tmp = x, x = y, y = tmp;
            tmp = p, p = q, q = tmp;
        }
        if (p < 0)
            p += mod;
        return p;
    }
    constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
    fp operator-() const {
        return fp() - *this;
    }
    fp pow(ull t) {
        fp res = 1, b = *this;
        while (t) {
            if (t & 1)
                res *= b;
            b *= b;
            t >>= 1;
        }
        return res;
    }
    fp &operator+=(const fp &x) {
        if ((v += x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator-=(const fp &x) {
        if ((v += mod - x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator*=(const fp &x) {
        v = ull(v) * x.v % mod;
        return *this;
    }
    fp &operator/=(const fp &x) {
        v = ull(v) * x.inv() % mod;
        return *this;
    }
    fp operator+(const fp &x) const {
        return fp(*this) += x;
    }
    fp operator-(const fp &x) const {
        return fp(*this) -= x;
    }
    fp operator*(const fp &x) const {
        return fp(*this) *= x;
    }
    fp operator/(const fp &x) const {
        return fp(*this) /= x;
    }
    bool operator==(const fp &x) const {
        return v == x.v;
    }
    bool operator!=(const fp &x) const {
        return v != x.v;
    }
    friend istream &operator>>(istream &is, fp &x) {
        return is >> x.v;
    }
    friend ostream &operator<<(ostream &os, const fp &x) {
        return os << x.v;
    }
};

template <unsigned mod> void rd(fp<mod> &x) {
    fastio::rd(x.v);
}
template <unsigned mod> void wt(fp<mod> x) {
    fastio::wt(x.v);
}

template <typename T> T Inv(ll n) {
    static const int md = T::get_mod();
    static vector<T> buf({0, 1});
    assert(n > 0);
    n %= md;
    while (SZ(buf) <= n) {
        int k = SZ(buf), q = (md + k - 1) / k;
        buf.push_back(buf[k * q - md] * q);
    }
    return buf[n];
}

template <typename T> T Fact(ll n, bool inv = 0) {
    static const int md = T::get_mod();
    static vector<T> buf({1, 1}), ibuf({1, 1});
    assert(n >= 0 and n < md);
    while (SZ(buf) <= n) {
        buf.push_back(buf.back() * SZ(buf));
        ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));
    }
    return inv ? ibuf[n] : buf[n];
}

template <typename T> T nPr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nCr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nHr(int n, int r, bool inv = 0) {
    return nCr<T>(n + r - 1, r, inv);
}

/**
 * @brief Modint
 */

int main() {
for (int _ = 0; ; _++) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    if (_ != 0) cout << endl;
    int X[4] = {10,50,100,500}, Y[4];
    cin >> Y[0] >> Y[1] >> Y[2] >> Y[3];
    int MIN = inf, ANS[4];
    rep(l,0,Y[3]+1) {
        rep(k,0,Y[2]+1) {
            rep(j,0,Y[1]+1) {
                rep(i,0,Y[0]+1) {
                    int COUNT = X[0] * i + X[1] * j + X[2] * k + X[3] * l;
                    if (COUNT >= N) {
                        COUNT -= N;
                        int Z[4];
                        rep(m,0,3) {
                            Z[m] = (COUNT % X[m+1]) / X[m];
                        }
                        Z[3] = COUNT / X[3];
                        if (chmin(MIN,Z[0]+Z[1]+Z[2]+Z[3]+X[0]-i+X[1]-j+X[2]-k+X[3]-l)) {
                            ANS[0] = i, ANS[1] = j, ANS[2] = k, ANS[3] = l;
                        }
                    }
                }
            }
        }
    }
    rep(i,0,4) {
        if (ANS[i] != 0) {
            cout << X[i] << ' ' << ANS[i] << endl;
        }
    }
}
}
accuracy: 1
time_ms: 90
memory_kb: 3184
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
const vector<int> C = {10, 50, 100, 500};
const int INF = 1e9;
int main() {
  int test = 0;
  while (1) {
    int n;
    cin >> n;
    if (n == 0) break;
    if (test) {
      cout << '\n';
    }
    test++;
    vector<int> A(4);
    for (int i = 0; i < 4; i++) {
      cin >> A[i];
    }
    int MIN = INF;
    vector<int> ans(4);
    for (int i = 0; i <= A[0]; i++) {
      for (int j = 0; j <= A[1]; j++) {
        for (int k = 0; k <= A[2]; k++) {
          for (int l = 0; l <= A[3]; l++) {
            int sum = i * 10 + j * 50 + k * 100 + l * 500;
            sum -= n;
            if (sum < 0) continue;
            int cnt = A[0] - i + A[1] - j + A[2] - k + A[3] - l;
            for (int m = 3; m >= 0; m--) {
              cnt += sum / C[m];
              sum -= C[m] * (sum / C[m]);
            }
            if (cnt < MIN) {
              MIN = cnt;
              ans[0] = i;
              ans[1] = j;
              ans[2] = k;
              ans[3] = l;
            }
          }
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      if (!ans[i]) continue;
      cout << C[i] << ' ' << ans[i] << '\n';
    }
  }
}
accuracy: 1
time_ms: 220
memory_kb: 3136
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define eb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<long long>;
using vvl = vector<vector<long long>>; using vs = vector<string>;
using pl = pair<long long, long long>;
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
template<class T>using rp_queue=priority_queue<T,vector<T>,greater<T>>;
template <typename T> T gcd(T a, T b) {if (b == 0)return a; else return gcd(b, a % b);}
template <typename T> inline T lcm(T a, T b) {return a /gcd(a, b)*b;}
const ll INF = 1LL << 60;
const ld PI = acos(-1);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec);
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr);
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const pair<T, U> &pa);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec);
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec);
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa);
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp);
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp);
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl);

template <class OStream, class T> OStream &operator<<(OStream &os, const std::vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T, size_t sz> OStream &operator<<(OStream &os, const std::array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <class... T> std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <class OStream, class... T> OStream &operator<<(OStream &os, const std::tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
template <class OStream, class T, class TH> OStream &operator<<(OStream &os, const std::unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T> OStream &operator<<(OStream &os, const std::unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <class OStream, class T, class U> OStream &operator<<(OStream &os, const std::pair<T, U> &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; }
template <class OStream, class TK, class TV> OStream &operator<<(OStream &os, const std::map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <class OStream, class TK, class TV, class TH> OStream &operator<<(OStream &os, const std::unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl
#define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr)
#else
#define dbg(x) ((void)0)
#define dbgif(cond, x) ((void)0)
#endif
void solve();
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
  int num_tc = 1;
  //cin >> num_tc;
  rep(tc,num_tc){
    //cout << "Case #" << tc+1 << ": " ;// << endl;
    solve();
  }
}
//見切り発車で実装しては行けない．頭の中でコードが完全に出来上がるまで書き始めるな．
//オーバーフローしているか確認する
//小さいケースで動いているかを確認する。
vl coins = {10,50,100,500};
void solve(){
  vector<vl> answer;
  while(true){
    ll X;cin>>X;
    if(X==0)break;
    ll a,b,c,d;cin>>a>>b>>c>>d;
    vl out = {a,b,c,d};
    vl F = {a,b,c,d};
    ll ans = INF;
    rep(A,a+1)rep(B,b+1)rep(C,c+1)rep(D,d+1){
      //それぞれA,B,C,D使った時の値
      ll now = 10*A+50*B+100*C+500*D;
      ll diff =  now- X;
      if(diff < 0)continue;
      vl num(4); 
      num[0] = a-A;
      num[1] = b-B;
      num[2] = c-C;
      num[3] = d-D;
      ll sum = 0;
      rep(k,4)sum+=num[k];
      for(int d=3;d>=0;d--){
        sum+=diff/coins[d];
        num[d]+=diff/coins[d];
        diff%=coins[d];
      }
      if(chmin(ans,sum))out={A,B,C,D};
    }
    answer.push_back(out);
  }
  const int n  = answer.size();
  rep(i,n){
    rep(d,4){
      if(answer[i][d]==0)continue;
      cout<<coins[d]<<" "<<answer[i][d]<<endl;
    }
    if(i!=n-1)cout<<endl;
  }
}
accuracy: 1
time_ms: 1340
memory_kb: 3540
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) a.begin(),a.end()
#define reps(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, a, n) for (int i = (a); i > (int)(n); i--)
const long long mod = 1000000007;
const int inf = 1e9;
const long long INF = 1e18;
ll myceil(ll a, ll b) {return (a+b-1)/b;}

int n;

void solve() {
  int a,b,c,d;
  int aq,bq,cq,dq;
  cin >> a >> b >> c >> d;
  ll ans = INF;
  ll pay = INF;
  rep(aa,a+1) {
    rep(bb,b+1) {
      rep(cc,c+1) {
        rep(dd,d+1) {
          int sm = aa*10+bb*50+cc*100+dd*500;
          if (sm < n) {
            continue;
          }
          int now = 0;
          int change = sm-n;
          while (change >= 500) {
            change -= 500;
            now++;
          }
          while (change >= 100) {
            change -= 100;
            now++;
          }
          while (change >= 50) {
            change -= 50;
            now++;
          }
          now += change/10;
          now += a+b+c+d-aa-bb-cc-dd;
          if (ans > now) {
            ans = now;
            pay = aa+bb+cc+dd;
            aq=aa;bq=bb;cq=cc;dq=dd;
          }
          else if (ans == now) {
            if (pay > aa+bb+cc+dd) {
              pay = aa+bb+cc+dd;
              aq=aa;bq=bb;cq=cc;dq=dd;
            }
          }
        }
      }
    }
  }
  if (aq) {
    cout << 10 << ' ' << aq << endl;
  }
  if (bq) {
    cout << 50 << ' ' << bq << endl;
  }
  if (cq) {
    cout << 100 << ' ' << cq << endl;
  }
  if (dq) {
    cout << 500 << ' ' << dq << endl;
  }
  return;
}

int main() {
    bool q = false;
  while (true) {
    cin >> n;
    if (n == 0) {
      return 0;
    }
    if (q) {
        cout << endl;
    }
    solve();
    q = true;
  }
}
accuracy: 1
time_ms: 150
memory_kb: 3356
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>>ans;
bool solve()
{
    auto calc=[](int x)->tuple<int,int,int,int>
    {
        int a=x/500;
        x%=500;
        int b=x/100;
        x%=100;
        int c=x/50;
        x%=50;
        int d=x/10;
        x%=10;
        assert(x==0);
        return make_tuple(d,c,b,a);
    };
    int X;
    cin>>X;
    if(X==0)return 0;
    int A,B,C,D;
    cin>>A>>B>>C>>D;
    int MIN=1<<30;
    for(int i=0;i<=A;i++)for(int j=0;j<=B;j++)for(int k=0;k<=C;k++)for(int l=0;l<=D;l++)
    {
        int pay=10*i+50*j+100*k+500*l;
        if(pay<X)continue;
        auto[a,b,c,d]=calc(pay-X);
        if(i&&a)continue;
        if(j&&b)continue;
        if(k&&c)continue;
        if(l&&d)continue;
        MIN=min(MIN,a+b+c+d-i-j-k-l);
    }
    for(int i=0;i<=A;i++)for(int j=0;j<=B;j++)for(int k=0;k<=C;k++)for(int l=0;l<=D;l++)
    {
        int pay=10*i+50*j+100*k+500*l;
        if(pay<X)continue;
        auto[a,b,c,d]=calc(pay-X);
        if(i&&a)continue;
        if(j&&b)continue;
        if(k&&c)continue;
        if(l&&d)continue;
        if(a+b+c+d-i-j-k-l==MIN)
        {
            vector<pair<int,int>>res;
            if(i)res.push_back(make_pair(10,i));
            if(j)res.push_back(make_pair(50,j));
            if(k)res.push_back(make_pair(100,k));
            if(l)res.push_back(make_pair(500,l));
            ans.push_back(res);
            return 1;
        }
    }
    return 1;
}
int main()
{
    while(solve()){}
    for(int i=0;i<ans.size();i++)
    {
        if(i)cout<<'\n';
        for(auto[v,c]:ans[i])cout<<v<<' '<<c<<'\n';
    }
}
accuracy: 1
time_ms: 160
memory_kb: 3456
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,k,n) for (int i = k; i < (n); ++i)
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
typedef pair<ll, ll> P;
const long long INF = 1e17;

int main(){
    bool f = false;
    while(true){
        int sum,c[4],p[4],r[4];
        int d[4] = {10,50,100,500};
        cin >> sum;
        if(sum==0)break;
        if(f)cout << endl;
        f = true;
        for(int i=0;i<4;i++)cin >> c[i];

        int ans = 1e9;
        int ans_p[4];

        for(p[0]=0;p[0]<=c[0];p[0]++){
            for(p[1]=0;p[1]<=c[1];p[1]++){
                for(p[2]=0;p[2]<=c[2];p[2]++){
                    for(p[3]=0;p[3]<=c[3];p[3]++){
                        int total = 0;
                        for(int i=0;i<4;i++){
                            total += p[i]*d[i];
                        }

                        if(total<sum)continue;

                        int res = total - sum;
                        for(int i=3;i>=0;i--){
                            r[i] = res/d[i];
                            res %= d[i];
                        }

                        int have = 0;
                        for(int i=3;i>=0;i--){
                            have += c[i] - p[i] + r[i]; 
                        }

                        if(ans>have){
                            ans = have;
                            for(int i=0;i<4;i++){
                                ans_p[i] = p[i];
                            }
                        }


                    }
                }
            }

        }

        for(int i=0;i<4;i++){
            if(ans_p[i]>0)cout << d[i] << ' ' << ans_p[i] << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3072
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)

template<class T> bool chmin(T &a, T b)
{
	if(a>b)
	{
		a = b;
		return true;
	}
	return false;
}

int N;

bool solve()
{
	if(N==0) return false;
	vector<int> A(4);
	rep(i,4) cin >> A[i];
	
	int v = 10000;
	vector<int> ans(4);
	rep(a,A[0]+1) rep(b,A[1]+1) rep(c,A[2]+1) rep(d,A[3]+1)
	{
		vector<int> C = {a,b,c,d};
		int r = a*10 + b*50 + c*100 + d*500 - N;
		if(r<0) continue;
		vector<int> B(4);
		B[3] = r/500;
		r %= 500;
		B[2] = r/100;
		r %= 100;
		B[1] = r/50;
		r %= 50;
		B[0] = r/10;
		assert(r%10==0);
		rep(i,4) if(C[i]>0 && B[i]>0) continue;
		int cnt = 0;
		rep(i,4) cnt += A[i]-C[i]+B[i];
		if(chmin(v, cnt)) ans = C;
	}
	vector<int> D = {10,50,100,500};
	rep(i,4) if(ans[i]!=0) cout << D[i] << " " << ans[i] << '\n';
	cin >> N;
	if(N==0) return false;
	else
	{
		cout << '\n';
		return true;
	}
}

int main()
{
	cin >> N;
	while(solve());
}
accuracy: 1
time_ms: 700
memory_kb: 3388
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;
using vll = vector<ll>;
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<n;i++)

int main(){
    bool a=true;
    while(1){
        
        ll X;cin>>X;
        if(X==0) break;
        if(!a)cout<<endl;
        a=false;
        ll N=4;
        vll A(4);rep(i,N) cin>>A[i];
        
        vll ans(N);
        ll sum=-(1ll<<60);
        rep(i,A[0]+1) rep(j,A[1]+1) rep(k,A[2]+1) rep(l,A[3]+1){
            ll sum2=i*10+j*50+k*100+l*500;
            if(sum2<X) continue;
            ll rest=sum2-X;
            ll i2,j2,k2,l2;
            l2=rest/500;rest-=rest/500*500;
            k2=rest/100;rest-=rest/100*100;
            j2=rest/50;rest-=rest/50*50;
            i2=rest/10;rest-=rest/10*10;
            if((i2&&i)||(j2&&j)||(k2&&k)||(l2&&l)) continue;
            if(rest>0) continue;
            if(sum<(i-i2)+(j-j2)+(k-k2)+(l-l2)){
                ans={i,j,k,l};
                sum=(i-i2)+(j-j2)+(k-k2)+(l-l2);
            }

        }
        if(ans[0]>0) cout<<10<<" "<<ans[0]<<endl;
        if(ans[1]>0) cout<<50<<" "<<ans[1]<<endl;
        if(ans[2]>0) cout<<100<<" "<<ans[2]<<endl;
        if(ans[3]>0) cout<<500<<" "<<ans[3]<<endl;
        
        
    }
}
accuracy: 1
time_ms: 90
memory_kb: 3060
final_rank: 3

code_snippet: // (⁠◕⁠ᴗ⁠◕⁠✿⁠)

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define srep(i, s, n) for (ll i = s; i < (n); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using vvv = vv<vc<T>>;
using vi = vc<int>;using vvi = vv<int>; using vvvi = vv<vi>;
using ll = long long;using vl = vc<ll>;using vvl = vv<ll>; using vvvl = vv<vl>;
using ld = long double; using vld = vc<ld>; using vvld = vc<vld>; using vvvld = vc<vvld>;
using uint = unsigned int;
using ull = unsigned long long;
const ld pi = 3.141592653589793;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
// const ll mod = 1000000007;
const ll mod = 998244353;
inline bool inside(ll y, ll x, ll H, ll W) {return 0 <= (y) and (y) < (H) and 0 <= (x) and (x) < (W); }

#define debug(var)  do{std::cout << #var << " : \n";view(var);}while(0)
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vc<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vv<T>& vv){ for(const auto& v : vv){ view(v); } }

vc<pair<int, int>> solve(int N){
    vi coin(4); rep(i, 4) cin >> coin[i];
    vc<pair<int, int>> ans;
    int mini = inf, sm = 0; for (auto x : coin) sm += x;
    rep(a, coin[0] + 1) rep(b, coin[1] + 1) rep(c, coin[2] + 1) rep(d, coin[3] + 1){
        int tmp = sm - a - b - c - d;
        vc<pair<int, int>> cand;
        if (a > 0) cand.push_back({10, a});
        if (b > 0) cand.push_back({50, b});
        if (c > 0) cand.push_back({100, c});
        if (d > 0) cand.push_back({500, d});
        int val = a * 10 + b * 50 + c * 100 + d * 500;
        if (val < N) continue;
        while (val >= N + 500){
            val -= 500;
            tmp++;
        }
        while (val >= N + 100){
            val -= 100;
            tmp++;
        }
        while (val >= N + 50){
            val -= 50;
            tmp++;
        }
        while (val >= N + 10){
            val -= 10;
            tmp++;
        }
        if (tmp < mini){
            mini = tmp;
            ans = cand;
        }
    }
    return ans;
}

int main(){
    vc<vc<pair<int, int>>> ans;
    while (true){
        int N; cin >> N;
        if (N == 0) break;
        ans.push_back(solve(N));
    }
    rep(i, len(ans)){
        for (auto [a, b] : ans[i]) cout << a << " " << b << endl;
        if (i < len(ans) - 1) cout << endl;
    }
    // for (auto x : ans){
    //     for (auto [a, b] : x) cout << a << " " << b << endl;
        
    // }
}
accuracy: 1
time_ms: 2220
memory_kb: 3348
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

int count_change(int x, int &t10, int &t50, int &t100, int &t500) {
    t500 = x/500;
    x -= (x/500)*500;
    t100 = x/100;
    x -= (x/100)*100;
    t50 = x/50;
    x -= (x/50)*50;
    t10 = x/10;
    x -= (x/10)*10;

    return t10 + t50 + t100 + t500;
}

int main() {
    int cnt = 0;
    while (true) {
        int x;
        cin >> x;

        if (x == 0) {
            break;
        }

        int t10, t50, t100, t500;
        cin >> t10 >> t50 >> t100 >> t500;

        int y10, y50, y100, y500;
        y10 = y50 = y100 = y500 = 0;
        int memo = INT_MAX;
        for (int i = 0; i <= t10; i++) {
            for (int j = 0; j <= t50; j++) {
                for (int k = 0; k <= t100; k++) {
                    for (int l = 0; l <= t500; l++) {
                        int total = i*10+j*50+k*100+l*500;
                        if (total < x) {
                            continue;
                        }
                        int c10, c50, c100, c500;
                        int cnt = count_change(total-x, c10, c50, c100, c500) + (t10-i) + (t50-j) + (t100-k) + (t500-l);
                        if ((i && c10) || (j && c50) || (k && c100) || (l && c500)) {
                            continue;
                        }
                        if (cnt < memo) {
                            memo = cnt;
                            y10 = i;
                            y50 = j;
                            y100 = k;
                            y500 = l;
                        }
                    }
                }
            }
        }

        if (cnt) {
            cout << endl;
        }
        if (y10) {
            cout << 10 << " " << y10 << endl;
        }
        if (y50) {
            cout << 50 << " " << y50 << endl;
        }
        if (y100) {
            cout << 100 << " " << y100 << endl;
        }
        if (y500) {
            cout << 500 << " " << y500 << endl;
        }
        cnt++;
    }

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3052
final_rank: 2

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "a.cpp"

vector<int> charge(int x) {
    vector<int> res;
    vector<int> p = {500, 100, 50, 10};
    rep (i, 4) {
        res.emplace_back(x / p[i]);
        x -= res.back() * p[i];
    }
    reverse(all(res));
    return res;
}

int main(void) {
    int loop = 0;
    while (true) {
        int x;
        cin >> x;
        if (!x)
            break;
        if (loop++)
            cout << endl;
        vector<int> a(4);
        cin >> a;
        vector<int> ans(4);
        int eval = -Inf;
        vector<int> p = {10, 50, 100, 500};
        rep (i, a[0] + 1) {
            rep (j, a[1] + 1) {
                rep (k, a[2] + 1) {
                    rep (l, a[3] + 1) {
                        vector<int> b = {i, j, k, l};
                        int sum = 0;
                        rep (m, 4) sum += b[m] * p[m];
                        if (x > sum)
                            continue;
                        auto c = charge(sum - x);
                        bool f = true;
                        rep (m, 4) f &= b[m] == 0 || c[m] == 0;
                        int s = accumulate(all(b), 0), t = accumulate(all(c), 0);
                        if (chmax(eval, s - t))
                            ans = b;
                    }
                }
            }
        }
        rep (i, 4) {
            if (ans[i])
                co(p[i], ans[i]);
        }
    }

    return 0;
}
accuracy: 1
time_ms: 1980
memory_kb: 3384
final_rank: 19

code_snippet: #include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  while (N != 0) {
    int c[4], f[4] = {0, 0, 0, 0}, ans[4] = {0, 0, 0, 0};
    int sum = 0;
    for (int i = 0; i < 4; i++) {
      cin >> c[i];
      sum += c[i];
    }
    int mini = 1e+9;
    for (int pay0 = 0; pay0 <= c[0]; pay0++) {
      f[0] = pay0 > 0 ? 1 : 0;
      int m_pay0 = 10 * pay0;
      for (int pay1 = 0; pay1 <= c[1]; pay1++) {
        f[1] = pay1 > 0 ? 1 : 0;
        int m_pay1 = m_pay0 + 50 * pay1;
        for (int pay2 = 0; pay2 <= c[2]; pay2++) {
          f[2] = pay2 > 0 ? 1 : 0;
          int m_pay2 = m_pay1 + 100 * pay2;
          for (int pay3 = 0; pay3 <= c[3]; pay3++) {
            f[3] = pay3 > 0 ? 1 : 0;
            int m_pay3 = m_pay2 + 500 * pay3;
            int n_pay = pay0 + pay1 + pay2 + pay3;
            if (m_pay3 >= N) {
              int ch[4] = {0, 0, 0, 0};
              int rem = m_pay3 - N;
              for (int i = 3; i >= 0; i--) {
                int coin_val = i == 0 ? 10 : i == 1 ? 50 : i == 2 ? 100 : 500;
                ch[i] = rem / coin_val;
                rem = rem % coin_val;
              }
              int n_charge = ch[0] + ch[1] + ch[2] + ch[3];
              if (mini > sum - n_pay + n_charge) {
                if (!(f[0] == 1 && ch[0] > 0) && !(f[1] == 1 && ch[1] > 0) && !(f[2] == 1 && ch[2] > 0) && !(f[3] == 1 && ch[3] > 0)) {
                  ans[0] = pay0; ans[1] = pay1; ans[2] = pay2; ans[3] = pay3;
                  mini = sum - n_pay + n_charge;
                }
              }
            }
          }
        }
      }
    }
    int coin_vals[4] = {10, 50, 100, 500};
    for (int i = 0; i < 4; i++) {
      if (ans[i] > 0) {
        cout << coin_vals[i] << " " << ans[i] << endl;
      }
    }
    cin >> N;
    if (N != 0) {
      cout << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3084
final_rank: 4

code_snippet: #include <cstdio>
#define MAX 4
#define INF 1e+9
#define pii pair<int,int>
#define mp make_pair

int payment(int *p) {
  return 10 * p[0] + 50 * p[1] + 100 * p[2] + 500 * p[3];
}

void charge(int m, int *ans) {
  ans[3] = m / 500;
  m -= ans[3] * 500;
  ans[2] = m / 100;
  m -= ans[2] * 100;
  ans[1] = m / 50;
  m -= ans[1] * 50;
  ans[0] = m / 10;
}

bool judge(int *f, int *ch) {
  for (int i = 0; i < MAX; ++i)
    if (f[i] == 1 && ch[i] > 0)
      return false;
  return true;
}

int main() {
  int N;
  scanf("%d", &N);
  while (1) {
    int c[MAX], sum = 0, f[MAX] = {0, 0, 0, 0}, mini = INF, pay[MAX], ans[MAX];
    for (int i = 0; i < MAX; ++i) {
      scanf("%d", &c[i]);
      sum += c[i];
    }
    for (pay[0] = 0; pay[0] <= c[0]; ++pay[0]) {
      if (pay[0] > 0)
        f[0] = 1;
      for (pay[1] = 0; pay[1] <= c[1]; ++pay[1]) {
        if (pay[1] > 0)
          f[1] = 1;
        for (pay[2] = 0; pay[2] <= c[2]; ++pay[2]) {
          if (pay[2] > 0)
            f[2] = 1;
          for (pay[3] = 0; pay[3] <= c[3]; ++pay[3]) {
            if (pay[3] > 0)
              f[3] = 1;
            int m_pay = payment(pay);
            if (m_pay >= N) {
              int ch[MAX];
              charge(m_pay - N, ch);
              if (mini > sum - pay[0] - pay[1] - pay[2] - pay[3] + ch[0] + ch[1] + ch[2] + ch[3]) {
                if (judge(f, ch)) {
                  for (int i = 0; i < MAX; ++i)
                    ans[i] = pay[i];
                  mini = sum - pay[0] - pay[1] - pay[2] - pay[3] + ch[0] + ch[1] + ch[2] + ch[3];
                }
              }
            }
            f[3] = 0;
          }
          f[2] = 0;
        }
        f[1] = 0;
      }
      f[0] = 0;
    }
    int deno[] = {10, 50, 100, 500};
    for (int i = 0; i < MAX; ++i) {
      if (ans[i] > 0)
        printf("%d %d\n", deno[i], ans[i]);
    }
    scanf("%d", &N);
    if (!N)
      break;
    printf("\n");
  }
}
accuracy: 1
time_ms: 140
memory_kb: 2596
final_rank: 1