from collections import * from itertools import * from functools import * from heapq import * import sys,math 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+2) for _ in range(N+1)] for i in range(N): v = V[i] c = C[i] for j in range(T+1): dp[i+1][j] = max(dp[i][j],dp[i+1][j]) tv = v sv = tv tj = j while tv: dp[i+1][min(tj+c,T+1)] = max(dp[i+1][min(tj+c,T+1)], dp[i][min(j,T+1)] + sv) tv //= 2 sv += tv tj += c ans = max(dp[-1][:T+1]) print(ans)