#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) int T, N; struct A { int c, v; }; vector a; int main() { cin>>T>>N; REP(i,N) { A ai = A(); cin >> ai.c; a.push_back(ai); } REP(i,N) cin >> a[i].v; int dp[20001] = {0}; REP(i,N) { A ai = a[i]; int v = ai.v; int c = ai.c; while(v > 0) { RREP(j,10001) { dp[j+c] = max(dp[j+c], dp[j] + v); } v /= 2; } } cout << dp[T] << endl; return 0; }