#include "bits/stdc++.h" using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define array2(type, x, y) array, x> #define vector2(type) vector > #define out(...) printf(__VA_ARGS__) int dxy[] = {0, 1, 0, -1, 0}; /*================================*/ int N,C; struct Bamboo { int l, w; }; bool is_kadomatsu(int a, int b, int c) { if (a==b || b==c || c==a) return false; if (!(b < a && b < c) && !(b > a && b > c)) return false; return true; } signed main() { #if DEBUG std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif cin>>N>>C; int L = 50; vector B(N); REP(i,N) cin >> B[i].l; REP(i,N) cin >> B[i].w; // 2つ前長さ, 1つ前長さ, 所持金 => 累計長さ int DP[L+1][L+1][C+1]; REP(i,L+1)REP(j,L+1)REP(k,C+1)DP[i][j][k]=-1; DP[0][0][C] = 0; int ans = 0; RREP(k,C+1) REP(i,L+1) REP(j,L+1) { if (j == 0 && i != 0) continue; // 2つ前が未定なら一つ前も未定である必要がある if (DP[i][j][k] < 0) continue; for (auto b : B) { if (k < b.w) continue; // 残高不足 if (i!=0 && j!=0 && !is_kadomatsu(i, j, b.l)) continue; int ret = DP[j][b.l][k-b.w] = DP[i][j][k] + b.l; if(i!=0 && j!=0) ans = max(ans, ret); } } cout << ans << endl; return 0; }