import java.io.*;
import java.util.*;
import java.math.*;

public class E {
	public static void main(String[] args) throws IOException {

		/**/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		/*/
		Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/e.in"))));
		/**/

		long w = sc.nextLong();
		long[] items = new long[9];
		for (int i = 1; i <= 8; i++)
			items[i] = sc.nextLong();

		long wunsat = w;
		for (int i = 8; i >= 1; i--) {
			if (items[i] > 100) {
				long reduction = (items[i]-100)*i;
				if (wunsat - reduction > 1000) {
					wunsat -= reduction;
					items[i] = 100;
				} else {
					reduction = (wunsat-1000)/i*i;
					wunsat -= reduction;
					items[i] -= reduction/i;
				}
			}
		}
		boolean[][] poss = new boolean[9][5001];
		poss[0][0] = true;
		for (int i = 0; i <= Math.min(5000, items[1]); i++)
			poss[1][i] = true;
		for (int i = 2; i <= 8; i++) {
			for (int j = 0; j <= 5000; ++j) {
				for (int k = j, count=0; k >= 0 && count<=items[i]; k-=i, ++count) {
					if (poss[i-1][k]) {
						poss[i][j] = true;
						break;
					}
				}
			}
		}
		for (int i = (int)Math.min(wunsat, 5000); i >= 0; i--) {
			if (poss[8][i]) {
				wunsat -= i;
				break;
			}
		}
		System.out.println(w-wunsat);
	}
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>500) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need) o = s;
        }
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        //a = ni();
        s = nl();
        long sum=0;
        long[] m = new long[8];
        for(int q=0; q<8; q++) {
            m[q] = nl();
            sum+=m[q]*(q+1);
        }
        if(sum<=s){
            System.out.println(sum);
            return;
        }
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(o==s) return;
        if(System.currentTimeMillis()-stt>1400) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need){
                o = s;
                return;
            }
        }
        for(long q=7; q>=0; q--){
            if(m[(int)q]==0) continue;
            if(o==s) return;
            long can = Math.min(m[(int)q], (s-cur)/(q+1));
            if(can==0) continue;
            for(long w=Math.max(1, can-10), i=0; i<10 && w<=can; w++, i++){
                m[(int)q]-=w;
                rec(m, cur+(q+1)*w);
                m[(int)q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

// practice with rainboy
import java.io.*;
import java.util.*;

public class CF1132E {
	static final int L = 840, K = 8;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		long w = Long.parseLong(br.readLine());
		StringTokenizer st = new StringTokenizer(br.readLine());
		long[] aa = new long[K + 1];
		for (int i = 1; i <= K; i++)
			aa[i] = Long.parseLong(st.nextToken());
		long[] dp = new long[L * K];
		long[] dq = new long[L * K];
		dp[0] = 0;
		for (int y = 1; y < L * K; y++)
			dp[y] = -1;
		for (int i = 1; i <= K; i++) {
			long a = aa[i];
			for (int y = 0; y < L * K; y++)
				dq[y] = -1;
			for (int y = 0; y < L * K; y++) {
				if (dp[y] < 0)
					continue;
				int q_ = L / i;
				for (int q = 0; q < q_ && q <= a; q++) {
					long p = (a - q) / q_;
					int y_ = y + q * i;
					dq[y_] = Math.max(dq[y_], dp[y] + p);
				}
			}
			for (int y = 0; y < L * K; y++)
				dp[y] = dq[y];
		}
		long ans = 0;
		for (int y = 0; y < L * K && y <= w; y++)
			ans = Math.max(ans, Math.min((w - y) / L, dp[y]) * L + y);
		System.out.println(ans);
	}
}

import java.io.*;
import java.util.*;

public class A{

	public static void main(String[] args) throws IOException {
		Scanner sc=new Scanner();
		PrintWriter out=new PrintWriter(System.out);
		long W=sc.nextLong();
		long []cnt=new long [9],used=new long [9];
		long curr=0;
		for(int i=1;i<=8;i++) {
			long x=sc.nextLong();
			long min=Math.min(x, (W-curr)/i);
			curr+=min*i;
			cnt[i]=x-min;
			used[i]=min;
		}
		long ans=curr;
		int it=100000;
		Random rand=new Random();
		while(it-->0) {
			int nxt=rand.nextInt(8)+1;

			if(curr>=W) { // remove
				if(used[nxt]>0) {
					curr-=nxt;
					used[nxt]--;
					cnt[nxt]++;
				}
			}
			else  // add
			{
				if(cnt[nxt]>0) {
					cnt[nxt]--;
					used[nxt]++;
					curr+=nxt;
				}
			}
			if(curr<=W && curr>=ans)
				ans=curr;

		}
		out.println(ans);

		out.close();

	}

	static class Scanner
	{
		BufferedReader br;
		StringTokenizer st;
		Scanner(){
			br=new BufferedReader(new InputStreamReader(System.in));
		}
		Scanner(String fileName) throws FileNotFoundException{
			br=new BufferedReader(new FileReader(fileName));
		}
		String next() throws IOException {
			while(st==null || !st.hasMoreTokens())
				st=new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		String nextLine() throws IOException {
			return br.readLine();
		}
		int nextInt() throws IOException{
			return Integer.parseInt(next());
		}
		long nextLong()  throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}
		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

	}
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ERyukzak solver = new ERyukzak();
        solver.solve(1, in, out);
        out.close();
    }

    static class ERyukzak {

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long w = in.nextLong();
            long a[] = new long[9];
            int lcm = 840;
            long dp[] = new long[lcm * 8];
            for (int i = 1; i < dp.length; i++) {
                dp[i] = -1;
            }
            for (int i = 1; i < 9; i++) {
                long count = in.nextLong();
                for (int j = dp.length - 1; j >= 0; j--) {
                    if (dp[j] == -1) {
                        continue;
                    }
                    for (int h = i; h < lcm && count >= h / i; h += i) {
                        int m = j + h;
                        dp[m] = Math.max(dp[m], dp[j] + ((count - h / i) * i) / lcm);
                    }
                    dp[j] = Math.max(dp[j], dp[j] + (count * i) / lcm);
                }
            }
            long best = 0;
            for (int i = 0; i < dp.length; i++) {
                if (dp[i] == -1 || w - i < 0) {
                    continue;
                }
                long cand = Math.min((w - i) / lcm, dp[i]) * lcm + i;
                if (w - cand < w - best) {
                    best = cand;
                }
            }
            out.println(best);
        }
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            TaskE solver = new TaskE();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class TaskE {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            long w = in.readLong();
            long[] cnts = new long[9];
            for (int i = 1; i <= 8; i++) {
                cnts[i] = in.readLong();
            }
            int g = 840 * 8;
            long[][] dp = new long[9][g];
            SequenceUtils.deepFill(dp, -(long) 1e18);
            dp[0][0] = 0;
            for (int i = 1; i <= 8; i++) {
                for (int j = 0; j < g; j++) {
                    for (int k = 0; k * i < 840 && k * i <= j && k <= cnts[i]; k++) {
                        dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - k * i] + (cnts[i] - k) * i / 840);
                    }
                }
            }

            long ans = 0;
            for (int i = 0; i < g && i <= w; i++) {
                if (dp[8][i] < 0) {
                    continue;
                }
                long local = i;
                long canUse = Math.min((w - local) / 840, dp[8][i]);
                local += 840 * canUse;
                ans = Math.max(ans, local);
            }

            out.println(ans);
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }

    static class SequenceUtils {
        public static void deepFill(Object array, long val) {
            if (!array.getClass().isArray()) {
                throw new IllegalArgumentException();
            }
            if (array instanceof long[]) {
                long[] longArray = (long[]) array;
                Arrays.fill(longArray, val);
            } else {
                Object[] objArray = (Object[]) array;
                for (Object obj : objArray) {
                    deepFill(obj, val);
                }
            }
        }

    }

    static class FastOutput implements AutoCloseable, Closeable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput println(long c) {
            cache.append(c).append('\n');
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 10000;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, Math.max(0, arr[i]-8));
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

//package baobab;

import java.io.*;
import java.util.*;

public class E {

    public static void main(String[] args) {
        Solver solver = new Solver();
    }

    static class Solver {
        IO io;

        public Solver() {
            this.io = new IO();
            try {
                solve();
            } catch (RuntimeException e) {
                if (!e.getMessage().equals("Clean exit")) {
                    throw e;
                }
            } finally {
                io.close();
            }
        }

        /****************************** START READING HERE ********************************/

        void solve() {
            long maxW = io.nextLong();
            long[] unused = new long[9];
            for (int i=1; i<=8; i++) {
                unused[i] = io.nextLong();
            }
            long currW = 0;
            long[] used = new long[9];
            for (int i=1; i<=8; i++) {
                long min = 0;
                long max = unused[i];
                while (min < max) {
                    long guess = min + (max-min+1)/2;
                    if (currW+guess*i <= maxW) {
                        min = guess;
                    } else {
                        max = guess-1;
                    }
                }
                used[i] += max;
                unused[i] -= max;
                currW += max*i;
            }

            Random rng = new Random();
            long bestKnown = 0;
            for (int i=0; i<10000000; i++) {
                if (currW > maxW) {
                    int removeI = rng.nextInt(8) + 1;
                    if (used[removeI] > 0) {
                        used[removeI]--;
                        unused[removeI]++;
                        currW -= removeI;
                    }
                } else {
                    int addI = rng.nextInt(8) + 1;
                    if (unused[addI] > 0) {
                        unused[addI]--;
                        used[addI]++;
                        currW += addI;
                    }
                }
                if (currW <= maxW) {
                    bestKnown = Math.max(currW, bestKnown);
                }
            }

            io.println(bestKnown);
        }

        /************************** UTILITY CODE BELOW THIS LINE **************************/

        long MOD = (long)1e9 + 7;

        boolean closeToZero(double v) {
            // Check if double is close to zero, considering precision issues.
            return Math.abs(v) <= 0.0000000001;
        }

        void draw(boolean[][] d) {
            System.out.print("  ");
            for (int x=0; x<d[0].length; x++) {
                System.out.print(" " + x + " ");
            }
            System.out.println("");
            for (int y=0; y<d.length; y++) {
                System.out.print(y + " ");
                for (int x=0; x<d[0].length; x++) {
                    System.out.print((d[y][x] ? "[x]" : "[ ]"));
                }
                System.out.println("");
            }
        }

        void draw(int[][] d) {
            int max = 1;
            for (int y=0; y<d.length; y++) {
                for (int x=0; x<d[0].length; x++) {
                    max = Math.max(max, ("" + d[y][x]).length());
                }
            }
            System.out.print("  ");
            String format = "%" + (max+2) + "s";
            for (int x=0; x<d[0].length; x++) {
                System.out.print(String.format(format, x) + " ");
            }
            format = "%" + (max) + "s";
            System.out.println("");
            for (int y=0; y<d.length; y++) {
                System.out.print(y + " ");
                for (int x=0; x<d[0].length; x++) {
                    System.out.print(" [" + String.format(format, (d[y][x])) + "]");
                }
                System.out.println("");
            }
        }

        void draw(long[][] d) {
            int max = 1;
            for (int y=0; y<d.length; y++) {
                for (int x=0; x<d[0].length; x++) {
                    max = Math.max(max, ("" + d[y][x]).length());
                }
            }
            System.out.print("  ");
            String format = "%" + (max+2) + "s";
            for (int x=0; x<d[0].length; x++) {
                System.out.print(String.format(format, x) + " ");
            }
            format = "%" + (max) + "s";
            System.out.println("");
            for (int y=0; y<d.length; y++) {
                System.out.print(y + " ");
                for (int x=0; x<d[0].length; x++) {
                    System.out.print(" [" + String.format(format, (d[y][x])) + "]");
                }
                System.out.println("");
            }
        }

        class IDval implements Comparable<IDval> {
            int id;
            long val;

            public IDval(int id, long val) {
                this.val = val;
                this.id = id;
            }

            @Override
            public int compareTo(IDval o) {
                if (this.val < o.val) return -1;
                if (this.val > o.val) return 1;
                return this.id - o.id;
            }
        }

        private class ElementCounter {
            private HashMap<Long, Integer> elements;

            public ElementCounter() {
                elements = new HashMap<>();
            }

            public void add(long element) {
                int count = 1;
                Integer prev = elements.get(element);
                if (prev != null) count += prev;
                elements.put(element, count);
            }

            public void remove(long element) {
                int count = elements.remove(element);
                count--;
                if (count > 0) elements.put(element, count);
            }

            public int get(long element) {
                Integer val = elements.get(element);
                if (val == null) return 0;
                return val;
            }

            public int size() {
                return elements.size();
            }
        }

        class StringCounter {
            HashMap<String, Integer> elements;

            public StringCounter() {
                elements = new HashMap<>();
            }

            public void add(String identifier) {
                int count = 1;
                Integer prev = elements.get(identifier);
                if (prev != null) count += prev;
                elements.put(identifier, count);
            }

            public void remove(String identifier) {
                int count = elements.remove(identifier);
                count--;
                if (count > 0) elements.put(identifier, count);
            }

            public long get(String identifier) {
                Integer val = elements.get(identifier);
                if (val == null) return 0;
                return val;
            }

            public int size() {
                return elements.size();
            }
        }

        class DisjointSet {
            /** Union Find / Disjoint Set data structure. */
            int[] size;
            int[] parent;
            int componentCount;

            public DisjointSet(int n) {
                componentCount = n;
                size = new int[n];
                parent = new int[n];
                for (int i=0; i<n; i++) parent[i] = i;
                for (int i=0; i<n; i++) size[i] = 1;
            }

            public void join(int a, int b) {
                /* Find roots */
                int rootA = parent[a];
                int rootB = parent[b];
                while (rootA != parent[rootA]) rootA = parent[rootA];
                while (rootB != parent[rootB]) rootB = parent[rootB];

                if (rootA == rootB) {
                    /* Already in the same set */
                    return;
                }

                /* Merge smaller set into larger set. */
                if (size[rootA] > size[rootB]) {
                    size[rootA] += size[rootB];
                    parent[rootB] = rootA;
                } else {
                    size[rootB] += size[rootA];
                    parent[rootA] = rootB;
                }
                componentCount--;
            }

        }

