#yuki37 t=int(raw_input()) n=int(raw_input()) c=map(int,raw_input().split()) v=map(int,raw_input().split()) dp=[0]*(t+1) for i in xrange(n): vi=v[i] while vi: ci=c[i] for j in xrange(t-ci,-1,-1): if dp[j]: dp[j+ci]=max(dp[j+ci],dp[j]+vi) if ci<=t: dp[ci]=max(dp[ci],vi) vi/=2 print max(dp)