import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main_yukicoder37 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int n = sc.nextInt(); int[] c = new int[n]; int[] v = new int[n]; for (int i = 0; i < n; i++) { c[i] = sc.nextInt(); } for (int i = 0; i < n; i++) { v[i] = sc.nextInt(); } int[] dp = new int[t + 1]; for (int i = 1; i <= n; i++) { List cc = new ArrayList<>(); List vv = new ArrayList<>(); int tmpv = v[i - 1]; int tmpc = c[i - 1]; int sumv = tmpv; while (tmpv > 0) { cc.add(tmpc); vv.add(sumv); tmpc += c[i - 1]; tmpv /= 2; sumv += tmpv; } int m = cc.size(); for (int j = t; j > 0; j--) { for (int k = 0; k < m; k++) { int tmpvv = vv.get(k); int tmpcc = cc.get(k); if (j - tmpcc >= 0) { dp[j] = Math.max(dp[j], dp[j - tmpcc] + tmpvv); } } } } System.out.println(dp[t]); sc.close(); } }