import java.util.*; public class Main { static int[] costs; static int[] values; static int[][] dp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); 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]; for (int[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(n - 1, t)); } static int dfw(int idx, int time) { if (time < 0) { return Integer.MIN_VALUE; } if (idx < 0) { return 0; } if (dp[idx][time] < 0) { int v = values[idx]; int t = time; int sum = 0; while (t >= 0) { dp[idx][time] = Math.max(dp[idx][time], dfw(idx - 1, t) + sum); sum += v; v /= 2; t -= costs[idx]; } } return dp[idx][time]; } }