T = int(input()) N = int(input()) c = list(map(int,input().split())) v = list(map(int,input().split())) for i in range(N): x = v[i] // 2 while x: c.append(c[i]) v.append(x) x //= 2 N = len(v) DP = [[0] * (T+10) for _ in range(N+10)] for i in range(N): for j in range(T+1): DP[i+1][j] = max(DP[i][j], DP[i+1][j]) DP[i][j+1] = max(DP[i][j], DP[i][j+1]) if j - c[i] >= 0: DP[i+1][j] = max(DP[i][j-c[i]] + v[i], DP[i+1][j]) print(DP[N][T])