using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Text.RegularExpressions; using System.Linq; using System.IO; class Program { static void Main() { new Magatro().Solve(); } } class Magatro { private int T, N; private int[] C, V; private void Scan() { T = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); C = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); V = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); } private int[] F(int n) { List result = new List(); result.Add(0); while (n > 0) { result.Add(result.Last() + n); n /= 2; } return result.ToArray(); } public void Solve() { Scan(); var dp = (new int[T + 1]).Select(i => -1).ToArray(); dp[0] = 0; for (int i = 0; i < N; i++) { var l = F(V[i]); for (int j = T; j >= 0; j--) { if (dp[j] == -1) continue; for (int k = 0; k < l.Length; k++) { int next = j + C[i] * k; if (next > T) continue; dp[next] = Math.Max(dp[next], dp[j] + l[k]); } } } Console.WriteLine(dp.Max()); } }