import java.util.*; public class Main { static int t; static int n; static int[] tArr; static int[] vArr; static int[][] dp; public static void main (String[] args) { Scanner sc = new Scanner(System.in); t = sc.nextInt(); n = sc.nextInt(); tArr = new int[n + 1]; for (int i = 1; i <= n; i++) { tArr[i] = sc.nextInt(); } vArr = new int[n + 1]; for (int i = 1; i <= n; i++) { vArr[i] = sc.nextInt(); } dp = new int[n + 1][t + 1]; System.out.println(dfw(n, t)); } static int dfw(int idx, int time) { if (idx == 0) { return 0; } if (dp[idx][time] != 0) { return dp[idx][time]; } int value = vArr[idx]; int total = 0; for (int i = 0; i * tArr[idx] <= time && value > 0; i++) { dp[idx][time] = Math.max(dp[idx][time], total + dfw(idx - 1, time - i * tArr[idx])); total += value; value /= 2; } return dp[idx][time]; } }