import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int T = sc.nextInt(); final int N = sc.nextInt(); int[] cs = new int[N]; int[] vs = new int[N]; for(int i = 0; i < N; i++){ cs[i] = sc.nextInt(); } for(int i = 0; i < N; i++){ vs[i] = sc.nextInt(); } int[] DP = new int[T + 1]; Arrays.fill(DP, -1); DP[0] = 0; for(int i = 0; i < N; i++){ for(int j = T; j >= 0; j--){ for(int value = vs[i], sum_value = vs[i], time = j + cs[i]; value > 0 && time <= T; value /= 2, sum_value += value, time += cs[i]){ DP[time] = Math.max(DP[time], DP[j] + sum_value); } } } System.out.println(Arrays.stream(DP).max().getAsInt()); } }