        class Trie {
            int N;
            int Z;
            int nextFreeId;
            int[][] pointers;
            boolean[] end;

            /** maxLenSum = maximum possible sum of length of words */
            public Trie(int maxLenSum, int alphabetSize) {
                this.N = maxLenSum;
                this.Z = alphabetSize;
                this.nextFreeId = 1;
                pointers = new int[N+1][alphabetSize];
                end = new boolean[N+1];
            }

            public void addWord(String word) {
                int curr = 0;
                for (int j=0; j<word.length(); j++) {
                    int c = word.charAt(j) - 'a';
                    int next = pointers[curr][c];
                    if (next == 0) {
                        next = nextFreeId++;
                        pointers[curr][c] = next;
                    }
                    curr = next;
                }
                end[curr] = true;
            }

            public boolean hasWord(String word) {
                int curr = 0;
                for (int j=0; j<word.length(); j++) {
                    int c = word.charAt(j) - 'a';
                    int next = pointers[curr][c];
                    if (next == 0) return false;
                    curr = next;
                }
                return end[curr];
            }

        }

        private static class Prob {

            /** For heavy calculations on probabilities, this class
             *  provides more accuracy & efficiency than doubles.
             *  Math explained: https://en.wikipedia.org/wiki/Log_probability
             *  Quick start:
             *      - Instantiate probabilities, eg. Prob a = new Prob(0.75)
             *      - add(), multiply() return new objects, can perform on nulls & NaNs.
             *      - get() returns probability as a readable double */

            /** Logarithmized probability. Note: 0% represented by logP NaN. */
            private double logP;

            /** Construct instance with real probability. */
            public Prob(double real) {
                if (real > 0) this.logP = Math.log(real);
                else this.logP = Double.NaN;
            }

            /** Construct instance with already logarithmized value. */
            static boolean dontLogAgain = true;
            public Prob(double logP, boolean anyBooleanHereToChooseThisConstructor) {
                this.logP = logP;
            }

            /** Returns real probability as a double. */
            public double get() {
                return Math.exp(logP);
            }

            @Override
            public String toString() {
                return ""+get();
            }

            /***************** STATIC METHODS BELOW ********************/

            /** Note: returns NaN only when a && b are both NaN/null. */
            public static Prob add(Prob a, Prob b) {
                if (nullOrNaN(a) && nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain);
                if (nullOrNaN(a)) return copy(b);
                if (nullOrNaN(b)) return copy(a);

                double x = Math.max(a.logP, b.logP);
                double y = Math.min(a.logP, b.logP);
                double sum = x + Math.log(1 + Math.exp(y - x));
                return new Prob(sum, dontLogAgain);
            }

            /** Note: multiplying by null or NaN produces NaN (repping 0% real prob). */
            public static Prob multiply(Prob a, Prob b) {
                if (nullOrNaN(a) || nullOrNaN(b)) return new Prob(Double.NaN, dontLogAgain);
                return new Prob(a.logP + b.logP, dontLogAgain);
            }

            /** Returns true if p is null or NaN. */
            private static boolean nullOrNaN(Prob p) {
                return (p == null || Double.isNaN(p.logP));
            }

            /** Returns a new instance with the same value as original. */
            private static Prob copy(Prob original) {
                return new Prob(original.logP, dontLogAgain);
            }
        }

        class Binary implements Comparable<Binary> {

            /**
             * Use example: Binary b = new Binary(Long.toBinaryString(53249834L));
             *
             * When manipulating small binary strings, instantiate new Binary(string)
             * When just reading large binary strings, instantiate new Binary(string,true)
             * get(int i) returns a character '1' or '0', not an int.
             */

            private boolean[] d;
            private int first; // Starting from left, the first (most remarkable) '1'
            public int length;

            public Binary(String binaryString) {
                this(binaryString, false);
            }
            public Binary(String binaryString, boolean initWithMinArraySize) {
                length = binaryString.length();
                int size = Math.max(2*length, 1);
                first = length/4;
                if (initWithMinArraySize) {
                    first = 0;
                    size = Math.max(length, 1);
                }
                d = new boolean[size];
                for (int i=0; i<length; i++) {
                    if (binaryString.charAt(i) == '1') d[i+first] = true;
                }
            }

            public void addFirst(char c) {
                if (first-1 < 0) doubleArraySize();
                first--;
                d[first] = (c == '1' ? true : false);
                length++;
            }

            public void addLast(char c) {
                if (first+length >= d.length) doubleArraySize();
                d[first+length] = (c == '1' ? true : false);
                length++;
            }

            private void doubleArraySize() {
                boolean[] bigArray = new boolean[(d.length+1) * 2];
                int newFirst = bigArray.length / 4;
                for (int i=0; i<length; i++) {
                    bigArray[i + newFirst] = d[i + first];
                }
                first = newFirst;
                d = bigArray;
            }

            public boolean flip(int i) {
                boolean value = (this.d[first+i] ? false : true);
                this.d[first+i] = value;
                return value;
            }

            public void set(int i, char c) {
                boolean value = (c == '1' ? true : false);
                this.d[first+i] = value;
            }

            public char get(int i) {
                return (this.d[first+i] ? '1' : '0');
            }

            @Override
            public int compareTo(Binary o) {
                if (this.length != o.length) return this.length - o.length;
                int len = this.length;
                for (int i=0; i<len; i++) {
                    int diff = this.get(i) - o.get(i);
                    if (diff != 0) return diff;
                }
                return 0;
            }

            @Override
            public String toString() {
                StringBuilder sb = new StringBuilder();
                for (int i=0; i<length; i++) {
                    sb.append(d[i+first] ? '1' : '0');
                }
                return sb.toString();
            }

        }

        /************************** Range queries **************************/

        class FenwickMin {
            long n;
            long[] original;
            long[] bottomUp;
            long[] topDown;

            public FenwickMin(int n) {
                this.n = n;
                original = new long[n+2];
                bottomUp = new long[n+2];
                topDown = new long[n+2];
            }

            public void set(int modifiedNode, long value) {
                long replaced = original[modifiedNode];
                original[modifiedNode] = value;
                // Update left tree
                int i = modifiedNode;
                long v = value;
                while (i <= n) {
                    if (v > bottomUp[i]) {
                        if (replaced == bottomUp[i]) {
                            v = Math.min(v, original[i]);
                            for (int r=1 ;; r++) {
                                int x = (i&-i)>>>r;
                                if (x == 0) break;
                                int child = i-x;
                                v = Math.min(v, bottomUp[child]);
                            }
                        } else break;
                    }
                    if (v == bottomUp[i]) break;
                    bottomUp[i] = v;
                    i += (i&-i);
                }
                // Update right tree
                i = modifiedNode;
                v = value;
                while (i > 0) {
                    if (v > topDown[i]) {
                        if (replaced == topDown[i]) {
                            v = Math.min(v, original[i]);
                            for (int r=1 ;; r++) {
                                int x = (i&-i)>>>r;
                                if (x == 0) break;
                                int child = i+x;
                                if (child > n+1) break;
                                v = Math.min(v, topDown[child]);
                            }
                        } else break;
                    }
                    if (v == topDown[i]) break;
                    topDown[i] = v;
                    i -= (i&-i);
                }
            }

            public long getMin(int a, int b) {
                long min = original[a];
                int prev = a;
                int curr = prev + (prev&-prev); // parent right hand side
                while (curr <= b) {
                    min = Math.min(min, topDown[prev]); // value from the other tree
                    prev = curr;
                    curr = prev + (prev&-prev);;
                }
                min = Math.min(min, original[prev]);
                prev = b;
                curr = prev - (prev&-prev); // parent left hand side
                while (curr >= a) {
                    min = Math.min(min,bottomUp[prev]); // value from the other tree
                    prev = curr;
                    curr = prev - (prev&-prev);
                }
                return min;
            }

        }

        class FenwickSum {
            public long[] d;

            public FenwickSum(int n) {
                d=new long[n+1];
            }

            /** a[0] must be unused. */
            public FenwickSum(long[] a) {
                d=new long[a.length];
                for (int i=1; i<a.length; i++) {
                    modify(i, a[i]);
                }
            }

            /** Do not modify i=0. */
            void modify(int i, long v) {
                while (i<d.length) {
                    d[i] += v;
                    // Move to next uplink on the RIGHT side of i
                    i += (i&-i);
                }
            }

            /** Returns sum from a to b, *BOTH* inclusive. */
            long getSum(int a, int b) {
                return getSum(b) - getSum(a-1);
            }

            private long getSum(int i) {
                long sum = 0;
                while (i>0) {
                    sum += d[i];
                    // Move to next uplink on the LEFT side of i
                    i -= (i&-i);
                }
                return sum;
            }
        }

        class SegmentTree {

            /* Provides log(n) operations for:
             * - Range query (sum, min or max)
             * - Range update ("+8 to all values between indexes 4 and 94")
             */

            int N;
            long[] lazy;
            long[] sum;
            long[] min;
            long[] max;
            boolean supportSum;
            boolean supportMin;
            boolean supportMax;

            public SegmentTree(int n) {
                this(n, true, true, true);
            }

            public SegmentTree(int n, boolean supportSum, boolean supportMin, boolean supportMax) {
                for (N=2; N<n;) N*=2;
                this.lazy = new long[2*N];
                this.supportSum = supportSum;
                this.supportMin = supportMin;
                this.supportMax = supportMax;
                if (this.supportSum) this.sum = new long[2*N];
                if (this.supportMin) this.min = new long[2*N];
                if (this.supportMax) this.max = new long[2*N];
            }

            void modifyRange(long x, int a, int b) {
                modifyRec(a, b, 1, 0, N-1, x);
            }

            void setRange() {
                //TODO
            }

            long getSum(int a, int b) {
                return querySum(a, b, 1, 0, N-1);
            }

            long getMin(int a, int b) {
                return queryMin(a, b, 1, 0, N-1);
            }

            long getMax(int a, int b) {
                return queryMax(a, b, 1, 0, N-1);
            }

            private long querySum(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) {
                if (wantedLeft > actualRight || wantedRight < actualLeft) {
                    return 0;
                }
                if (wantedLeft == actualLeft && wantedRight == actualRight) {
                    int count = wantedRight - wantedLeft + 1;
                    return sum[i] + count * lazy[i];
                }
                if (lazy[i] != 0) propagate(i, actualLeft, actualRight);
                int d = (actualRight - actualLeft + 1) / 2;
                long left = querySum(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1);
                long right = querySum(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight);
                return left + right;
            }

            private long queryMin(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) {
                if (wantedLeft > actualRight || wantedRight < actualLeft) {
                    return Long.MAX_VALUE;
                }
                if (wantedLeft == actualLeft && wantedRight == actualRight) {
                    return min[i] + lazy[i];
                }
                if (lazy[i] != 0) propagate(i, actualLeft, actualRight);
                int d = (actualRight - actualLeft + 1) / 2;
                long left = queryMin(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1);
                long right = queryMin(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight);
                return min(left, right);
            }

            private long queryMax(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight) {
                if (wantedLeft > actualRight || wantedRight < actualLeft) {
                    return Long.MIN_VALUE;
                }
                if (wantedLeft == actualLeft && wantedRight == actualRight) {
                    return max[i] + lazy[i];
                }
                if (lazy[i] != 0) propagate(i, actualLeft, actualRight);
                int d = (actualRight - actualLeft + 1) / 2;
                long left = queryMax(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1);
                long right = queryMax(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight);
                return max(left, right);
            }

            private void modifyRec(int wantedLeft, int wantedRight, int i, int actualLeft, int actualRight, long value) {
                if (wantedLeft > actualRight || wantedRight < actualLeft) {
                    return;
                }
                if (wantedLeft == actualLeft && wantedRight == actualRight) {
                    lazy[i] += value;
                    return;
                }
                if (lazy[i] != 0) propagate(i, actualLeft, actualRight);
                int d = (actualRight - actualLeft + 1) / 2;
                modifyRec(wantedLeft, min(actualLeft+d-1, wantedRight), 2*i, actualLeft, actualLeft+d-1, value);
                modifyRec(max(actualLeft+d, wantedLeft), wantedRight, 2*i+1, actualLeft+d, actualRight, value);
                if (supportSum) sum[i] += value * (min(actualRight, wantedRight) - max(actualLeft, wantedLeft) + 1);
                if (supportMin) min[i] = min(min[2*i] + lazy[2*i], min[2*i+1] + lazy[2*i+1]);
                if (supportMax) max[i] = max(max[2*i] + lazy[2*i], max[2*i+1] + lazy[2*i+1]);
            }

            private void propagate(int i, int actualLeft, int actualRight) {
                lazy[2*i] += lazy[i];
                lazy[2*i+1] += lazy[i];
                if (supportSum) sum[i] += lazy[i] * (actualRight - actualLeft + 1);
                if (supportMin) min[i] += lazy[i];
                if (supportMax) max[i] += lazy[i];
                lazy[i] = 0;
            }

        }

        /***************************** Graphs *****************************/

        List<Integer>[] toGraph(IO io, int n) {
            /* Trees only. */
            List<Integer>[] g = new ArrayList[n+1];
            for (int i=1; i<=n; i++) g[i] = new ArrayList<>();
            for (int i=1; i<=n-1; i++) {
                int a = io.nextInt();
                int b = io.nextInt();
                g[a].add(b);
                g[b].add(a);
            }
            return g;
        }

        class Graph {

            int n;
            List<Integer>[] edges;

            public Graph(int n) {
                this.n = n;
                edges = new ArrayList[n+1];
                for (int i=1; i<=n; i++) edges[i] = new ArrayList<>();
            }

            void addBiEdge(int a, int b) {
                addEdge(a, b);
                addEdge(b, a);
            }

            void addEdge(int from, int to) {
                edges[from].add(to);
            }

            /*********** Strongly Connected Components (Kosaraju) ****************/

            ArrayList<Integer>[] bacw;

