import java.io.*; import java.util.*; public class Main { static int[] costs; static int[] values; static int[][] dp; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int time = sc.nextInt(); int n = sc.nextInt(); costs = new int[n]; for (int i = 0; i < n; i++) { costs[i] = sc.nextInt(); } values = new int[n]; for (int i = 0; i < n; i++) { values[i] = sc.nextInt(); } dp = new int[n][time + 1]; for (int[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(n - 1, time)); } static int dfw(int idx, int t) { if (t < 0) { return Integer.MIN_VALUE; } if (idx < 0) { return 0; } if (dp[idx][t] < 0) { dp[idx][t] = dfw(idx - 1, t); int sum = 0; int rate = 1; int total = 0; while (sum <= t && rate <= values[idx]) { sum += costs[idx]; total += values[idx] / rate; rate *= 2; dp[idx][t] = Math.max(dp[idx][t], dfw(idx - 1, t - sum) + total); } } return dp[idx][t]; } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }