import math T=int(input()) N=int(input()) C=list(map(int,input().split())) V=list(map(int,input().split())) Z=list(zip(C,V)) Z2=Z[:] O=0 for z in Z: c=z[0] v=z[1] while True: v=math.floor(v/2) if v<=0: break Z2.append((c,v)) A=[[0]*(T+1) for i in range(len(Z2)+1)] for i in range(1,len(Z2)+1): for t in range(1,T+1): z=Z2[i-1] c=z[0] v=z[1] if t-c<0: A[i][t]=A[i-1][t] else: A[i][t]=max(A[i-1][t-c]+v,A[i-1][t]) O=max(max(A[i]),O) print(O)