T = int(input()) N = int(input()) C = list(map(int, input().split())) V = list(map(int, input().split())) cs = C[:] vs = V[:] for c, v in zip(C, V): v //= 2 while v > 0: cs.append(c) vs.append(v) v //= 2 dp = [0] * (T+1) for c, v in zip(cs, vs): for i in reversed(range(T)): if i+c > T: continue dp[i+c] = max(dp[i+c], dp[i] + v) ans = dp[T] print(ans)