#pragma GCC optimize ("O3") #pragma GCC target ("avx") #include "bits/stdc++.h" // define macro "/D__MAI" using namespace std; typedef long long int ll; #define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:v){cout< ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout< ostream& operator <<(ostream &o, const pair p) { o << "(" << p.first << ":" << p.second << ")"; return o; } template T& maxset(T& to, const T& val) { return to = max(to, val); } template T& minset(T& to, const T& val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(0); } mt19937_64 randdev(8901016); inline ll rand_range(ll l, ll h) { return uniform_int_distribution(l, h)(randdev); } #if defined(_WIN32) || defined(_WIN64) #define getchar_unlocked _getchar_nolock #define putchar_unlocked _putchar_nolock #elif __GNUC__ #else #define getchar_unlocked getchar #define putchar_unlocked putchar #endif namespace { #define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E) class MaiScanner { public: template void input_integer(T& var) { var = 0; T sign = 1; int cc = getchar_unlocked(); for (; cc<'0' || '9'>(int& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(long long& var) { input_integer(var); return *this; } inline MaiScanner& operator>>(string& var) { int cc = getchar_unlocked(); for (; !isvisiblechar(cc); cc = getchar_unlocked()); for (; isvisiblechar(cc); cc = getchar_unlocked()) var.push_back(cc); } template void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; } }; class MaiPrinter { public: template void output_integer(T var) { if (var == 0) { putchar_unlocked('0'); return; } if (var < 0) putchar_unlocked('-'), var = -var; char stack[32]; int stack_p = 0; while (var) stack[stack_p++] = '0' + (var % 10), var /= 10; while (stack_p) putchar_unlocked(stack[--stack_p]); } inline MaiPrinter& operator<<(char c) { putchar_unlocked(c); return *this; } inline MaiPrinter& operator<<(int var) { output_integer(var); return *this; } inline MaiPrinter& operator<<(long long var) { output_integer(var); return *this; } inline MaiPrinter& operator<<(char* str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; } inline MaiPrinter& operator<<(const string& str) { const char* p = str.c_str(); const char* l = p + str.size(); while (p < l) putchar_unlocked(*p++); return *this; } template void join(IT begin, IT end, char sep = '\n') { for (auto it = begin; it != end; ++it) *this << *it << sep; } }; } MaiScanner scanner; MaiPrinter printer; #define TIME chrono::system_clock::now() #define MILLISEC(t) (chrono::duration_cast(t).count()) // ================================================================= // ================================================================= struct bamboo { bamboo() { } bamboo(int l, int w) {} int length; int weight; }; ostream& operator <<(ostream &o, const bamboo p) { o << make_pair(p.length, p.weight); return o; } inline bool iskadomatsu(int a, int b, int c) { return a!=c && ((ac)||(a>b && b bb; // 最も効率が良い竹3つ int lightest_weight[3]; // 最も長い竹3つの長さ int longest_length[3]; auto stopwatch = TIME; int best = 0; void dfs(vector selected) { //debugv(selected) // 現在の重さと長さを計算しておく. int cw = 0, cl = 0; for (auto b : selected) cw += b.weight, cl += b.length; if (selected.size() >= 3) best = max(best, cl); if (capacity == cw) return; // 近似解を求める. int approximation_best = cl; { int w = cw; for (int i = 0;; i = ++i%3) { w += lightest_weight[i]; if (w > capacity) break; approximation_best += longest_length[i]; } } // double approximation_best = cl; // approximation_best += (double)bb[0].length/(double)bb[0].weight*(double)(capacity - cw); // 近似解でもbestを超えないならば,枝を刈る if (approximation_best <= best) return; // 2つ前 bamboo prepre = selected[selected.size() - 2]; // 1つ前 bamboo pre = selected[selected.size() - 1]; //if (MILLISEC(TIME-stopwatch) > 1900) return; repeat(i, n) { if (bb[i].weight + cw > capacity) continue; if (iskadomatsu(prepre.length, pre.length, bb[i].length)) { selected.push_back(bb[i]); dfs(selected); selected.pop_back(); } } return; } int main() { scanner >> n >> capacity; bb.resize(n); repeat(i, n) { scanner >> bb[i].length; } repeat(i, n) { scanner >> bb[i].weight; } // 同じ長さの竹が複数あっても意味が無いので,減らす. { map zip; for (auto b : bb) if (zip.count(b.length) == 0) zip[b.length] = b.weight; else zip[b.length] = min(zip[b.length], b.weight); bb.clear(); for (auto z : zip) bb.emplace_back(z.first, z.second); } n = bb.size(); if (n <= 2) bye("0"); // 最も軽い竹3種を選んでおき,その重さを記憶しておく // 同様に,最も長い竹3種を選んでおき,その長さを記憶しておく { vector ws; vector ls; for (auto b : bb) ws.push_back(b.weight), ls.push_back(b.length); sort(ALL(ws)); sort(ALL(ls), greater()); repeat(i, 3) lightest_weight[i] = ws[i], longest_length[i] = ls[i]; } // 効率(L/W)降順にソート sort(ALL(bb), [](bamboo l, bamboo r) {return l.length > r.length; }); stable_sort(ALL(bb), [](bamboo l, bamboo r) { return l.length*r.weight > r.length*l.weight; }); // 最初の2つの竹を決めてから,dfs repeat(first, n) { repeat(second, n) { if (bb[first].length != bb[second].length) dfs({ bb[first], bb[second] }); } } cout << best << endl; return 0; }