import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines t=int(readline()) n=int(readline()) c=list(map(int, readline().split())) v=list(map(int, readline().split())) dp=[0]*(t+1) for x, y in zip(c, v): while y: newdp=dp[:] for i, z in enumerate(dp): if i+x>t: break newv=z+y if newdp[i+x]>=1 print(dp[t])