#include #ifdef LOCAL #include "./debug.cpp" #else #define debug(...) #define print_line #endif using namespace std; using ll = long long; template struct segment_tree { using F = function; segment_tree(int n, F f, T e) { this->n = 1; while (this->n < n) { this->n *= 2; } this->size = n; this->f = f; this->e = e; dat = vector(this->n * 2 - 1, e); } void build(const vector &a) { for (int i = 0; i < (int)a.size(); i++) { dat[i + n - 1] = a[i]; } for (int i = n - 2; i >= 0; i--) { dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } } void set(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { return query(l, r, 0, 0, n); } T operator[](int i) { return dat[n - 1 + i]; } int size; private: int n; vector dat; F f; T e; T query(int left, int right, int i, int l, int r) { if (r <= left || right <= l) { return e; } else if (left <= l && r <= right) { return dat[i]; } else { int mid = (l + r) / 2; return f(query(left, right, i * 2 + 1, l, mid), query(left, right, i * 2 + 2, mid, r)); } } }; template segment_tree range_min_query(int n, T e = numeric_limits::max()) { auto f = [](T a, T b) { return min(a, b); }; return segment_tree(n, f, e); } template segment_tree range_max_query(int n, T e = numeric_limits::min()) { auto f = [](T a, T b) { return max(a, b); }; return segment_tree(n, f, e); } template segment_tree range_sum_query(int n, T e = 0) { auto f = [](T a, T b) { return a + b; }; return segment_tree(n, f, e); } struct S { ll val, len, base; }; S f(S a, S b) { S ret; ret.val = a.val + a.len * b.base + b.val; ret.len = a.len + b.len; ret.base = a.base + b.base; return ret; } S fe = S{0, 0, 0}; int main() { int N; ll H; cin >> N >> H; vector A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector B(N); for (int i = 0; i < N; i++) { cin >> B[i]; } segment_tree seg(N, f, fe); for (int i = 0; i < N; i++) { seg.set(i, S{B[i], 1, B[i]}); } vector pref(N + 1); for (int i = 0; i < N; i++) { pref[i + 1] = pref[i] + A[i]; } ll ans = 0; for (int i = 0; i < N; i++) { if (B[i] > H) { continue; } int lo = i, hi = N; while (hi - lo > 1) { int mid = (lo + hi) / 2; ll sumb = seg.query(i, mid + 1).val; if (sumb <= H) { lo = mid; } else { hi = mid; } } ans = max(ans, pref[lo + 1] - pref[i]); } cout << ans << endl; }