#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> N >> C; rep(i, 0, N) cin >> L[i]; rep(i, 0, N) cin >> W[i]; rep(a, 0, N) rep(b, 0, N) if (L[a] != L[b] and W[a] + W[b] <= C) dp[2][a][b][C - W[a] - W[b]] = L[a] + L[b]; rep(len, 2, C) rep(prepre, 0, N) rep(pre, 0, N) rep(c, 0, C) if (0 < dp[len][prepre][pre][c]) { rep(nxt, 0, N) if (L[prepre] != L[nxt] and L[pre] != L[nxt]) { if (L[prepre] < L[pre] and L[pre] > L[nxt] and 0 <= c - W[nxt]) { chmax(dp[len + 1][pre][nxt][c - W[nxt]], dp[len][prepre][pre][c] + L[nxt]); } if (L[prepre] > L[pre] and L[pre] < L[nxt] and 0 <= c - W[nxt]) { chmax(dp[len + 1][pre][nxt][c - W[nxt]], dp[len][prepre][pre][c] + L[nxt]); } } } int ans = 0; rep(len, 3, C + 1) rep(prepre, 0, N) rep(pre, 0, N) rep(c, 0, C) chmax(ans, dp[len][prepre][pre][c]); cout << ans << endl; }