            public int getCount() {
                bacw = new ArrayList[n+1];
                for (int i=1; i<=n; i++) {
                    bacw[i] = new ArrayList<Integer>();
                }
                for (int a=1; a<=n; a++) {
                    for (int b : edges[a]) {
                        bacw[b].add(a);
                    }
                }
                int count = 0;
                List<Integer> list = new ArrayList<Integer>();
                boolean[] visited = new boolean[n+1];
                for (int i=1; i<=n; i++) {
                    dfsForward(i, visited, list);
                }
                visited = new boolean[n+1];
                for (int i=n-1; i>=0; i--) {
                    int node = list.get(i);
                    if (visited[node]) continue;
                    count++;
                    dfsBackward(node, visited);
                }
                return count;
            }

            void dfsForward(int i, boolean[] visited, List<Integer> list) {
                if (visited[i]) return;
                visited[i] = true;
                for (int neighbor : edges[i]) {
                    dfsForward(neighbor, visited, list);
                }
                list.add(i);
            }

            void dfsBackward(int i, boolean[] visited) {
                if (visited[i]) return;
                visited[i] = true;
                for (int neighbor : bacw[i]) {
                    dfsBackward(neighbor, visited);
                }
            }

            /************************* Topological Order **********************/

            int UNTOUCHED = 0;
            int FINISHED = 2;
            int INPROGRESS = 1;
            int[] vis;
            List<Integer> topoAns;

            // Returns nodes in topological order or null if cycle was found
            public List<Integer> topoSort() {
                topoAns = new ArrayList<>();
                vis = new int[n+1];
                for (int i=1; i<=n; i++) {
                    if (!topoDFS(i)) return null;
                }
                Collections.reverse(topoAns);
                return topoAns;
            }

            boolean topoDFS(int curr) {
                Integer status = vis[curr];
                if (status == null) status = UNTOUCHED;
                if (status == FINISHED) return true;
                if (status == INPROGRESS) {
                    return false;
                }
                vis[curr] = INPROGRESS;
                for (int next : edges[curr]) {
                    if (!topoDFS(next)) return false;
                }
                vis[curr] = FINISHED;
                topoAns.add(curr);
                return true;
            }

        }

        class LCAFinder {

            /* O(n log n) Initialize: new LCAFinder(graph)
             * O(log n) Queries: find(a,b) returns lowest common ancestor for nodes a and b */

            int[] nodes;
            int[] depths;
            int[] entries;
            int pointer;
            FenwickMin fenwick;

            public LCAFinder(List<Integer>[] graph) {
                this.nodes = new int[(int)10e6];
                this.depths = new int[(int)10e6];
                this.entries = new int[graph.length];
                this.pointer = 1;
                boolean[] visited = new boolean[graph.length+1];
                dfs(1, 0, graph, visited);
                fenwick = new FenwickMin(pointer-1);
                for (int i=1; i<pointer; i++) {
                    fenwick.set(i, depths[i] * 1000000L + i);
                }
            }

            private void dfs(int node, int depth, List<Integer>[] graph, boolean[] visited) {
                visited[node] = true;
                entries[node] = pointer;
                nodes[pointer] = node;
                depths[pointer] = depth;
                pointer++;
                for (int neighbor : graph[node]) {
                    if (visited[neighbor]) continue;
                    dfs(neighbor, depth+1, graph, visited);
                    nodes[pointer] = node;
                    depths[pointer] = depth;
                    pointer++;
                }
            }

            public int find(int a, int b) {
                int left = entries[a];
                int right = entries[b];
                if (left > right) {
                    int temp = left;
                    left = right;
                    right = temp;
                }
                long mixedBag = fenwick.getMin(left, right);
                int index = (int) (mixedBag % 1000000L);
                return nodes[index];
            }
        }

        /**************************** Geometry ****************************/

        class Point {
            int y;
            int x;

            public Point(int y, int x) {
                this.y = y;
                this.x = x;
            }
        }

        boolean segmentsIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
            // Returns true if segment 1-2 intersects segment 3-4

            if (x1 == x2 && x3 == x4) {
                // Both segments are vertical
                if (x1 != x3) return false;
                if (min(y1,y2) < min(y3,y4)) {
                    return max(y1,y2) >= min(y3,y4);
                } else {
                    return max(y3,y4) >= min(y1,y2);
                }
            }
            if (x1 == x2) {
                // Only segment 1-2 is vertical. Does segment 3-4 cross it? y = a*x + b
                double a34 = (y4-y3)/(x4-x3);
                double b34 = y3 - a34*x3;
                double y = a34 * x1 + b34;
                return y >= min(y1,y2) && y <= max(y1,y2) && x1 >= min(x3,x4) && x1 <= max(x3,x4);
            }
            if (x3 == x4) {
                // Only segment 3-4 is vertical. Does segment 1-2 cross it? y = a*x + b
                double a12 = (y2-y1)/(x2-x1);
                double b12 = y1 - a12*x1;
                double y = a12 * x3 + b12;
                return y >= min(y3,y4) && y <= max(y3,y4) && x3 >= min(x1,x2) && x3 <= max(x1,x2);
            }
            double a12 = (y2-y1)/(x2-x1);
            double b12 = y1 - a12*x1;
            double a34 = (y4-y3)/(x4-x3);
            double b34 = y3 - a34*x3;
            if (closeToZero(a12 - a34)) {
                // Parallel lines
                return closeToZero(b12 - b34);
            }
            // Non parallel non vertical lines intersect at x. Is x part of both segments?
            double x = -(b12-b34)/(a12-a34);
            return x >= min(x1,x2) && x <= max(x1,x2) && x >= min(x3,x4) && x <= max(x3,x4);
        }

        boolean pointInsideRectangle(Point p, List<Point> r, boolean countBorderAsInside) {
            Point a = r.get(0);
            Point b = r.get(1);
            Point c = r.get(2);
            Point d = r.get(3);
            double apd = areaOfTriangle(a, p, d);
            double dpc = areaOfTriangle(d, p, c);
            double cpb = areaOfTriangle(c, p, b);
            double pba = areaOfTriangle(p, b, a);
            double sumOfAreas = apd + dpc + cpb + pba;
            if (closeToZero(sumOfAreas - areaOfRectangle(r))) {
                if (closeToZero(apd) || closeToZero(dpc) || closeToZero(cpb) || closeToZero(pba)) {
                    return countBorderAsInside;
                }
                return true;
            }
            return false;
        }

        double areaOfTriangle(Point a, Point b, Point c) {
            return 0.5 * Math.abs((a.x-c.x)*(b.y-a.y)-(a.x-b.x)*(c.y-a.y));
        }

        double areaOfRectangle(List<Point> r) {
            double side1xDiff = r.get(0).x - r.get(1).x;
            double side1yDiff = r.get(0).y - r.get(1).y;
            double side2xDiff = r.get(1).x - r.get(2).x;
            double side2yDiff = r.get(1).y - r.get(2).y;
            double side1 = Math.sqrt(side1xDiff * side1xDiff + side1yDiff * side1yDiff);
            double side2 = Math.sqrt(side2xDiff * side2xDiff + side2yDiff * side2yDiff);
            return side1 * side2;
        }

