#!/usr/bin/env python3.8 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np # %% T = int(readline()) N = int(readline()) C = tuple(map(int, readline().split())) V = tuple(map(int, readline().split())) # %% dp = np.zeros(T + 1, np.int64) for c, v in zip(C, V): newdp = dp.copy() total_c = 0 total_v = 0 while v: total_c += c total_v += v v //= 2 if total_c > T: break np.maximum(newdp[total_c:], dp[:-total_c] + total_v, out=newdp[total_c:]) dp = newdp # %% print(dp.max())