t = int(input()) n = int(input()) c = list(map(int, input().split())) v = list(map(int, input().split())) dp = {} dp[0] = 0 for i in range(n): cost = c[i] value = 0 cur = v[i] ndp = {} while cur > 0: value += cur for ck, cv in dp.items(): ndp[ck] = max(ndp.get(ck, -1), cv) if ck + cost <= t: nxt = ck + cost ndp[nxt] = max(ndp.get(nxt, -1), cv + value) cost += c[i] cur //= 2 dp = ndp print(max(dp.values()))