        boolean pointsOnSameLine(double x1, double y1, double x2, double y2, double x3, double y3) {
            double areaTimes2 = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2);
            return (closeToZero(areaTimes2));
        }

        class PointToLineSegmentDistanceCalculator {

            // Just call this
            double minDistFromPointToLineSegment(double point_x, double point_y, double x1, double y1, double x2, double y2) {
                return Math.sqrt(distToSegmentSquared(point_x, point_y, x1, y1, x2, y2));
            }

            private double distToSegmentSquared(double point_x, double point_y, double x1, double y1, double x2, double y2) {
                double l2 = dist2(x1,y1,x2,y2);
                if (l2 == 0) return dist2(point_x, point_y, x1, y1);
                double t = ((point_x - x1) * (x2 - x1) + (point_y - y1) * (y2 - y1)) / l2;
                if (t < 0) return dist2(point_x, point_y, x1, y1);
                if (t > 1) return dist2(point_x, point_y, x2, y2);

                double com_x = x1 + t * (x2 - x1);
                double com_y = y1 + t * (y2 - y1);
                return dist2(point_x, point_y, com_x, com_y);
            }

            private double dist2(double x1, double y1, double x2, double y2) {
                return Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2);
            }

        }

        /****************************** Math ******************************/

        long pow(long base, int exp) {
            if (exp == 0) return 1L;
            long x = pow(base, exp/2);
            long ans = x * x;
            if (exp % 2 != 0) ans *= base;
            return ans;
        }

        long gcd(long... v) {
            /** Chained calls to Euclidean algorithm. */
            if (v.length == 1) return v[0];
            long ans = gcd(v[1], v[0]);
            for (int i=2; i<v.length; i++) {
                ans = gcd(ans, v[i]);
            }
            return ans;
        }

        long gcd(long a, long b) {
            /** Euclidean algorithm. */
            if (b == 0) return a;
            return gcd(b, a%b);
        }

        int[] generatePrimesUpTo(int last) {
            /* Sieve of Eratosthenes. Practically O(n). Values of 0 indicate primes. */
            int[] div = new int[last+1];
            for (int x=2; x<=last; x++) {
                if (div[x] > 0) continue;
                for (int u=2*x; u<=last; u+=x) {
                    div[u] = x;
                }
            }
            return div;
        }

        long lcm(long a, long b) {
            /** Least common multiple */
            return a * b / gcd(a,b);
        }

        class BaseConverter {

            /* Palauttaa luvun esityksen kannassa base */
            public String convert(Long number, int base) {
                return Long.toString(number, base);
            }

            /* Palauttaa luvun esityksen kannassa baseTo, kun annetaan luku Stringinä kannassa baseFrom */
            public String convert(String number, int baseFrom, int baseTo) {
                return Long.toString(Long.parseLong(number, baseFrom), baseTo);
            }

            /* Tulkitsee kannassa base esitetyn luvun longiksi (kannassa 10) */
            public long longify(String number, int baseFrom) {
                return Long.parseLong(number, baseFrom);
            }
        }

        class BinomialCoefficients {
            /** Total number of K sized unique combinations from pool of size N (unordered)
             N! / ( K! (N - K)! )   */

            /** For simple queries where output fits in long. */
            public long biCo(long n, long k) {
                long r = 1;
                if (k > n) return 0;
                for (long d = 1; d <= k; d++) {
                    r *= n--;
                    r /= d;
                }
                return r;
            }

            /** For multiple queries with same n, different k. */
            public long[] precalcBinomialCoefficientsK(int n, int maxK) {
                long v[] = new long[maxK+1];
                v[0] = 1; // nC0 == 1
                for (int i=1; i<=n; i++) {
                    for (int j=Math.min(i,maxK); j>0; j--) {
                        v[j] = v[j] + v[j-1]; // Pascal's triangle
                    }
                }
                return v;
            }

            /** When output needs % MOD. */
            public long[] precalcBinomialCoefficientsK(int n, int k, long M) {
                long v[] = new long[k+1];
                v[0] = 1; // nC0 == 1
                for (int i=1; i<=n; i++) {
                    for (int j=Math.min(i,k); j>0; j--) {
                        v[j] = v[j] + v[j-1]; // Pascal's triangle
                        v[j] %= M;
                    }
                }
                return v;
            }
        }

        int invertNumber(int a, int k) {
            // Inverts the binary representation of a, using only k last bits e.g. 01101 -> 10010
            int inv32k = ~a;
            int mask = 1;
            for (int i = 1; i < k; ++i) mask |= mask << 1;
            return inv32k & mask;
        }

        /**************************** Strings ****************************/

        class Zalgo {

            public int pisinEsiintyma(String haku, String kohde) {
                char[] s = new char[haku.length() + 1 + kohde.length()];
                for (int i=0; i<haku.length(); i++) {
                    s[i] = haku.charAt(i);
                }
                int j = haku.length();
                s[j++] = '#';
                for (int i=0; i<kohde.length(); i++) {
                    s[j++] = kohde.charAt(i);
                }
                int[] z = toZarray(s);
                int max = 0;
                for (int i=haku.length(); i<z.length; i++) {
                    max = Math.max(max, z[i]);
                }
                return max;
            }

            public int[] toZarray(char[] s) {
                int n = s.length;
                int[] z = new int[n];
                int a = 0, b = 0;
                for (int i = 1; i < n; i++) {
                    if (i > b) {
                        for (int j = i; j < n && s[j - i] == s[j]; j++)  z[i]++;
                    }
                    else {
                        z[i] = z[i - a];
                        if (i + z[i - a] > b) {
                            for (int j = b + 1; j < n && s[j - i] == s[j]; j++) z[i]++;
                            a = i;
                            b = i + z[i] - 1;
                        }
                    }
                }
                return z;
            }

            public List<Integer> getStartIndexesWhereWordIsFound(String haku, String kohde) {
                // this is alternative use case
                char[] s = new char[haku.length() + 1 + kohde.length()];
                for (int i=0; i<haku.length(); i++) {
                    s[i] = haku.charAt(i);
                }
                int j = haku.length();
                s[j++] = '#';
                for (int i=0; i<kohde.length(); i++) {
                    s[j++] = kohde.charAt(i);
                }
                int[] z = toZarray(s);
                List<Integer> indexes = new ArrayList<>();
                for (int i=haku.length(); i<z.length; i++) {
                    if (z[i] < haku.length()) continue;
                    indexes.add(i);
                }
                return indexes;
            }

        }

        class StringHasher {

            class HashedString {
                long[] hashes;
                long[] modifiers;

                public HashedString(long[] hashes, long[] modifiers) {
                    this.hashes = hashes;
                    this.modifiers = modifiers;
                }
            }

            long P;
            long M;

            public StringHasher() {
                initializePandM();
            }

            HashedString hashString(String s) {
                int n = s.length();
                long[] hashes = new long[n];
                long[] modifiers = new long[n];
                hashes[0] = s.charAt(0);
                modifiers[0] = 1;
                for (int i=1; i<n; i++) {
                    hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M;
                    modifiers[i] = (modifiers[i-1] * P) % M;
                }
                return new HashedString(hashes, modifiers);
            }

            /**
             * Indices are inclusive.
             */
            long getHash(HashedString hashedString, int startIndex, int endIndex) {
                long[] hashes = hashedString.hashes;
                long[] modifiers = hashedString.modifiers;
                long result = hashes[endIndex];
                if (startIndex > 0) result -= (hashes[startIndex-1] * modifiers[endIndex-startIndex+1]) % M;
                if (result < 0) result += M;
                return result;
            }

            // Less interesting methods below

            /**
             * Efficient for 2 input parameter strings in particular.
             */
            HashedString[] hashString(String first, String second) {
                HashedString[] array = new HashedString[2];
                int n = first.length();
                long[] modifiers = new long[n];
                modifiers[0] = 1;

                long[] firstHashes = new long[n];
                firstHashes[0] = first.charAt(0);
                array[0] = new HashedString(firstHashes, modifiers);

                long[] secondHashes = new long[n];
                secondHashes[0] = second.charAt(0);
                array[1] = new HashedString(secondHashes, modifiers);

                for (int i=1; i<n; i++) {
                    modifiers[i] = (modifiers[i-1] * P) % M;
                    firstHashes[i] = (firstHashes[i-1] * P + first.charAt(i)) % M;
                    secondHashes[i] = (secondHashes[i-1] * P + second.charAt(i)) % M;
                }
                return array;
            }

            /**
             * Efficient for 3+ strings
             * More efficient than multiple hashString calls IF strings are same length.
             */
            HashedString[] hashString(String... strings) {
                HashedString[] array = new HashedString[strings.length];
                int n = strings[0].length();
                long[] modifiers = new long[n];
                modifiers[0] = 1;
                for (int j=0; j<strings.length; j++) {
                    // if all strings are not same length, defer work to another method
                    if (strings[j].length() != n) {
                        for (int i=0; i<n; i++) {
                            array[i] = hashString(strings[i]);
                        }
                        return array;
                    }

                    // otherwise initialize stuff
                    long[] hashes = new long[n];
                    hashes[0] = strings[j].charAt(0);
                    array[j] = new HashedString(hashes, modifiers);
                }
                for (int i=1; i<n; i++) {
                    modifiers[i] = (modifiers[i-1] * P) % M;
                    for (int j=0; j<strings.length; j++) {
                        String s = strings[j];
                        long[] hashes = array[j].hashes;
                        hashes[i] = (hashes[i-1] * P + s.charAt(i)) % M;
                    }
                }
                return array;
            }

            void initializePandM() {
                ArrayList<Long> modOptions = new ArrayList<>(20);
                modOptions.add(353873237L);
                modOptions.add(353875897L);
                modOptions.add(353878703L);
                modOptions.add(353882671L);
                modOptions.add(353885303L);
                modOptions.add(353888377L);
                modOptions.add(353893457L);
                P = modOptions.get(new Random().nextInt(modOptions.size()));

                modOptions.clear();
                modOptions.add(452940277L);
                modOptions.add(452947687L);
                modOptions.add(464478431L);
                modOptions.add(468098221L);
                modOptions.add(470374601L);
                modOptions.add(472879717L);
                modOptions.add(472881973L);
                M = modOptions.get(new Random().nextInt(modOptions.size()));

            }
        }

        int editDistance(String a, String b) {
            a = "#"+a;
            b = "#"+b;
            int n = a.length();
            int m = b.length();
            int[][] dp = new int[n+1][m+1];
            for (int y=0; y<=n; y++) {
                for (int x=0; x<=m; x++) {
                    if (y == 0) dp[y][x] = x;
                    else if (x == 0) dp[y][x] = y;
                    else {
                        int e1 = dp[y-1][x] + 1;
                        int e2 = dp[y][x-1] + 1;
                        int e3 = dp[y-1][x-1] + (a.charAt(y-1) != b.charAt(x-1) ? 1 : 0);
                        dp[y][x] = min(e1, e2, e3);
                    }
                }
            }
            return dp[n][m];
        }

        /*************************** Technical ***************************/

        private class IO extends PrintWriter {
            private InputStreamReader r;
            private static final int BUFSIZE = 1 << 15;
            private char[] buf;
            private int bufc;
            private int bufi;
            private StringBuilder sb;

            public IO() {
                super(new BufferedOutputStream(System.out));
                r = new InputStreamReader(System.in);

                buf = new char[BUFSIZE];
                bufc = 0;
                bufi = 0;
                sb = new StringBuilder();
            }

            /** Print, flush, return nextInt. */
            private int queryInt(String s) {
                io.println(s);
                io.flush();
                return nextInt();
            }

            /** Print, flush, return nextLong. */
            private long queryLong(String s) {
                io.println(s);
                io.flush();
                return nextLong();
            }

            /** Print, flush, return next word. */
            private String queryNext(String s) {
                io.println(s);
                io.flush();
                return next();
            }

            private void fillBuf() throws IOException {
                bufi = 0;
                bufc = 0;
                while(bufc == 0) {
                    bufc = r.read(buf, 0, BUFSIZE);
                    if(bufc == -1) {
                        bufc = 0;
                        return;
                    }
                }
            }

            private boolean pumpBuf() throws IOException {
                if(bufi == bufc) {
                    fillBuf();
                }
                return bufc != 0;
            }

            private boolean isDelimiter(char c) {
                return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
            }

            private void eatDelimiters() throws IOException {
                while(true) {
                    if(bufi == bufc) {
                        fillBuf();
                        if(bufc == 0) throw new RuntimeException("IO: Out of input.");
                    }

                    if(!isDelimiter(buf[bufi])) break;
                    ++bufi;
                }
            }

            public String next() {
                try {
                    sb.setLength(0);

                    eatDelimiters();
                    int start = bufi;

                    while(true) {
                        if(bufi == bufc) {
                            sb.append(buf, start, bufi - start);
                            fillBuf();
                            start = 0;
                            if(bufc == 0) break;
                        }

                        if(isDelimiter(buf[bufi])) break;
                        ++bufi;
                    }

                    sb.append(buf, start, bufi - start);

                    return sb.toString();
                } catch(IOException e) {
                    throw new RuntimeException("IO.next: Caught IOException.");
                }
            }

            public int nextInt() {
                try {
                    int ret = 0;

                    eatDelimiters();

                    boolean positive = true;
                    if(buf[bufi] == '-') {
                        ++bufi;
                        if(!pumpBuf()) throw new RuntimeException("IO.nextInt: Invalid int.");
                        positive = false;
                    }

                    boolean first = true;
                    while(true) {
                        if(!pumpBuf()) break;
                        if(isDelimiter(buf[bufi])) {
                            if(first) throw new RuntimeException("IO.nextInt: Invalid int.");
                            break;
                        }
                        first = false;

                        if(buf[bufi] >= '0' && buf[bufi] <= '9') {
                            if(ret < -214748364) throw new RuntimeException("IO.nextInt: Invalid int.");
                            ret *= 10;
                            ret -= (int)(buf[bufi] - '0');
                            if(ret > 0) throw new RuntimeException("IO.nextInt: Invalid int.");
                        } else {
                            throw new RuntimeException("IO.nextInt: Invalid int.");
                        }

                        ++bufi;
                    }

                    if(positive) {
                        if(ret == -2147483648) throw new RuntimeException("IO.nextInt: Invalid int.");
                        ret = -ret;
                    }

                    return ret;
                } catch(IOException e) {
                    throw new RuntimeException("IO.nextInt: Caught IOException.");
                }
            }

            public long nextLong() {
                try {
                    long ret = 0;

                    eatDelimiters();

                    boolean positive = true;
                    if(buf[bufi] == '-') {
                        ++bufi;
                        if(!pumpBuf()) throw new RuntimeException("IO.nextLong: Invalid long.");
                        positive = false;
                    }

                    boolean first = true;
                    while(true) {
                        if(!pumpBuf()) break;
                        if(isDelimiter(buf[bufi])) {
                            if(first) throw new RuntimeException("IO.nextLong: Invalid long.");
                            break;
                        }
                        first = false;

                        if(buf[bufi] >= '0' && buf[bufi] <= '9') {
                            if(ret < -922337203685477580L) throw new RuntimeException("IO.nextLong: Invalid long.");
                            ret *= 10;
                            ret -= (long)(buf[bufi] - '0');
                            if(ret > 0) throw new RuntimeException("IO.nextLong: Invalid long.");
                        } else {
                            throw new RuntimeException("IO.nextLong: Invalid long.");
                        }

                        ++bufi;
                    }

                    if(positive) {
                        if(ret == -9223372036854775808L) throw new RuntimeException("IO.nextLong: Invalid long.");
                        ret = -ret;
                    }

                    return ret;
                } catch(IOException e) {
                    throw new RuntimeException("IO.nextLong: Caught IOException.");
                }
            }

            public double nextDouble() {
                return Double.parseDouble(next());
            }

        }

        void print(Object output) {
            io.println(output);
        }

        void done(Object output) {
            print(output);
            done();
        }

        void done() {
            io.close();
            throw new RuntimeException("Clean exit");
        }

        long min(long... v) {
            long ans = v[0];
            for (int i=1; i<v.length; i++) {
                ans = Math.min(ans, v[i]);
            }
            return ans;
        }

        double min(double... v) {
            double ans = v[0];
            for (int i=1; i<v.length; i++) {
                ans = Math.min(ans, v[i]);
            }
            return ans;
        }

        int min(int... v) {
            int ans = v[0];
            for (int i=1; i<v.length; i++) {
                ans = Math.min(ans, v[i]);
            }
            return ans;
        }

        long max(long... v) {
            long ans = v[0];
            for (int i=1; i<v.length; i++) {
                ans = Math.max(ans, v[i]);
            }
            return ans;
        }

        double max(double... v) {
            double ans = v[0];
            for (int i=1; i<v.length; i++) {
                ans = Math.max(ans, v[i]);
            }
            return ans;
        }

        int max(int... v) {
            int ans = v[0];
            for (int i=1; i<v.length; i++) {
                ans = Math.max(ans, v[i]);
            }
            return ans;
        }

    }

}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 840;
        long[][] dp = new long[9][lcm];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm; j++) {
                if (dp[i - 1][j] != -1) {
                    for (int k = 0; k < min(lcm / i, cnt[i] + 1); k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % lcm] = max(dp[i][(j + k * i) % lcm], dp[i - 1][j] + (j + k * i) / lcm + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 840;
        long[][] dp = new long[9][lcm * 8];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm * 8; j++) {
                if (dp[i - 1][j] != -1) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + min((W - (dp[i - 1][j] * lcm + j)) / lcm, cnt[i] / (lcm / i)));
                    for (int k = 1; (k * i) % lcm != 0 && k <= cnt[i]; k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % (lcm * 8)] = max(dp[i][(j + k * i) % (lcm * 8)], dp[i - 1][j] + (j + k * i) / (lcm * 8) + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm * 8; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 840;
        long[][] dp = new long[9][lcm];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm; j++) {
                if (dp[i - 1][j] != -1) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + min((W - (dp[i - 1][j] * lcm + j)) / lcm, cnt[i] / (lcm / i)));
                    for (int k = 1; (k * i) % lcm != 0 && k <= cnt[i]; k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % lcm] = max(dp[i][(j + k * i) % lcm], dp[i - 1][j] + (j + k * i) / lcm + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 840;
        long[][] dp = new long[9][lcm];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;

        for (int i = 1; i <= 8; i++)
            for (int j = 0; j < lcm; j++)
                if (dp[i - 1][j] != -1)
                    for (int k = 0; k < min(lcm / i, cnt[i] + 1); k++)
                        if (dp[i - 1][j] * lcm + j + k * i <= W)
                            dp[i][(j + k * i) % lcm] = max(dp[i][(j + k * i) % lcm], dp[i - 1][j] + (j + k * i) / lcm + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));

        long ans = 0;
        for (int i = 0; i < lcm; i++)
            ans = max(ans, dp[8][i] * lcm + i);
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main1{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static boolean sieve[];
    static ArrayList<Integer> primes;
    static ArrayList<Long> factorial;
    static HashSet<Integer> graph[];
/****************************************Solutions Begins*****************************************/
    static long W,input[],T,dp[][];
    static long dfs(int i,int t){
        if(i==8){
            return t;
        }
        if(dp[i][t]!=-1){
            return dp[i][t];
        }
        long ans=0;
        for(int j=0;j<=input[i];j++){
            int kk=t+j*(i+1);
            if(kk<=T){
                ans=Math.max(ans,dfs(i+1,kk));
            }
            else break;
        }
        dp[i][t]=ans;
        return dp[i][t];
    }
    public static void main (String[] args) throws Exception {
        String st[]=br.readLine().split(" ");
        W=Long.parseLong(st[0]);
        input=new long[8];
        st=br.readLine().split(" ");
        long tot=0;
        for(int i=0;i<8;i++){
            input[i]=Long.parseLong(st[i]);
            tot+=(i+1)*input[i];
        }
        long ans=0;
        long totw=W;

        if(tot<=W){
            ans=tot;
        }
        else{
            for(int i=7;i>=0;i--){
                long take=Math.max(0,Math.min(input[i]-10,(long)(totw-1000)/(i+1)));
                ///debug(input[i]-10,(totw-1000)/(i+1));
                ans+=take*(i+1);
                totw-=take*(i+1);

                input[i]=input[i]-take;
                //debug(take);
            }
         //   debug(input);
          //  debug(totw);
            dp=new long[8][(int)totw+1];
            for(long arr[]:dp){
                Arrays.fill(arr,-1);
            }
            T=totw;
            ans+=dfs(0,0);
        }
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }

/****************************************Template Begins************************************************/
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.00000000000000000");
        out.println(ft.format(d));
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
/*********************************************PAIR********************************************************/
    static class PairComp implements Comparator<Pair>{
        public int compare(Pair p1,Pair p2){
            if(p1.u>p2.u){
                return -1;
            }
            else if(p1.u<p2.u){
                return 81;
            }
            else{
                return p2.v-p1.v;
            }
        }
    }
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*************************************************Long Pair*************************************************/
    static class PairCompL implements Comparator<Pairl>{
        public int compare(Pairl p1,Pairl p2){
            if(p1.v>p2.v){
                return -1;
            }
            else if(p1.v<p2.v){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    static class Pairl implements Comparable<Pair> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }

            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }

            public boolean equals(Object o) {
                Pair other = (Pair) o;
                return u == other.u && v == other.v;
            }

            public int compareTo(Pair other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }

            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c) {
        long x=1;
        long y=a;
        while(b > 0){
            if(b%2 == 1){
                x=(x*y)%c;
            }
            y = (y*y)%c; // squaring the base
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y)
    {
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/******************************************SIEVE**********************************************************/
    static void sieveMake(int n){
        sieve=new boolean[n];
        Arrays.fill(sieve,true);
        sieve[0]=false;
        sieve[1]=false;
        for(int i=2;i*i<n;i++){
            if(sieve[i]){
                for(int j=i*i;j<n;j+=i){
                    sieve[j]=false;
                }
            }
        }
        primes=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            if(sieve[i]){
                primes.add(i);
            }
        }
    }
/********************************************End***********************************************************/
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author beginner1010
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        boolean[][] visited;
        long[][] dp;

        long rec(int pos, int rem_weight, long[] cnt, int pack) {
            if (pos == 8) {
                return rem_weight == 0 ? 0 : -1;
            }

            if (visited[pos][rem_weight] == true) {
                return dp[pos][rem_weight];
            }
            visited[pos][rem_weight] = true;

            long res = -1;
            int used = 0;
            while (used <= cnt[pos] && used * (pos + 1) < pack && rem_weight - used * (pos + 1) >= 0) {
                long counter = ((cnt[pos] - used) * (pos + 1)) / pack;
                long cur_res = rec(pos + 1, rem_weight - used * (pos + 1), cnt, pack);
                if (cur_res != -1) {
                    assert (cur_res >= 0);
                    res = Math.max(res, cur_res + counter);
                }
                used++;
            }

            dp[pos][rem_weight] = res;
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long W = in.nextLong();
            long[] cnt = new long[8];
            for (int i = 0; i < 8; i++) {
                cnt[i] = in.nextLong();
            }
            int pack = 840;
            visited = new boolean[8][8 * pack + 1];
            dp = new long[8][8 * pack + 1];

            long ans = 0;
            for (int i = 0; i <= Math.min(8 * pack, W); i++) {
                long res = rec(0, i, cnt, pack);
                long bound = Math.min((W - i) / pack, res);
                ans = Math.max(ans, bound * pack + i);
            }
            out.println(ans);
            return;
        }

    }

    static class InputReader {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputStream stream;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public long nextLong() {
            int c = read();
            while (isWhitespace(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isWhitespace(c));
            return res * sgn;
        }

    }
}

import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Main implements Runnable {
    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars==-1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                }
                catch (IOException e) {
                    throw new InputMismatchException();
                }

                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        public int nextInt() {
            int c = read();

            while(isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if(c<'0'||c>'9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
                return res * sgn;
        }

        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    public static void main(String args[]) throws Exception {
        new Thread(null, new Main(),"Main",1<<26).start();
    }
    public void run() {
        InputReader sc = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);

        long weight = sc.nextLong();

        long cnt[] = new long[9];
        for(int i = 1; i <= 8; ++i)
            cnt[i] = sc.nextLong();

        long cycle = 840, possCycles = 0;
        int possSum = 0;
        for(int i = 1; i <= 8; ++i) {
            long reqEle = cycle / i;
            long curCycle = cnt[i] / reqEle;

            long curPossCycles = max(0, curCycle - 1);
            possCycles += curPossCycles;
            cnt[i] -= curPossCycles * reqEle;
            possSum += (int)cnt[i] * i;
        }

        long dp[][] = new long[9][possSum + 1];
        dp[0][0] = 1;
        for(int i = 0; i <= possSum; ++i) {
            for(int j = 1; j <= 8; ++j) {
                for(int k = 0; k <= cnt[j]; ++k) {
                    if(i - k * j < 0)
                        break;
                    dp[j][i] |= dp[j - 1][i - j * k];
                }
            }
        }

        long ans = 0;
        for(int i = 0; i <= possSum; ++i) {
            if(i <= weight && dp[8][i] == 1) {
                long left = weight - i;
                long ansCycles = min(possCycles, left / cycle);
                long cans = i + ansCycles * cycle;
                ans = max(ans, cans);
            }
        }

        w.print(ans);

        w.close();
    }
}

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		StringTokenizer sb=new StringTokenizer(in.readLine());
		long w=Long.parseLong(sb.nextToken());
		long[] cnt=new long[9];
		sb=new StringTokenizer(in.readLine());
		long tot=0;
		for (int i=1;i<=8;++i) {
			cnt[i]=Long.parseLong(sb.nextToken());
			long num=cnt[i]/(840/i);
			if (num>0) tot+=num-1;
			cnt[i]%=(840/i);
			if (num>0) cnt[i]+=840/i;
		}
		int maxW=840*16;
		if (w<maxW) maxW=(int)w;
		boolean[] dp=new boolean[maxW+1];
		dp[0]=true;
		long res=0;
		for (int i=1;i<=8;++i) {
			int num=(int)cnt[i];
			for (int j=0;j<20;++j) {
				if (num<=(1<<j)) break;
				num-=(1<<j);
				for (int k=maxW;k>=(1<<j)*i;--k)
					dp[k]|=dp[k-i*(1<<j)];
			}
			for (int k=maxW;k>=num*i;--k)
				dp[k]|=dp[k-num*i];
		}
		for (int i=0;i<=maxW;++i) if (dp[i])
			res=Math.max(res,i+Math.min((w-i)/840,tot)*840);
		out.println(res);
		out.close();
	}
}

import java.lang.*;
import java.math.*;
import java.util.*;
import java.io.*;

public class Main{
    void solve(){
        long W=nl();
        long cnt[]=new long[9];
        long sm=0;
        for(int i=1;i<=8;i++){
            cnt[i]=nl();
            sm+=(cnt[i]*i);
        }
        if(sm<=W){
            pw.println(sm);
            return;
        }
        long dp[][]=new long[9][8*L+1];
        for(int i=0;i<=8;i++) Arrays.fill(dp[i],-1);
        dp[0][0]=0;
        for(int i=1;i<=8;i++){
            for(int j=0;j<8*L;j++){
                for(int k=0;k<L/i && k*i<=j && k<=cnt[i];k++){
                    if(dp[i-1][j-k*i]==-1) continue;
                    long p=(cnt[i]-k)/(L/i);
                    dp[i][j]=Math.max(dp[i][j],dp[i-1][j-k*i]+p);
                }
            }
        }
        long ans=0;
        for(int y=0;y<8*L && y<=W;y++){
            if(dp[8][y]==-1) continue;
            long x=Math.min(dp[8][y],(W-y)/L);
        //    if(x!=0) pw.println(y+" "+dp[8][y]);
            ans=Math.max(ans,y+x*L);
        }
        pw.println(ans);
    }
    int L=840;

    long M = (long)1e9+7;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");

    }

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
    }

}

