#include using namespace std; //#define int long long #define FOR(i, j, k) for(int i = j; i < (int)k; ++i) #define rep(i, j) FOR(i, 0, j) #define INF (1 << 30) typedef unsigned long long ull; typedef pair P; typedef pair Pi; const int MOD = 1e9 + 7; const int dy[] = {0, 0, 1, -1}; const int dx[] = {1, -1, 0, 0}; template void chmin(T& a, const T& b) { a = min(a, b); } template void chmax(T& a, const T& b) { a = max(a, b); } int dp[16][10001]; vector c, v; signed main() { cin.tie(0); ios::sync_with_stdio(false); int t, n; cin >> t >> n; int tmp[100]; rep(i, n) cin >> tmp[i]; rep(i, n) { int a; cin >> a; while(a > 0) { v.push_back(a); c.push_back(tmp[i]); a /= 2; } } rep(i, c.size()) rep(j, t + 1) { if(j < c[i]) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j], dp[i][j - c[i]] + v[i]); } } cout << dp[c.size()][t] << endl; return 0; }