import sys input = sys.stdin.readline T = int(input()) N = int(input()) c = list(map(int, input().split())) v = list(map(int, input().split())) dp = [0] * (T+1) for i in range(N): for j in range(T)[::-1]: k = 1 x = v[i] s = x while j + k*c[i] <= T and x > 0: dp[j+k*c[i]] = max(dp[j+k*c[i]], dp[j] + s) x //= 2 s += x k += 1 print(dp[T])