#include using namespace std; typedef long long ll; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--) #define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); const ll inf = LONG_LONG_MIN / 2 - 1; ll t, n; cin >> t >> n; vector c(n), v(n); rep(i, n) cin >> c[i]; rep(i, n) cin >> v[i]; vector dp(t + 1, inf); dp[0] = 0; rep(i, n) { while(v[i] > 0) { rreps(j, c[i], t + 1) { dp[j] = max(dp[j], dp[j - c[i]] + v[i]); } v[i] /= 2; } } cout << *max_element(all(dp)) << endl; return 0; }