#include #include #include #include #include #include #include #include #include #include #include const int MOD = 1e9 + 7; const int iINF = 1000000000; const long long int llINF = 1000000000000000000; #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long int; using P = pair; using edge = struct { int to; int cost; }; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int T, N; int dp[20][10000]; int main() { cin >> T >> N; vector c(N), v(N); rep(i, N) cin >> c[i]; rep(i, N) cin >> v[i]; memset(dp, -1, sizeof(dp)); dp[0][0] = 0; for (int i = 0; i < N; i++) { rep(j, T + 1) { dp[i + 1][j] = dp[i][j]; int sum = 0; for(int k = v[i], cnt = 1; k > 0; k /= 2, cnt++) { sum += k; if(j - c[i] * cnt >= 0 && dp[i][j - c[i] * cnt] != -1) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - c[i] * cnt] + sum); } } } } int ans = 0; rep(i, T + 1) ans = max(ans, dp[N][i]); cout << ans << endl; return 0; }