import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum inf = 1 << 30; void main() { int n, c; scan(n, c); auto len = readln.split.to!(int[]); auto w = readln.split.to!(int[]); // dp(総コスト, 2個前の竹の長さ, 1個前の竹の長さ) = (竹の長さの総和のmax) auto dp = new int[][][](c + 1, 51, 51); fillAll(dp, -inf); foreach (i ; 0 .. n) { foreach (j ; 0 .. n) { if (len[i] == len[j]) continue; if (w[i] + w[j] > c) continue; debug { writefln("%d, %d, %d", w[i] + w[j], len[i], len[j]); } dp[w[i] + w[j]][len[i]][len[j]] = max(dp[w[i] + w[j]][len[i]][len[j]], len[i] + len[j]); } } int ans; foreach (ci ; 0 .. c + 1) { foreach (i ; 0 .. 51) { foreach (j ; 0 .. 51) { if (dp[ci][i][j] == -inf) continue; foreach (k ; 0 .. n) { if (len[k] == i || len[k] == j) continue; if (i < j && j < len[k]) continue; if (i > j && j > len[k]) continue; if (ci + w[k] > c) continue; debug { writefln("(%d, %d, %d)", i, j, len[k]); } dp[ci + w[k]][j][len[k]] = max(dp[ci + w[k]][j][len[k]], dp[ci][i][j] + len[k]); ans = max(ans, dp[ci + w[k]][j][len[k]]); } } } } writeln(ans); } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }