using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; class Template { static Scanner sc; public static void Main(string[] args) { sc = new Scanner(); int T = sc.nextInt(); int N = sc.nextInt(); var c = sc.nextIntArray(); var v = sc.nextIntArray(); int[][] dp = new int[N + 1][]; for (int i = 0; i <= N; i++) { dp[i] = Array.ConvertAll(new int[T * 2], e => e = -1); } dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < T + 1; j++) { int vbuf = v[i]; int time = c[i]; int vsum = 0; dp[i + 1][j] = Math.Max(dp[i][j], dp[i + 1][j]); while (vbuf > 0) { vsum += vbuf; if (j - time >= 0 && dp[i][j - time] != -1) { dp[i + 1][j] = Math.Max(dp[i + 1][j], dp[i][j - time] + vsum); } vbuf /= 2; time += c[i]; } } } Console.WriteLine(dp[N].Max()); } } public class Scanner { public Scanner() { } public string next() { return Console.ReadLine(); } public int nextInt() { return int.Parse(next()); } public double nextDouble() { return double.Parse(next()); } public long nextLong() { return long.Parse(next()); } public string[] nextArray() { return next().Split(' '); } public int[] nextIntArray() { return Array.ConvertAll(nextArray(), e => int.Parse(e)); } public long[] nextLongArray() { return Array.ConvertAll(nextArray(), e => long.Parse(e)); } public double[] nextDoubleArray() { return Array.ConvertAll(nextArray(), e => double.Parse(e)); } }