import java.util.*;

public class SomeClass {
    private static long w, cnt[], all;

    private static int solveDp() {
        int dp[] = new int[1010];
        dp[0] = 1;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < Math.min(cnt[i], 1000); j++) {
                for (int k = 1000 - 1; k >= 0; k--) {
                    if (dp[k] == 1) {
                        dp[k + i] = 1;
                    }
                }
            }
        }
        // System.out.println(w);
        for(int i = (int) w; i >= 0; i--) {
            if (dp[i] == 1) {
                return i;
            }
        }
        return 0;
    }

    private static void reduce() {
        for (int i = 1; i <= 8; i++) {
            if (cnt[i] > 20) {
                long toTake = cnt[i] - 20;
                if (w - toTake * i >= 1000) {
                    w -= toTake * i;
                    cnt[i] = 20;
                } else {
                    long diff = w - 1000;
                    long take = diff / i + 1;
                    cnt[i] -= take;
                    w -= take * i;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        w = input.nextLong();
        cnt = new long[9];
        for (int i = 1; i <= 8; i++) {
            cnt[i] = input.nextLong();
            all += 1L * i * cnt[i];
        }
        if (all <= w) {
            System.out.println(all);
            return;
        }
        long oldW = w;
        if (w > 1000) {
            reduce();
        }
        System.out.println(oldW - w + solveDp());
    }
}

import java.math.*;
import java.util.*;
import java.io.*;

public class Prg10 {

    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        System.out.println(o);
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>500) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++) if((s-cur)%(q+1)==0 && m[q]>=(s-cur)/(q+1)) o = s;
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>500) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need){
                o = s;
                return;
            }
        }
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
import java.io.*;

public class Prg10 {

    long o =0, s, stt = System.currentTimeMillis();

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        rec(m, 0);
        System.out.println(o);
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>500) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++) if((s-cur)%(q+1)==0 && m[q]>=(s-cur)/(q+1)) o = s;
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(o==s) return;
        if(System.currentTimeMillis()-stt>1000) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need){
                o = s;
                return;
            }
        }
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EKnapsack solver = new EKnapsack();
        solver.solve(1, in, out);
        out.close();
    }

    static class EKnapsack {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            long w = in.scanLong();
            long arr[] = new long[8 + 1];
            for (int i = 1; i <= 8; i++) arr[i] = in.scanLong();
            long dp[][] = new long[9][(840 * 8) + 1];
            for (int i = 0; i <= 8; i++) Arrays.fill(dp[i], Long.MIN_VALUE / 2);

            dp[0][0] = 0;
            for (int i = 1; i <= 8; i++) {
                for (int sum = 0; sum <= 8 * 840l; sum++) {
                    for (int count = 0; count <= Math.min(arr[i], 840 / i); count++) {
                        if (sum - (count * i) >= 0 && dp[i - 1][sum - (count * i)] >= 0) {
                            dp[i][sum] = Math.max(dp[i][sum], dp[i - 1][sum - (count * i)] + ((arr[i] - count) / (840 / i)));
                        }
                    }
                }
            }

            long ans = 0;

            for (int sum = 0; sum <= 8 * 840; sum++) {
                if (sum <= w && dp[8][sum] >= 0) {
                    ans = Math.max(ans, (long) sum + (840 * Math.min(dp[8][sum], (w - sum) / 840)));
                }
            }

            out.println(ans);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

        public long scanLong() {
            long I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>1000) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need){
                o = s;
                return;
            }
        }
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(o==s) return;
        if(System.currentTimeMillis()-stt>1400) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need){
                o = s;
                return;
            }
        }
        for(long q=7; q>=0; q--){
            if(m[(int)q]==0) continue;
            if(o==s) return;
            long can = Math.min(m[(int)q], (s-cur)/(q+1));
            if(can==0) continue;
            for(long w=Math.max(1, can-10), i=0; i<10 && w<=can; w++, i++){
                m[(int)q]-=w;
                rec(m, cur+(q+1)*w);
                m[(int)q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
import java.io.*;

public class Prg10 {

    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        System.out.println(o);
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>500) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            if(m[q]>=(s-cur)/(q+1)) o = s;
        }
        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(System.currentTimeMillis()-stt>1000) return;
        o = Math.max(o, cur);

        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s, stt;

    void run() {
        s = nl();
        long[] m = new long[8];
        for(int q=0; q<8; q++) m[q] = nl();
        stt = System.currentTimeMillis();
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(o==s) return;
        if(System.currentTimeMillis()-stt>1000) return;
        o = Math.max(o, cur);

        for(int q=7; q>=0; q--){
            long can = Math.min(m[q], (s-cur)/(q+1));
            for(long w=Math.max(1, can-10); w<=can; w++){
                m[q]-=w;
                rec(m, cur+(q+1)*w);
                m[q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            TaskE solver = new TaskE();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class TaskE {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            long w = in.readLong();
            long[] cnts = new long[9];
            for (int i = 1; i <= 8; i++) {
                cnts[i] = in.readLong();
            }
            long ans = solve(w, cnts, new int[8], new boolean[9], 0);
            out.println(ans);
        }

        public long solve(long w, long[] cnts, int[] perm, boolean[] occupy, int i) {
            if (i == 8) {
                long sum = 0;
                for (int p : perm) {
                    long used = Math.min(cnts[p], (w - sum) / p);
                    sum += used * p;
                }
                return sum;
            }
            long ans = 0;
            for (int j = 1; j <= 8; j++) {
                if (occupy[j]) {
                    continue;
                }
                occupy[j] = true;
                perm[i] = j;
                ans = Math.max(ans, solve(w, cnts, perm, occupy, i + 1));
                occupy[j] = false;
            }
            return ans;
        }

    }

    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public long readLong() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            long val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }

    static class FastOutput implements AutoCloseable, Closeable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput println(long c) {
            cache.append(c).append('\n');
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 100;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]);
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 100;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]-(100/i));
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 10000;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]);
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 100;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]-8);
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 1000;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]-(100/i));
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long sum = nextLong();
	long[] arr = new long[8];
	for(int i=0;i<8;i++) {
	    arr[i] = nextLong();
	}
	long max = 0;
	for(int i=0;i<256;i++) {
	    long s = 0;
	    for(int j=0;j<arr.length;j++) {
		s += arr[j] * ((i >> j) & 1);
	    }
	    if(s > max && s <= sum) {
		max = s;
	    }
	}
	println(max);
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 1000;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]);
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 100;
	for (int i = arr.length - 1; i >= 1; i--) {
	    long cnt = (safeSum - sum) / i;
	    boolean done = false;
	    if (cnt <= arr[i]) {
		done = true;
	    }
	    cnt = Math.min(cnt, arr[i]);
	    sum -= cnt * i;
	    arr[i] -= cnt;
	    if (done) {
		break;
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * arr[ind];
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 100;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]-(64/i));
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    public static void solve() throws Exception {
	long maxSum = nextLong();
	long[] arr = new long[9];
	for (int i = 1; i <= 8; i++) {
	    arr[i] = nextLong();
	}
	leaveMax4(arr);

	long sum = 0;
	long safeSum = maxSum - 10000;
	if (safeSum > 0) {
	    for (int i = arr.length - 1; i >= 1; i--) {
		long cnt = (safeSum - sum) / i;
		boolean done = false;
		if (cnt <= arr[i]) {
		    done = true;
		}
		cnt = Math.min(cnt, arr[i]-8);
		sum += cnt * i;
		arr[i] -= cnt;
		if (done) {
		    break;
		}
	    }
	}
	System.out.println(go(arr, sum, maxSum, 8));
    }

    private static long go(long[] arr, long sum, long maxSum, int ind) {
	long max = sum;
	for (long i = 0; i <= arr[ind]; i++) {
	    long v = sum + i * ind;
	    if (v == maxSum) {
		return maxSum;
	    } else if (v > maxSum) {
		break;
	    } else {
		if (ind > 1) {
		    v = go(arr, v, maxSum, ind - 1);
		}
		if (v == maxSum) {
		    return maxSum;
		}
		if (v > max) {
		    max = v;
		}
	    }
	}
	return max;
    }

    private static void leaveMax4(long[] arr) {
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		for (int j = i + 1; j < arr.length; j++) {
		    if (arr[j] > 0) {
			int mod = j % i;
			if (mod == 0) {
			    int x = j / i;
			    if (arr[i] >= x - 1) {
				arr[i] += x * arr[j];
				arr[j] = 0;
			    }
			}
		    }
		}
	    }
	}
	int count = 0;
	for (int i = 1; i < arr.length; i++) {
	    if (arr[i] > 0) {
		count++;
	    }
	}
	if (count > 4) {
	    throw new RuntimeException();
	}
    }

    public static void main(String[] args) throws Exception {
	try {
	    fastReader = new FastReader(System.in);
	    systemOut = new BufferedOutputStream(System.out);
	    solve();
	} finally {
	    systemOut.close();
	}
    }

    private static FastReader fastReader = null;
    private static BufferedOutputStream systemOut = null;

    public static void print(Object obj) {
	print(obj.toString());
    }

    public static void print(String str) {
	try {
	    systemOut.write(str.getBytes("utf-8"));
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static void println(Object obj) {
	println(obj.toString());
    }

    public static void println(String str) {
	try {
	    print(str);
	    systemOut.write('\n');
	} catch (Exception ex) {
	    throw new RuntimeException(ex);
	}
    }

    public static String next() {
	return fastReader.readNextToken(false);
    }

    public static String nextLine() {
	return fastReader.readNextToken(true);
    }

    public static int nextInt() {
	return Integer.parseInt(fastReader.readNextToken(false));
    }

    public static long nextLong() {
	return Long.parseLong(fastReader.readNextToken(false));
    }

    public static double nextDouble() {
	return Double.parseDouble(fastReader.readNextToken(false));
    }

    static class FastReader {
	private byte[] buf = new byte[65536];
	private int ind = 0;
	private int maxInd = -1;
	private InputStream is = null;
	private boolean eof = false;
	private boolean lastCharRead = false;

	public FastReader(InputStream is) {
	    this.is = is;
	}

	public String readNextToken(boolean endOfLine) {
	    try {
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		while (true) {
		    if (lastCharRead) {
			return null;
		    } else if (ind > maxInd) {
			if (eof) {
			    lastCharRead = true;
			} else {
			    fillBuffer();
			}
		    }
		    byte b = '\n';
		    if (!lastCharRead) {
			b = buf[ind++];
		    }

		    if (b == '\r') {
			// ignore
		    } else if ((b == '\n' && endOfLine) || (Character.isWhitespace(b) && !endOfLine)) {
			if (found) {
			    break;
			}
		    } else {
			sb.append((char) b);
			found = true;
		    }
		}
		return sb.toString();
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}

	private void fillBuffer() {
	    try {
		int read = is.read(buf, 0, buf.length);
		if (read < buf.length) {
		    eof = true;
		}
		ind = 0;
		maxInd = read - 1;
	    } catch (Exception ex) {
		throw new RuntimeException(ex);
	    }
	}
    }
}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 8;
        long[][] dp = new long[9][lcm];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm; j++) {
                if (dp[i - 1][j] != -1) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + min((W - (dp[i - 1][j] * lcm + j)) / lcm, cnt[i] / (lcm / i)));
                    for (int k = 1; (k * i) % lcm != 0 && k <= cnt[i]; k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % lcm] = max(dp[i][(j + k * i) % lcm], dp[i - 1][j] + (j + k * i) / lcm + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 1024;
        long[][] dp = new long[9][lcm];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm; j++) {
                if (dp[i - 1][j] != -1) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + min((W - (dp[i - 1][j] * lcm + j)) / lcm, cnt[i] / (lcm / i)));
                    for (int k = 1; (k * i) % lcm != 0 && k <= cnt[i]; k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % lcm] = max(dp[i][(j + k * i) % lcm], dp[i - 1][j] + (j + k * i) / lcm + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) / (lcm / i)));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.fill;

