#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

#define let auto
#define fn auto
using namespace std;

typedef size_t usize;
typedef ssize_t isize;
typedef int32_t i32;
typedef u_int32_t u32;
typedef int64_t i64;
typedef u_int64_t u64;

fn fastio() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

struct State {
    usize curr;
    usize w;
    vector<u64> pays;
};

fn main() -> int {
    fastio();
    const usize N = 6;
    let coins = vector<u64>{500, 100, 50, 10, 5, 1};
    let counts = vector<u64>(N);

    usize w;
    cin >> w;

    for (let &v: counts) {
        cin >> v;
    }

    let stack = vector<State>();
    let maxpays = vector<u64>(N, 0);
    let maxtotal = (u64) 0;
    stack.push_back({N - 1, w, vector<u64>(N, 0)});
    while (!stack.empty()) {
        let now = stack.back();
        stack.pop_back();

        let curr = now.curr;
        if (curr == 0) {
            if (now.w % coins[curr] != 0) {
                continue;
            } else {
                now.pays[curr] = now.w / coins[curr];
                u64 total = accumulate(now.pays.begin(), now.pays.end(), 0);
                if (total > maxtotal) {
                    maxpays = now.pays;
                    maxtotal = total;
                }
            }

        } else {
            let rem = now.w % coins[curr - 1] / coins[curr];
            let delta = coins[curr - 1] / coins[curr];
            if (counts[curr] < rem) {
                continue;
            }
            let maxto = (counts[curr] - rem) / delta;

            for (usize i = 0; i <= maxto; i++) {
                let cst = (i * delta + rem) * coins[curr];
                if (cst > now.w) {
                    break;
                }
                let newpay = now.pays;
                newpay[curr] = i * delta + rem;
                stack.push_back({curr - 1, now.w - cst, newpay});
            }
        }
    }

    cout << maxtotal << endl;
    for (let p: maxpays) {
        cout << p << " ";
    }
    cout << endl;
}