#include #include using namespace std; #define FOR(i,s,e) for(int i = (s);i <= (e);i++) int T; int N; int c[20]; int v[20]; vector Vs[20]; int 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); } int result = 0; for(int i = 1;i <= N;i++) { for(int j = 0;j <= T;j++) { if(j >= 1) { if(dp[i][j] < dp[i - 1][j]) { dp[i][j] = dp[i - 1][j]; dp_cou[i][j] = 0; } } 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; }