public class Main {
    FastScanner in;
    PrintWriter out;

    private void solve() throws IOException {
        long W = in.nextLong();
        long[] cnt = new long[9];
        for (int i = 1; i <= 8; i++)
            cnt[i] = in.nextLong();

        int lcm = 8;
        long[][] dp = new long[9][lcm];
        for (int i = 0; i < 9; i++)
            fill(dp[i], -1);
        dp[0][0] = 0;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < lcm; j++) {
                if (dp[i - 1][j] != -1) {
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + min((W - (dp[i - 1][j] * lcm + j)) / lcm, cnt[i] * i / lcm));
                    for (int k = 1; (k * i) % lcm != 0 && k <= cnt[i]; k++) {
                        if (dp[i - 1][j] * lcm + j + k * i <= W) {
                            dp[i][(j + k * i) % lcm] = max(dp[i][(j + k * i) % lcm], dp[i - 1][j] + (j + k * i) / lcm + min((W - (dp[i - 1][j] * lcm + j + k * i)) / lcm, (cnt[i] - k) * i / lcm));
                        }
                    }
                }
            }
        }

        long ans = 0;
        for (int i = 0; i < lcm; i++) {
            if (dp[8][i] != -1) {
                ans = max(ans, dp[8][i] * lcm + i);
            }
        }
        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean hasNextLine() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

    public static void main(String[] args) throws IOException {
        new Main().run();
    }
}

