import sys sys.setrecursionlimit(10 ** 6) import numpy as np def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def main(): t=II() n=II() cc=LI() vv=LI() dp=np.zeros(t+1,dtype="i8") for c,v in zip(cc,vv): if c>t:continue while v: np.maximum(dp[t:c-1:-1],dp[t-c::-1]+v,out=dp[t:c-1:-1]) v>>=1 print(dp[t]) main()