#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef _MSC_VER #include #endif #define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define MEMSET(v, s) memset(v, s, sizeof(v)) #define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end()) #define MP make_pair #define MT make_tuple using namespace std; typedef long long ll; typedef long double ld; typedef pair P; int main(){ cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); int t, n; cin >> t >> n; vector w(n), v(n); rep(i, n) cin >> w[i]; rep(i, n) cin >> v[i]; rep(i, w.size()){ if (v[i] / 2){ w.push_back(w[i]); v.push_back(v[i] / 2); } } vector dp(t + 1); for (int i = 0; i < w.size(); ++i){ for (int j = t - w[i]; j >= 0; --j){ dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]); } } cout << dp[t] << endl; }