#include using namespace std; int main () { int N, C; cin >> N >> C; int L[N], W[N]; for (int i = 0; i < N; i++) cin >> L[i]; for (int i = 0; i < N; i++) cin >> W[i]; int dp[2][51][51][51] = {}; int cur = 0, prev = 1; int ans = 0; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < N; k++) { int a = L[i], b = L[j], c = L[k], cost = W[i]+W[j]+W[k], sum = L[i]+L[j]+L[k]; if (a != c && ((a < b && c < b) || (a > b && c > b)) && cost <= C) { dp[prev][b][c][cost] = max(dp[prev][b][c][cost], sum); ans = max(ans, sum); } } for (int x = 0; x <= C; x++) { for (int a = 1; a <= 50; a++) for (int b = 1; b <= 50; b++) { if (a == b) continue; for (int i = 0; i < N; i++) { // a, b, L[i] if ((a < b && L[i] < b && a != L[i]) || (a > b && L[i] > b && a != L[i])) for (int k = 0; k <= C-W[i]; k++) { if (dp[prev][a][b][k] > 0) { int sum = dp[prev][a][b][k] + L[i]; dp[cur][b][L[i]][k+W[i]] = max(dp[cur][b][L[i]][k+W[i]], sum); ans = max(ans, sum); } } } } swap(cur, prev); } cout << ans << endl; }