#include using namespace std; typedef long long int ll; typedef pair pii; typedef vector vi; typedef vector > vii; #define rrep(i, m, n) for(int (i)=(m); (i)<(n); (i)++) #define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++) #define rep(i, n) for(int (i)=0; (i)<(n); (i)++) #define rev(i, n) for(int (i)=(n)-1; (i)>=0; (i)--) #define vrep(i, c) for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++) #define ALL(v) (v).begin(), (v).end() #define mp make_pair #define pb push_back template inline void minup(T1& m, T2 x){ if(m>x) m=static_cast(x); } template inline void maxup(T1& m, T2 x){ if(m(x); } #define INF 1000000000 #define MOD 1000000007 #define EPS 1E-12 const int MAX_T = 10000; const int MAX_N = 15; int T, N; int c[MAX_N]; int v[MAX_N]; int dp[MAX_T+1]; int main() { cin >> T >> N; rep(i, N) cin >> c[i]; rep(i, N) cin >> v[i]; rep(i, N){ for(int w = v[i]; w > 0; w /= 2){ rev(j, T+1) if(j + c[i] <= T) maxup(dp[j + c[i]], dp[j] + w); } } cout << dp[T] << endl; return 0; }