T = int(input()) N = int(input()) C = list(map(int, input().split())) V = list(map(int, input().split())) dp = [0] * 20000 for i in range(N): while V[i] > 0: for j in range(T - 1, -1, -1): dp[j + C[i]] = max(dp[j + C[i]], dp[j] + V[i]) V[i] = V[i] // 2 ans = 0 for i in range(T + 1): ans = max(ans, dp[i]) print(ans)