import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) T = I() N = I() C = LI() V = LI() NC = [] NV = [] for i in range(N): c = C[i] v = V[i] while v: NC.append(c) NV.append(v) v //= 2 N = len(NC) dp = [[0]*(T+1) for _ in range(N+1)] for i in range(1,N+1): c = NC[i-1] v = NV[i-1] for j in range(T+1): if j >= c: dp[i][j] = max(dp[i-1][j],dp[i-1][j-c]+v) else: dp[i][j] = dp[i-1][j] print(dp[-1][-1])