t = int(input()) n = int(input()) C = list(map(int, input().split())) V = list(map(int, input().split())) CV = [[C[i], V[i]] for i in range(n)] CV.sort(key=lambda x: (x[0], x[1])) DP = [0 for _ in range(t + 1)] for i in range(n): NDP = [-1 for _ in range(t + 1)] c, v = CV[i] for j in range(t + 1): NDP[j] = max(NDP[j], DP[j]) res = v curr = v for k in range(j + c, t + 1, c): NDP[k] = max(NDP[k], DP[j] + res) curr //= 2 if curr == 0: break res += curr for j in range(1, t + 1): NDP[j] = max(NDP[j], NDP[j - 1]) DP = NDP print(DP[t])