T = int(input()) N = int(input()) C = tuple(map(int, input().split())) V = tuple(map(int, input().split())) dp = [0] * (T + 1) for c, v in zip(C, V): new_dp = dp.copy() u = 0 for n in range(v.bit_length()): u += v >> n if T < c * (n + 1) - 1: break for i in range(T - c * (n + 1) + 1): new_dp[i + c * (n + 1)] = max(new_dp[i + c * (n + 1)], dp[i] + u) dp = new_dp print(max(dp))