#include #include using namespace std; #define FOR(i, s, e) for (int i = (s); i <= (e); i++) int T; int N; int c[20]; long long v[20]; vector Vs[20]; long long dp[20][20000]; int dp_cou[20][20000]; int main() { cin >> T; cin >> N; FOR(i, 1, N) { cin >> c[i]; } FOR(i, 1, N) { cin >> v[i]; while (v[i] > 0) { Vs[i].push_back(v[i]); v[i] /= 2; } Vs[i].push_back(0); } long long result = 0; for (int i = 1; i <= N; i++) { for (int j = 0; j <= T; j++) { dp[i][j] = max(dp[i][j] , dp[i - 1][j]); { if (j + c[i] <= T && dp[i][j + c[i]] <= dp[i - 1][j] + Vs[i][0]) { dp[i][j + c[i]] = dp[i - 1][j] + Vs[i][0]; dp_cou[i][j + c[i]] = min(1, (int)Vs[i].size() - 1); } } if (j + c[i] <= T && dp[i][j + c[i]] < dp[i][j] + Vs[i][dp_cou[i][j]]) { dp[i][j + c[i]] = dp[i][j] + Vs[i][dp_cou[i][j]]; dp_cou[i][j + c[i]] = min(dp_cou[i][j] + 1, (int)Vs[i].size() - 1); } result = max(result, dp[i][j]); } } cout << result << endl; return 0; }