T=int(raw_input()) N=int(raw_input()) cs = map(int,raw_input().split()) vs= map(int,raw_input().split()) dp = [-1 for i in range(10001)] dp[0]=0 for i in range(N): for j in range(10000,-1,-1): if dp[j]>=0: c=cs[i] v=vs[i] nt = j+c nv = dp[j]+v while nt <= T and v != 0: dp[nt] = max(dp[nt],nv) v/=2 nt+=c nv+=v print max(dp)