import java.util.*; import java.io.*; public class Main { static int[][] dp; static int[] costs; static int[] values; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int t = 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][t + 1]; System.out.println(dfw(n - 1, t)); } static int dfw(int idx, int t) { if (t < 0) { return Integer.MIN_VALUE; } if (idx < 0) { return 0; } if (dp[idx][t] == 0) { int time = costs[idx]; int atract = values[idx]; int sum = atract; dp[idx][t] = dfw(idx - 1, t); while (time <= t && atract > 0) { dp[idx][t] = Math.max(dp[idx][t], dfw(idx - 1, t - time) + sum); time += costs[idx]; atract /= 2; sum += atract; } } 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 nextLine() throws Exception { return br.readLine(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }