T = int(input()) n = int(input()) C = list(map(int,input().split())) V = list(map(int,input().split())) dp = [0]*(T+1) for c,v in zip(C,V): for i in range(10): for t in range(T+1)[::-1]: if c+t <= T: dp[c+t] = max(dp[c+t],dp[t]+v) v //= 2 print(dp[-1])