/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main1{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static boolean sieve[];
    static ArrayList<Integer> primes;
    static ArrayList<Long> factorial;
    static HashSet<Integer> graph[];
/****************************************Solutions Begins*****************************************/
    static long W,input[],arr[],L,target,dp[][];
    static long dfs(int i,int t){
        if(i==8){
            return t;
        }
        if(dp[i][t]!=-1){
            return dp[i][t];
        }
        long ans=0;
        for(int j=0;j<=(int)arr[i];j++){
            if(t+j*(i+1)<=target){
                ans=Math.max(ans,dfs(i+1,t+j*(i+1)));
            }
        }
        dp[i][t]=ans;
        return dp[i][t];
    }
    public static void main (String[] args) throws Exception {
        String st[]=br.readLine().split(" ");
        W=Long.parseLong(st[0]);
        input=new long[8];
        st=br.readLine().split(" ");
        long tot=0;
        for(int i=0;i<8;i++){
            input[i]=Long.parseLong(st[i]);
            tot+=(i+1)*input[i];
        }
        long ans=0;
        if(tot<=W){
            ans=tot;
        }
        else{
            arr=new long[8];
            L=840;
            target=W-L*((W-1)/L);
            for(int i=0;i<8;i++){
                arr[i]=Math.min(input[i],L/(i+1)-1);
            }
            dp=new long[8][(int)target+1];
            for(long arr[]:dp){
                Arrays.fill(arr,-1);
            }
            ans=L*((W-1)/L)+dfs(0,0);
        }
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }

/****************************************Template Begins************************************************/
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.00000000000000000");
        out.println(ft.format(d));
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
/*********************************************PAIR********************************************************/
    static class PairComp implements Comparator<Pair>{
        public int compare(Pair p1,Pair p2){
            if(p1.u>p2.u){
                return -1;
            }
            else if(p1.u<p2.u){
                return 81;
            }
            else{
                return p2.v-p1.v;
            }
        }
    }
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*************************************************Long Pair*************************************************/
    static class PairCompL implements Comparator<Pairl>{
        public int compare(Pairl p1,Pairl p2){
            if(p1.v>p2.v){
                return -1;
            }
            else if(p1.v<p2.v){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    static class Pairl implements Comparable<Pair> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }

            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }

            public boolean equals(Object o) {
                Pair other = (Pair) o;
                return u == other.u && v == other.v;
            }

            public int compareTo(Pair other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }

            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c) {
        long x=1;
        long y=a;
        while(b > 0){
            if(b%2 == 1){
                x=(x*y)%c;
            }
            y = (y*y)%c; // squaring the base
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y)
    {
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/******************************************SIEVE**********************************************************/
    static void sieveMake(int n){
        sieve=new boolean[n];
        Arrays.fill(sieve,true);
        sieve[0]=false;
        sieve[1]=false;
        for(int i=2;i*i<n;i++){
            if(sieve[i]){
                for(int j=i*i;j<n;j+=i){
                    sieve[j]=false;
                }
            }
        }
        primes=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            if(sieve[i]){
                primes.add(i);
            }
        }
    }
/********************************************End***********************************************************/
}

/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main1{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static boolean sieve[];
    static ArrayList<Integer> primes;
    static ArrayList<Long> factorial;
    static HashSet<Integer> graph[];
/****************************************Solutions Begins*****************************************/
    static long W,input[],arr[],L,target,dp[][],dd;
    static long dfs(int i,int t){
        if(i==8){
            dd=Math.max(dd,t);
            return t;
        }
        if(dp[i][t]!=-1){
            return dp[i][t];
        }
        long ans=0;
        for(int j=0;j<=(int)arr[i];j++){
            if(t+j*(i+1)<=target){
                ans=Math.max(ans,dfs(i+1,t+j*(i+1))+L*(((input[i]-j)*(i+1))/L));
            }
        }
        dp[i][t]=ans;
        return dp[i][t];
    }
    public static void main (String[] args) throws Exception {
        String st[]=br.readLine().split(" ");
        W=Long.parseLong(st[0]);
        input=new long[8];
        st=br.readLine().split(" ");
        long tot=0;
        for(int i=0;i<8;i++){
            input[i]=Long.parseLong(st[i]);
            tot+=(i+1)*input[i];
        }
        long ans=0;
        if(tot<=W){
            ans=tot;
        }
        else{
            arr=new long[8];
            L=840;
            target=W-L*((W-1)/L);
            for(int i=0;i<8;i++){
                arr[i]=Math.min(input[i],L/(i+1)-1);
            }
            dp=new long[8][(int)target+1];
            for(long arr[]:dp){
                Arrays.fill(arr,-1);
            }
           // debug(arr);
            //debug(target,W,L*((W-1)/L),tot,input[7]*8/840-(W-1)/L);
           // dd=0;
            //debug(dfs(0,0),dd);
            ans=Math.min(dd+L*((W-1)/L),dfs(0,0));
        }
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }

/****************************************Template Begins************************************************/
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.00000000000000000");
        out.println(ft.format(d));
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
/*********************************************PAIR********************************************************/
    static class PairComp implements Comparator<Pair>{
        public int compare(Pair p1,Pair p2){
            if(p1.u>p2.u){
                return -1;
            }
            else if(p1.u<p2.u){
                return 81;
            }
            else{
                return p2.v-p1.v;
            }
        }
    }
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*************************************************Long Pair*************************************************/
    static class PairCompL implements Comparator<Pairl>{
        public int compare(Pairl p1,Pairl p2){
            if(p1.v>p2.v){
                return -1;
            }
            else if(p1.v<p2.v){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    static class Pairl implements Comparable<Pair> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }

            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }

            public boolean equals(Object o) {
                Pair other = (Pair) o;
                return u == other.u && v == other.v;
            }

            public int compareTo(Pair other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }

            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c) {
        long x=1;
        long y=a;
        while(b > 0){
            if(b%2 == 1){
                x=(x*y)%c;
            }
            y = (y*y)%c; // squaring the base
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y)
    {
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/******************************************SIEVE**********************************************************/
    static void sieveMake(int n){
        sieve=new boolean[n];
        Arrays.fill(sieve,true);
        sieve[0]=false;
        sieve[1]=false;
        for(int i=2;i*i<n;i++){
            if(sieve[i]){
                for(int j=i*i;j<n;j+=i){
                    sieve[j]=false;
                }
            }
        }
        primes=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            if(sieve[i]){
                primes.add(i);
            }
        }
    }
/********************************************End***********************************************************/
}

/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main1{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static boolean sieve[];
    static ArrayList<Integer> primes;
    static ArrayList<Long> factorial;
    static HashSet<Integer> graph[];
/****************************************Solutions Begins*****************************************/
    static long W,input[],arr[],L,target,dp[][],dd;
    static long dfs(int i,int t){
        if(i==8){
            dd=Math.max(dd,t);
            return t;
        }
        if(dp[i][t]!=-1){
            return dp[i][t];
        }
        long ans=0;
        for(int j=0;j<=(int)arr[i];j++){
            if(t+j*(i+1)<=target){
                ans=Math.max(ans,dfs(i+1,t+j*(i+1))+L*(((input[i]-j)*(i+1))/L));
            }
        }
        dp[i][t]=ans;
        return dp[i][t];
    }
    public static void main (String[] args) throws Exception {
        String st[]=br.readLine().split(" ");
        W=Long.parseLong(st[0]);
        input=new long[8];
        st=br.readLine().split(" ");
        long tot=0;
        for(int i=0;i<8;i++){
            input[i]=Long.parseLong(st[i]);
            tot+=(i+1)*input[i];
        }
        long ans=0;
        if(tot<=W){
            ans=tot;
        }
        else{
            arr=new long[8];
            L=840;
            target=W-L*((W-1)/L);
            for(int i=0;i<8;i++){
                arr[i]=Math.min(input[i],L/(i+1)-1);
            }
            dp=new long[8][(int)target+1];
            for(long arr[]:dp){
                Arrays.fill(arr,-1);
            }
           // debug(arr);
            //debug(target,W,L*((W-1)/L),tot,input[7]*8/840-(W-1)/L);
            dd=0;
           // debug(dfs(0,0),dd);
           dfs(0,0);
            ans=Math.min(dd+L*((W-1)/L),dfs(0,0));
        }
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }

/****************************************Template Begins************************************************/
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.00000000000000000");
        out.println(ft.format(d));
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
/*********************************************PAIR********************************************************/
    static class PairComp implements Comparator<Pair>{
        public int compare(Pair p1,Pair p2){
            if(p1.u>p2.u){
                return -1;
            }
            else if(p1.u<p2.u){
                return 81;
            }
            else{
                return p2.v-p1.v;
            }
        }
    }
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*************************************************Long Pair*************************************************/
    static class PairCompL implements Comparator<Pairl>{
        public int compare(Pairl p1,Pairl p2){
            if(p1.v>p2.v){
                return -1;
            }
            else if(p1.v<p2.v){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    static class Pairl implements Comparable<Pair> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }

            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }

            public boolean equals(Object o) {
                Pair other = (Pair) o;
                return u == other.u && v == other.v;
            }

            public int compareTo(Pair other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }

            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c) {
        long x=1;
        long y=a;
        while(b > 0){
            if(b%2 == 1){
                x=(x*y)%c;
            }
            y = (y*y)%c; // squaring the base
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y)
    {
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/******************************************SIEVE**********************************************************/
    static void sieveMake(int n){
        sieve=new boolean[n];
        Arrays.fill(sieve,true);
        sieve[0]=false;
        sieve[1]=false;
        for(int i=2;i*i<n;i++){
            if(sieve[i]){
                for(int j=i*i;j<n;j+=i){
                    sieve[j]=false;
                }
            }
        }
        primes=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            if(sieve[i]){
                primes.add(i);
            }
        }
    }
/********************************************End***********************************************************/
}

/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main1{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static boolean sieve[];
    static ArrayList<Integer> primes;
    static ArrayList<Long> factorial;
    static HashSet<Integer> graph[];
/****************************************Solutions Begins*****************************************/
    static long W,input[],arr[],L,target,dp[][],dd;
    static long dfs(int i,int t){
        if(i==8){
            dd=Math.max(dd,t);
            return t;
        }
        if(dp[i][t]!=-1){
            return dp[i][t];
        }
        long ans=0;
        for(int j=0;j<=(int)arr[i];j++){
            if(t+j*(i+1)<=target){
                ans=Math.max(ans,dfs(i+1,t+j*(i+1))+L*(((input[i]-j)*(i+1))/L));
            }
        }
        dp[i][t]=ans;
        return dp[i][t];
    }
    public static void main (String[] args) throws Exception {
        String st[]=br.readLine().split(" ");
        W=Long.parseLong(st[0]);
        input=new long[8];
        st=br.readLine().split(" ");
        long tot=0;
        for(int i=0;i<8;i++){
            input[i]=Long.parseLong(st[i]);
            tot+=(i+1)*input[i];
        }
        long ans=0;
        if(tot<=W){
            ans=tot;
        }
        else{
            arr=new long[8];
            L=840;
            target=W-L*((W-1)/L);
            for(int i=0;i<8;i++){
                arr[i]=Math.min(input[i],L/(i+1)-1);
            }
            dp=new long[8][(int)target+1];
            for(long arr[]:dp){
                Arrays.fill(arr,-1);
            }
           // debug(arr);
            //debug(target,W,L*((W-1)/L),tot,input[7]*8/840-(W-1)/L);
            dd=0;
           // debug(dfs(0,0),dd);
            ans=dfs(0,0);
        }
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }

/****************************************Template Begins************************************************/
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.00000000000000000");
        out.println(ft.format(d));
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
/*********************************************PAIR********************************************************/
    static class PairComp implements Comparator<Pair>{
        public int compare(Pair p1,Pair p2){
            if(p1.u>p2.u){
                return -1;
            }
            else if(p1.u<p2.u){
                return 81;
            }
            else{
                return p2.v-p1.v;
            }
        }
    }
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*************************************************Long Pair*************************************************/
    static class PairCompL implements Comparator<Pairl>{
        public int compare(Pairl p1,Pairl p2){
            if(p1.v>p2.v){
                return -1;
            }
            else if(p1.v<p2.v){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    static class Pairl implements Comparable<Pair> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }

            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }

            public boolean equals(Object o) {
                Pair other = (Pair) o;
                return u == other.u && v == other.v;
            }

            public int compareTo(Pair other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }

            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c) {
        long x=1;
        long y=a;
        while(b > 0){
            if(b%2 == 1){
                x=(x*y)%c;
            }
            y = (y*y)%c; // squaring the base
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y)
    {
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/******************************************SIEVE**********************************************************/
    static void sieveMake(int n){
        sieve=new boolean[n];
        Arrays.fill(sieve,true);
        sieve[0]=false;
        sieve[1]=false;
        for(int i=2;i*i<n;i++){
            if(sieve[i]){
                for(int j=i*i;j<n;j+=i){
                    sieve[j]=false;
                }
            }
        }
        primes=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            if(sieve[i]){
                primes.add(i);
            }
        }
    }
/********************************************End***********************************************************/
}

/*Author: Satyajeet Singh, Delhi Technological University*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.*;
import java.math.*;
public class Main1{
/*********************************************Constants******************************************/
    static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static long mod=(long)1e9+7;
    static long mod1=998244353;
    static boolean sieve[];
    static ArrayList<Integer> primes;
    static ArrayList<Long> factorial;
    static HashSet<Integer> graph[];
/****************************************Solutions Begins*****************************************/
    static long W,input[],ans;
    static void dfs(int i,long wt){
        if(i==8){
            if(wt<=W){
                ans=Math.max(ans,wt);
            }
            return ;
        }
        dfs(i+1,wt);
        dfs(i+1,wt+input[i]);
        return ;
    }
    public static void main (String[] args) throws Exception {
        String st[]=br.readLine().split(" ");
        W=Long.parseLong(st[0]);
        input=new long[8];
        st=br.readLine().split(" ");
        for(int i=0;i<8;i++){
            input[i]=Long.parseLong(st[i]);
        }
        ans=0;
        dfs(0,0l);
        out.println(ans);
/****************************************Solutions Ends**************************************************/
        out.flush();
        out.close();
    }

/****************************************Template Begins************************************************/
/***************************************Precision Printing**********************************************/
    static void printPrecision(double d){
        DecimalFormat ft = new DecimalFormat("0.00000000000000000");
        out.println(ft.format(d));
    }
/******************************************Graph*********************************************************/
    static void Makegraph(int n){
        graph=new HashSet[n];
        for(int i=0;i<n;i++){
            graph[i]=new HashSet<>();
        }
    }
    static void addEdge(int a,int b){
        graph[a].add(b);
    }
/*********************************************PAIR********************************************************/
    static class PairComp implements Comparator<Pair>{
        public int compare(Pair p1,Pair p2){
            if(p1.u>p2.u){
                return -1;
            }
            else if(p1.u<p2.u){
                return 81;
            }
            else{
                return p2.v-p1.v;
            }
        }
    }
    static class Pair implements Comparable<Pair> {
        int u;
        int v;
        int index=-1;
        public Pair(int u, int v) {
            this.u = u;
            this.v = v;
        }

        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }

        public boolean equals(Object o) {
            Pair other = (Pair) o;
            return u == other.u && v == other.v;
        }

        public int compareTo(Pair other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }

        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*************************************************Long Pair*************************************************/
    static class PairCompL implements Comparator<Pairl>{
        public int compare(Pairl p1,Pairl p2){
            if(p1.v>p2.v){
                return -1;
            }
            else if(p1.v<p2.v){
                return 1;
            }
            else{
                return 0;
            }
        }
    }
    static class Pairl implements Comparable<Pair> {
            long u;
            long v;
            int index=-1;
            public Pairl(long u, long v) {
                this.u = u;
                this.v = v;
            }

            public int hashCode() {
                int hu = (int) (u ^ (u >>> 32));
                int hv = (int) (v ^ (v >>> 32));
                return 31 * hu + hv;
            }

            public boolean equals(Object o) {
                Pair other = (Pair) o;
                return u == other.u && v == other.v;
            }

            public int compareTo(Pair other) {
                if(index!=other.index)
                    return Long.compare(index, other.index);
                return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
            }

            public String toString() {
                return "[u=" + u + ", v=" + v + "]";
            }
        }
/*****************************************DEBUG***********************************************************/
    public static void debug(Object... o) {
        System.out.println(Arrays.deepToString(o));
    }
/************************************MODULAR EXPONENTIATION***********************************************/
    static long modulo(long a,long b,long c) {
        long x=1;
        long y=a;
        while(b > 0){
            if(b%2 == 1){
                x=(x*y)%c;
            }
            y = (y*y)%c; // squaring the base
            b /= 2;
        }
        return  x%c;
    }
/********************************************GCD**********************************************************/
    static long gcd(long x, long y)
    {
        if(x==0)
            return y;
        if(y==0)
            return x;
        long r=0, a, b;
        a = (x > y) ? x : y; // a is greater number
        b = (x < y) ? x : y; // b is smaller number
        r = b;
        while(a % b != 0)
        {
            r = a % b;
            a = b;
            b = r;
        }
        return r;
    }
/******************************************SIEVE**********************************************************/
    static void sieveMake(int n){
        sieve=new boolean[n];
        Arrays.fill(sieve,true);
        sieve[0]=false;
        sieve[1]=false;
        for(int i=2;i*i<n;i++){
            if(sieve[i]){
                for(int j=i*i;j<n;j+=i){
                    sieve[j]=false;
                }
            }
        }
        primes=new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            if(sieve[i]){
                primes.add(i);
            }
        }
    }
/********************************************End***********************************************************/
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author beginner1010
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        boolean[][] visited;
        long[][] dp;

        long rec(int pos, int rem_weight, long[] cnt, int pack) {
            if (pos == 8) {
                return rem_weight == 0 ? 0 : -1;
            }

            if (visited[pos][rem_weight] == true) {
                return dp[pos][rem_weight];
            }
            visited[pos][rem_weight] = true;
            long res = -1;
            int used = 0;
            while (used <= cnt[pos] && used * (pos + 1) < pack && rem_weight - used * (pos + 1) >= 0) {
                long counter = ((cnt[pos] - used) * (pos + 1)) / pack;
                long cur_res = rec(pos + 1, rem_weight - used * (pos + 1), cnt, pack) + counter;
                if (cur_res != -1) {
                    assert (cur_res >= 0);
                    res = Math.max(res, cur_res + counter);
                }

                used += 1;
            }

            dp[pos][rem_weight] = res;
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long W = in.nextLong();
            long[] cnt = new long[8];
            for (int i = 0; i < 8; i++) {
                cnt[i] = in.nextLong();
            }
            int pack = 840;
            visited = new boolean[8][8 * pack + 1];
            dp = new long[8][8 * pack + 1];

            long ans = 0;
            for (int i = 0; i <= Math.min(8 * pack, W); i++) {
                long res = rec(0, i, cnt, pack);
                long bound = Math.min((W - res) / pack, res);
                ans = Math.max(ans, bound * pack + i);
            }
            out.println(ans);
            return;
        }

    }

    static class InputReader {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputStream stream;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public long nextLong() {
            int c = read();
            while (isWhitespace(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isWhitespace(c));
            return res * sgn;
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author beginner1010
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        boolean[][] visited;
        long[][] dp;

        long rec(int pos, int rem_weight, long[] cnt, int pack) {
            if (pos == 8) {
                return rem_weight == 0 ? 0 : -1;
            }

            if (visited[pos][rem_weight] == true) {
                return dp[pos][rem_weight];
            }
            visited[pos][rem_weight] = true;
            long res = -1;
            int used = 0;
            while (used <= cnt[pos] && used * (pos + 1) < pack && rem_weight - used * (pos + 1) >= 0) {
                long counter = ((cnt[pos] - used) * (pos + 1)) / pack;
                long cur_res = rec(pos + 1, rem_weight - used * (pos + 1), cnt, pack) + counter;
                if (cur_res != -1) {
                    assert (cur_res >= 0);
                    res = Math.max(res, cur_res + counter);
                }
                used++;
            }

            dp[pos][rem_weight] = res;
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long W = in.nextLong();
            long[] cnt = new long[8];
            for (int i = 0; i < 8; i++) {
                cnt[i] = in.nextLong();
            }
            int pack = 840;
            visited = new boolean[8][8 * pack + 1];
            dp = new long[8][8 * pack + 1];

            long ans = 0;
            for (int i = 0; i <= Math.min(8 * pack, W); i++) {
                long res = rec(0, i, cnt, pack);
                long bound = Math.min((W - i) / pack, res);
                ans = Math.max(ans, bound * pack + i);
            }
            out.println(ans);
            return;
        }

    }

    static class InputReader {
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputStream stream;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public long nextLong() {
            int c = read();
            while (isWhitespace(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isWhitespace(c));
            return res * sgn;
        }

    }
}

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		//BufferedReader in=new BufferedReader(new FileReader("cowxor.in"));
		//PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("cowxor.out")));
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		StringTokenizer sb=new StringTokenizer(in.readLine());
		long w=Long.parseLong(sb.nextToken());
		long[] cnt=new long[9];
		long[] cc=new long[9];
		sb=new StringTokenizer(in.readLine());
		for (int i=1;i<=8;++i) cnt[i]=Long.parseLong(sb.nextToken());
		long res=0;
		for (int val=10000;val<=50000;val+=9999) {
			long add=0,ww=w;
			for (int i=1;i<=8;++i) cc[i]=cnt[i];
			while (ww>val) {
				for (int i=8;i>=1;--i) if (ww>val) {
					long num=Math.min(((ww-val)/i+1),cc[i]);
					long tmp=num*i;
					cc[i]-=num;
					add+=tmp;
					ww-=tmp;
				}
			}
			if (ww>val) {
				res=Math.max(res,add);
				continue;
			}
			boolean[] dp=new boolean[(int)ww+1];
			dp[0]=true;
			for (int i=1;i<=8;++i) {
				long num=cnt[i];
				for (int j=0;j<20;++j) {
					if (num<=(1<<j)) break;
					num-=(1<<j);
					for (int k=(int)ww;k>=(1<<j)*i;--k)
						dp[k]|=dp[k-i*(1<<j)];
				}
				for (int k=(int)ww;k>=num*i;--k)
					dp[k]|=dp[k-(int)num*i];
			}
			for (int i=(int)ww;i>=0;--i)
				if (dp[i]) {
					res=Math.max(res,add+i);
					break;
				}
		}
		out.println(res);
		out.close();
	}
}
/*
39402932710920212
0 0 0 7036655838029546 0 2597660979346835 0 0
*/

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		StringTokenizer sb=new StringTokenizer(in.readLine());
		long w=Long.parseLong(sb.nextToken());
		long[] cnt=new long[9];
		sb=new StringTokenizer(in.readLine());
		long tot=0;
		for (int i=1;i<=8;++i) {
			cnt[i]=Long.parseLong(sb.nextToken());
			tot+=cnt[i]/(840/i);
			cnt[i]%=840/i;
		}
		int maxW=840*8;
		if (w<maxW) maxW=(int)w;
		boolean[] dp=new boolean[maxW+1];
		dp[0]=true;
		long res=0;
		for (int i=1;i<=8;++i) {
			long num=cnt[i];
			for (int j=0;j<20;++j) {
				if (num<=(1<<j)) break;
				num-=(1<<j);
				for (int k=maxW;k>=(1<<j)*i;--k)
					dp[k]|=dp[k-i*(1<<j)];
			}
			for (int k=maxW;k>=num*i;--k)
				dp[k]|=dp[k-(int)num*i];
		}
		for (int i=0;i<=maxW;++i) if (dp[i])
			res=Math.max(res,i+Math.min((w-i)/840,tot)*840);
		out.println(res);
		out.close();
	}
}
/*
39402932710920212
0 0 0 7036655838029546 0 2597660979346835 0 0
*/

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		//BufferedReader in=new BufferedReader(new FileReader("cowxor.in"));
		//PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("cowxor.out")));
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		StringTokenizer sb=new StringTokenizer(in.readLine());
		long w=Long.parseLong(sb.nextToken());
		long add=0;
		long[] cnt=new long[9];
		sb=new StringTokenizer(in.readLine());
		for (int i=1;i<=8;++i) cnt[i]=Long.parseLong(sb.nextToken());
		while (w>50000) {
			for (int i=8;i>=1;--i) if (w>50000) {
				long tmp=Math.min(((w-50000)/i+1),cnt[i])*i;
				add+=tmp;
				w-=tmp;
			}
		}
		if (w>50000) {
			System.out.println(add);
			return;
		}
		boolean[] dp=new boolean[(int)w+1];
		dp[0]=true;
		for (int i=1;i<=8;++i) {
			long num=cnt[i];
			for (int j=0;j<20;++j) {
				if (num<=(1<<j)) break;
				num-=(1<<j);
				for (int k=(int)w;k>=(1<<j)*i;--k)
					dp[k]|=dp[k-i*(1<<j)];
			}
			for (int k=(int)w;k>=num*i;--k)
				dp[k]|=dp[k-(int)num*i];
		}
		for (int i=(int)w;i>=0;--i)
			if (dp[i]) {
				out.println(add+i);
				break;
			}
		out.close();
	}
}

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		//BufferedReader in=new BufferedReader(new FileReader("cowxor.in"));
		//PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("cowxor.out")));
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		StringTokenizer sb=new StringTokenizer(in.readLine());
		long w=Long.parseLong(sb.nextToken());
		long ww=w;
		long[] cnt=new long[9];
		sb=new StringTokenizer(in.readLine());
		for (int i=1;i<=8;++i) cnt[i]=Long.parseLong(sb.nextToken());
		while (w>50000) {
			for (int i=8;i>=1;--i) if (w>50000)
				w-=Math.min(((w-50000)/i+1),cnt[i])*i;
		}
		if (w>50000) {
			System.out.println(ww-w);
			return;
		}
		boolean[] dp=new boolean[(int)w+1];
		dp[0]=true;
		for (int i=1;i<=8;++i) {
			long num=cnt[i];
			for (int j=0;j<20;++j) {
				if (num<=(1<<j)) break;
				num-=(1<<j);
				for (int k=(int)w;k>=(1<<j)*i;--k)
					dp[k]|=dp[k-i*(1<<j)];
			}
			for (int k=(int)w;k>=num*i;--k)
				dp[k]|=dp[k-(int)num*i];
		}
		for (int i=(int)w;i>=0;--i)
			if (dp[i]) {
				out.println(ww-i+w);
				break;
			}
		out.flush();
	}
}

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException {
		//BufferedReader in=new BufferedReader(new FileReader("cowxor.in"));
		//PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter("cowxor.out")));
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
		StringTokenizer sb=new StringTokenizer(in.readLine());
		long w=Long.parseLong(sb.nextToken());
		long add=0;
		long[] cnt=new long[9];
		sb=new StringTokenizer(in.readLine());
		for (int i=1;i<=8;++i) cnt[i]=Long.parseLong(sb.nextToken());
		while (w>50000) {
			for (int i=8;i>=1;--i) if (w>50000) {
				long num=Math.min(((w-50000)/i+1),cnt[i]);
				long tmp=num*i;
				cnt[i]-=num;
				add+=tmp;
				w-=tmp;
			}
		}
		if (w>50000) {
			System.out.println(add);
			return;
		}
		boolean[] dp=new boolean[(int)w+1];
		dp[0]=true;
		for (int i=1;i<=8;++i) {
			long num=cnt[i];
			for (int j=0;j<20;++j) {
				if (num<=(1<<j)) break;
				num-=(1<<j);
				for (int k=(int)w;k>=(1<<j)*i;--k)
					dp[k]|=dp[k-i*(1<<j)];
			}
			for (int k=(int)w;k>=num*i;--k)
				dp[k]|=dp[k-(int)num*i];
		}
		for (int i=(int)w;i>=0;--i)
			if (dp[i]) {
				out.println(add+i);
				break;
			}
		out.close();
	}
}

