#include #include #include using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000 int main(){ int T,N; cin>>T>>N; vector c,v; { vector x(N),y(N); rep(i,N)cin>>x[i]; rep(i,N)cin>>y[i]; rep(i,N){ int a,b; a = x[i],b = y[i]; while(b!=0){ c.push_back(a); v.push_back(b); b>>=1; } } } vector dp(T+1,0); rep(i,c.size()){ vector ndp(T+1,0); rep(j,T+1){ ndp[j] = max(ndp[j],dp[j]); if(j+c[i]<=T){ ndp[j+c[i]] = max(ndp[j+c[i]],dp[j] + v[i]); } } swap(dp,ndp); } int ans = 0; rep(i,T+1)ans = max(ans,dp[i]); cout<