#include int kado(int x, int y, int z) { if (x == y || x == z || y == z) return 0; return (x < y && y > z) || (x > y && y < z); } int N, C; int L[50], W[50]; int dp[51][51][51]; void to(int *x, int y) { if (*x < y) *x = y; } int main(void) { scanf("%d %d", &N, &C); for (int i = 0; i < N; i++) { scanf("%d", L + i); } for (int i = 0; i < N; i++) { scanf("%d", W + i); } for (int i = 0; i <= N; i++) { for (int j = 0; j <= C; j++) { for (int k = 0; k <= C; k++) { dp[i][j][k] = -1e9; } } } int ans = 0; for (int i = 0; i < N; i++) { if (W[i] <= C) { to(&ans, L[i]); } for (int j = 0; j < N; j++) { if (L[i] != L[j] && W[i] + W[j] <= C) { to(&dp[W[i] + W[j]][L[i]][L[j]], L[i] + L[j]); } } } for (int i = 1; i <= C; i++) { for (int j = 1; j <= 50; j++) { for (int k = 1; k <= 50; k++) { for (int l = 0; l < N; l++) { if (kado(j, k, L[l]) && i + W[l] <= C) { to(&dp[i + W[l]][k][L[l]], dp[i][j][k] + L[l]); } } } } } for (int i = 1; i <= C; i++) { for (int j = 1; j <= 50; j++) { for (int k = 1; k <= 50; k++) { to(&ans, dp[i][j][k]); } } } printf("%d\n", ans); return 0; }