import sys input = sys.stdin.readline def main(): T = int(input()) N = int(input()) *C, = map(int, input().split()) *V, = map(int, input().split()) DP = [0]*(T+1) for i in range(N): c, v = C[i], V[i] while v > 0: for i in range(T-c, -1, -1): DP[i+c] = max(DP[i+c], DP[i]+v) v //= 2 print(DP[-1]) if __name__ == '__main__': main()