#include #define show(x) std::cerr << #x << " = " << (x) << std::endl using ll = long long; using ld = long double; constexpr ll MOD = 1000000007LL; template constexpr T INF() { return std::numeric_limits::max() / 16; } std::mt19937 mt{std::random_device{}()}; template std::vector Vec(const std::size_t n, T v) { return std::vector(n, v); } template auto Vec(const std::size_t n, Args... args) { return std::vector(n, Vec(args...)); } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { os << "["; for (const auto& p : v) { os << p << ","; } return (os << "]\n"); } int main() { int N, C; std::cin >> N >> C; std::vector L(N), W(N); for (int i = 0; i < N; i++) { std::cin >> L[i]; } for (int i = 0; i < N; i++) { std::cin >> W[i]; } L.push_back(100000); auto dp = Vec(N + 1, N + 1, 51, 0); for (int i = 0; i < N; i++) { dp[N][i][W[i]] = L[i]; } for (int i = 0; i <= C; i++) { auto tmp = (i >= 2 ? dp : Vec(N + 1, N + 1, 51, 0)); for (int pp = 0; pp <= N; pp++) { for (int p = 0; p < N; p++) { if (L[p] == L[pp]) { continue; } if (L[pp] > L[p]) { for (int n = 0; n < N; n++) { if (L[p] >= L[n] or L[pp] == L[n]) { continue; } for (int c = 0; c + W[n] <= C; c++) { tmp[p][n][c + W[n]] = std::max(tmp[p][n][c + W[n]], dp[pp][p][c] + L[n]); } } } else if (L[pp] < L[p]) { for (int n = 0; n < N; n++) { if (L[p] <= L[n] or L[pp] == L[n]) { continue; } for (int c = 0; c + W[n] <= C; c++) { tmp[p][n][c + W[n]] = std::max(tmp[p][n][c + W[n]], dp[pp][p][c] + L[n]); } } } } } dp = tmp; } // show(dp); int ans = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int c = 0; c <= C; c++) { ans = std::max(ans, dp[i][j][c]); } } } std::cout << ans << std::endl; return 0; }