import java.util.*;

public class Main {
    private static long w, cnt[], all;

    private static int solveDp() {
        int dp[] = new int[1010];
        dp[0] = 1;
        for (int i = 1; i <= 8; i++) {
            for (int j = 0; j < Math.min(cnt[i], 1000); j++) {
                for (int k = 0; k < 1000; k++) {
                    if (dp[k] == 1) {
                        dp[k + i] = 1;
                    }
                }
            }
        }
        for(int i = (int) w; i >= 0; i--) {
            if (dp[i] == 1) {
                return i;
            }
        }
        return 0;
    }

    private static void reduce() {
        for (int i = 1; i <= 8; i++) {
            if (cnt[i] > 1000) {
                long toTake = cnt[i] - 20;
                if (w - toTake * i >= 1000) {
                    w -= toTake * i;
                    cnt[i] = 20;
                } else {
                    long diff = w - 1000;
                    long take = diff / i + 1;
                    cnt[i] -= take;
                    w -= take * i;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        w = input.nextLong();
        cnt = new long[9];
        for (int i = 1; i <= 8; i++) {
            cnt[i] = input.nextLong();
            all += 1L * i * cnt[i];
        }
        if (all <= w) {
            System.out.println(all);
            return;
        }
        long oldW = w;
        if (w > 1000) {
            reduce();
        }
        System.out.println(oldW - w + solveDp());
    }
}

import java.math.*;
import java.util.*;
//import javafx.util.*;
import java.io.*;

public class Prg10 {

    PrintWriter pw = new PrintWriter(System.out);
    Random rnd = new Random();
    int a;
    long o =0, s;

    void run() {
        //a = ni();
        s = nl();
        long sum=0;
        long[] m = new long[8];
        for(int q=0; q<8; q++) {
            m[q] = nl();
            sum+=m[q]*(q+1);
        }
        System.out.println(sum-s);
        if(sum<=s){
            System.out.println(sum);
            return;
        }
        rec(m, 0);
        pw.print(o);
        pw.flush();
    }

    void rec(long[] m, long cur){
        if(o==s) return;
        o = Math.max(o, cur);
        for(int q=0; q<8; q++){
            if((s-cur)%(q+1)>0) continue;
            long need = (s-cur)/(q+1);
            if(m[q]>=need){
                o = s;
                return;
            }
        }
        for(long q=7; q>=0; q--){
            if(m[(int)q]==0) continue;
            if(o==s) return;
            long can = Math.min(m[(int)q], (s-cur)/(q+1));
            for(long w=Math.max(0, can-10), i=0; i<10 && w<=can; w++, i++){
                m[(int)q]-=w;
                rec(m, cur+(q+1)*w);
                m[(int)q]+=w;
            }
        }
    }

    static class PyraSort {

        private static int heapSize;

        public static void sort(long[] a) {
            buildHeap(a);
            while (heapSize > 1) {
                swap(a, 0, heapSize - 1);
                heapSize--;
                heapify(a, 0);
            }
        }

        private static void buildHeap(long[] a) {
            heapSize = a.length;
            for (int i = a.length / 2; i >= 0; i--) {
                heapify(a, i);
            }
        }

        private static void heapify(long[] a, int i) {
            int l = 2 * i + 2;
            int r = 2 * i + 1;
            int largest = i;
            if (l < heapSize && a[i] < a[l]) {
                largest = l;
            }
            if (r < heapSize && a[largest] < a[r]) {
                largest = r;
            }
            if (i != largest) {
                swap(a, i, largest);
                heapify(a, largest);
            }
        }

        private static void swap(long[] a, int i, int j) {
            a[i] ^= a[j] ^= a[i];
            a[j] ^= a[i];
        }
    }

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Prg10().run();
            }
        }, "1", 1 << 28).start();
    }

    InputStream is = System.in;
    private byte[] inbuf = new byte[1<<16];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte()
    {
        if(lenbuf == -1)throw new InputMismatchException();
        if(ptrbuf >= lenbuf){
            ptrbuf = 0;
            try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
            if(lenbuf <= 0)return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
    private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

    private double nd() { return Double.parseDouble(ns()); }
    private char nc() { return (char)skip(); }

    private String ns()
    {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!(isSpaceChar(b))){
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private String nline(){
        int b = readByte();
        StringBuilder sb = new StringBuilder();
        while (b!=10) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n)
    {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while(p < n && !(isSpaceChar(b))){
            buf[p++] = (char)b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m)
    {
        char[][] map = new char[n][];
        for(int i = 0;i < n;i++)map[i] = ns(m);
        return map;
    }

    private int[] na(int n)
    {
        int[] a = new int[n];
        for(int i = 0;i < n;i++)a[i] = ni();
        return a;
    }

    private int ni()
    {
        int num = 0, b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl()
    {
        long num = 0;
        int b;
        boolean minus = false;
        while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if(b == '-'){
            minus = true;
            b = readByte();
        }
        while(true){
            if(b >= '0' && b <= '9'){
                num = num * 10 + (b - '0');
            }else{
                return minus ? -num : num;
            }
            b = readByte();
        }
    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EKnapsack solver = new EKnapsack();
        solver.solve(1, in, out);
        out.close();
    }

    static class EKnapsack {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            long w = in.scanLong();
            long arr[] = new long[8 + 1];
            for (int i = 1; i < 8; i++) arr[i] = in.scanLong();
            long dp[][] = new long[9][(840 * 8) + 1];
            for (int i = 0; i <= 8; i++) Arrays.fill(dp[i], Long.MIN_VALUE / 2);

            dp[0][0] = 0;
            for (int i = 1; i <= 8; i++) {
                for (int sum = 0; sum <= 8 * 840; sum++) {
                    for (int count = 0; count <= Math.min(arr[i], 840 / i); count++) {
                        if (sum - (count * i) >= 0)
                            dp[i][sum] = Math.max(dp[i][sum], dp[i - 1][sum - (count * i)] + ((arr[i] - count) / (840 / i)));
                    }
                }
            }

            long ans = 0;

            for (int sum = 0; sum <= 8 * 840; sum++) {
                if (sum <= w) {
                    ans = Math.max(ans, sum + (840 * Math.min(dp[8][sum], (w - sum) / 840)));
                }
            }

            out.println(ans);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

        public long scanLong() {
            long I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EKnapsack solver = new EKnapsack();
        solver.solve(1, in, out);
        out.close();
    }

    static class EKnapsack {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            long w = in.scanLong();
            long arr[] = new long[8 + 1];
            for (int i = 1; i <= 8; i++) arr[i] = in.scanLong();
            long dp[][] = new long[9][(840 * 8) + 1];
            for (int i = 0; i <= 8; i++) Arrays.fill(dp[i], Long.MIN_VALUE / 2);

            dp[0][0] = 0;
            for (int i = 1; i <= 8; i++) {
                for (int sum = 0; sum <= 8 * 840l; sum++) {
                    for (int count = 0; count <= Math.min(arr[i], 840 / i); count++) {
                        if (sum - (count * i) >= 0 && dp[i - 1][sum - (count * i)] != Long.MIN_VALUE / 2) {
                            dp[i][sum] = Math.max(dp[i][sum], dp[i - 1][sum - (count * i)] + ((arr[i] - count) / (840 / i)));
                        }
                    }
                }
            }

            long ans = 0;

            for (int sum = 0; sum <= 8 * 840; sum++) {
                if (sum <= w && dp[8][sum] != Long.MAX_VALUE / 2) {
                    ans = Math.max(ans, (long) sum + (840 * Math.min(dp[8][sum], (w - sum) / 840)));
                }
            }

            out.println(ans);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

        public long scanLong() {
            long I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

    }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        EKnapsack solver = new EKnapsack();
        solver.solve(1, in, out);
        out.close();
    }

    static class EKnapsack {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            long w = in.scanLong();
            long arr[] = new long[8 + 1];
            for (int i = 1; i < 8; i++) arr[i] = in.scanLong();
            long dp[][] = new long[9][(840 * 8) + 1];
            for (int i = 0; i <= 8; i++) Arrays.fill(dp[i], Long.MIN_VALUE / 2);

            dp[0][0] = 0;
            for (int i = 1; i <= 8; i++) {
                for (int sum = 0; sum <= 8 * 840; sum++) {
                    for (int count = 0; count <= Math.min(arr[i], 840 / i); count++) {
                        if (sum - (count * i) >= 0)
                            dp[i][sum] = Math.max(dp[i][sum], dp[i - 1][sum - (count * i)] + ((arr[i] - count) / (840 / i)));
                    }
                }
            }

            long ans = 0;

            for (int sum = 0; sum <= 8 * 840; sum++) {
                if (sum <= w && dp[8][sum] >= 0) {
                    ans = Math.max(ans, sum + (840 * Math.min(dp[8][sum], (w - sum) / 840)));
                }
            }

            out.println(ans);
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

        public long scanLong() {
            long I = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    I *= 10;
                    I += n - '0';
                    n = scan();
                }
            }
            return neg * I;
        }

    }
}