#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define INF 100000000 #define EPS 1e-9 using namespace std; typedef pair P; typedef long long ll; #define MAX_N 15 #define MAX_T 10000 int T, N; int c[MAX_N], v[MAX_N]; int cc[MAX_N * 10], vv[MAX_N * 10]; int dp[MAX_N * 10][MAX_T]; int main() { cin >> T; cin >> N; for (int i = 0; i < N; i++){ cin >> c[i]; } for (int i = 0; i < N; i++){ cin >> v[i]; } int v_count = 0; for (int i = 0; i < N; i++){ int two = 0; while (v[i] >= (int)pow(2,two) ){ vv[v_count] = (int)(v[i] / (int)pow(2, two)); cc[v_count] = c[i]; v_count++; two++; } } int ans = 0; for (int i = 0; i < v_count; i++){ for (int j = 0; j <= T; j++){ if (j - cc[i] >= 0) dp[i + 1][j] = max(dp[i][j], dp[i][j - cc[i]] + vv[i]); else dp[i + 1][j] = dp[i][j]; ans = max(ans, dp[i + 1][j]); } } cout << ans << endl; return 0; }