#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vs = vector; using pii = pair; using pll = pair; using pdd = pair; using vpii = vector; using vpll = vector; using vpdd = vector; const int inf = (1 << 30) - 1; const ll INF = 1LL << 60; //const int MOD = 1000000007; const int MOD = 998244353; int main() { int t, n; cin >> t >> n; vi c(n), v(n); for (int i = 0; i < n; i++) { cin >> c[i]; } for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n; i++) { int x = v[i] / 2; while (x > 0) { v.push_back(x); c.push_back(c[i]); x /= 2; } } int imax = v.size(); vi dp(t + 1, 0); for (int i = 0; i < imax; i++) { vi tmp = dp; for (int j = t; j >= 0; j--) { if (j >= c[i]) dp[j] = max(tmp[j], tmp[j - c[i]] + v[i]); } } cout << dp[t] << endl; return 0; }