#include using namespace std; #include #include #include #include #include #include struct FastIO { FastIO() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); } }; inline FastIO fast_io_init; template std::istream& operator>>(std::istream& is, std::vector& v); template std::istream& operator>>(std::istream& is, std::pair& p) { return is >> p.first >> p.second; } template void read_tuple_impl(std::istream& is, Tuple& t, std::index_sequence) { (..., (is >> std::get(t))); } template std::istream& operator>>(std::istream& is, std::tuple& t) { read_tuple_impl(is, t, std::index_sequence_for{}); return is; } template std::istream& operator>>(std::istream& is, std::vector& v) { for (auto& elem : v) { is >> elem; } return is; } template struct is_pair : std::false_type {}; template struct is_pair> : std::true_type {}; template struct is_tuple : std::false_type {}; template struct is_tuple> : std::true_type {}; template struct is_vector : std::false_type {}; template struct is_vector> : std::true_type {}; template void adjust_zero_indexed(T& val) { using DecayedT = std::decay_t; if constexpr (is_pair::value) { adjust_zero_indexed(val.first); adjust_zero_indexed(val.second); } else if constexpr (is_tuple::value) { std::apply([](auto&... args) { (adjust_zero_indexed(args), ...); }, val); } else if constexpr (is_vector::value) { for (auto& elem : val) { adjust_zero_indexed(elem); } } else if constexpr (std::is_arithmetic_v && !std::is_same_v && !std::is_same_v && !std::is_same_v && !std::is_same_v && #if defined(__cpp_char8_t) !std::is_same_v && #endif !std::is_same_v && !std::is_same_v && !std::is_same_v) { --val; } else { } } using default_type = long; template void read(Args&... args) { (std::cin >> ... >> args); } template T read_val() { T val; std::cin >> val; return val; } template std::pair read_pair() { std::pair p; std::cin >> p; return p; } template std::tuple read_tuple() { std::tuple t; std::cin >> t; return t; } template && !std::is_same_v, int> = 0> std::vector read_vec(Size n, bool zero_indexed = false) { std::vector v(n); std::cin >> v; if (zero_indexed) { adjust_zero_indexed(v); } return v; } template std::vector read_vec(bool zero_indexed = false) { int n; std::cin >> n; return read_vec(n, zero_indexed); } template && !std::is_same_v, int> = 0> std::vector> read_vec_pair(Size n, bool zero_indexed = false) { return read_vec>(n, zero_indexed); } template std::vector> read_vec_pair(bool zero_indexed = false) { int n; std::cin >> n; return read_vec_pair(n, zero_indexed); } template && !std::is_same_v, int> = 0> std::vector> read_vec_tuple(Size n, bool zero_indexed = false) { return read_vec>(n, zero_indexed); } template std::vector> read_vec_tuple(bool zero_indexed = false) { int n; std::cin >> n; return read_vec_tuple(n, zero_indexed); } template std::vector> read_vec_grid(int h, int w, bool zero_indexed = false) { std::vector> grid(h, std::vector(w)); std::cin >> grid; if (zero_indexed) { adjust_zero_indexed(grid); } return grid; } template std::vector> read_vec_grid(bool zero_indexed = false) { int h, w; std::cin >> h >> w; return read_vec_grid(h, w, zero_indexed); } template && !std::is_same_v, int> = 0> std::vector> read_vec_var(Size n, bool zero_indexed = false) { std::vector> res(n); for (int i = 0; i < static_cast(n); ++i) { int m; std::cin >> m; res[i] = read_vec(m, zero_indexed); } return res; } template std::vector> read_vec_var(bool zero_indexed = false) { int n; std::cin >> n; return read_vec_var(n, zero_indexed); } template T read_zero_idx() { T val; std::cin >> val; adjust_zero_indexed(val); return val; } inline std::vector> read_graph(int n, int m, bool directed = false) { std::vector> g(n); for (int i = 0; i < m; ++i) { int u = read_zero_idx(); int v = read_zero_idx(); g[u].push_back(v); if (!directed) { g[v].push_back(u); } } return g; } inline std::vector> read_graph(bool directed = false) { int n, m; std::cin >> n >> m; return read_graph(n, m, directed); } #include #include #include #include #define ALL(a) (a).begin(), (a).end() using i128 = __int128; template inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return true; } return false; } template inline T div_ceil(T a, T b) { if (a > 0) return a / b + (a % b != 0); return a / b; } template inline T div_floor(T a, T b) { if (a < 0) return a / b - (a % b != 0); return a / b; } template inline T mod(T a, T m) { a %= m; if (a < 0) a += m; return a; } template inline constexpr T INF = std::numeric_limits::max() / 2; template <> inline constexpr float INF = std::numeric_limits::infinity(); template <> inline constexpr double INF = std::numeric_limits::infinity(); template <> inline constexpr long double INF = std::numeric_limits::infinity(); template inline std::vector iota_vec(int n, T start = 0) { std::vector v(n); std::iota(v.begin(), v.end(), start); return v; } template inline std::vector doubled_vec(const std::vector& v) { std::vector res; res.reserve(v.size() * 2); res.insert(res.end(), v.begin(), v.end()); res.insert(res.end(), v.begin(), v.end()); return res; } inline void Yes(bool b = true) { std::println("{}", (b ? "Yes" : "No")); } inline void No() { std::println("No"); } #ifdef LOCAL #include #else #define debug(...) #endif #include #include #include #include #include #include #include #include #include template > struct SWAG { private: struct Node { T val; T acc; }; std::vector front, back; Op op; T identity; public: using size_type = std::size_t; SWAG(Op op_, T identity_) : op(op_), identity(identity_) { } inline T all_prod() const { if (empty()) return identity; if (front.empty()) return back.back().acc; if (back.empty()) return front.back().acc; return op(front.back().acc, back.back().acc); } inline size_type size() const { return front.size() + back.size(); } inline bool empty() const { return front.empty() && back.empty(); } void push(const T& x) { if (back.empty()) { back.push_back({x, x}); } else { back.push_back({x, op(back.back().acc, x)}); } } void pop() { assert(!empty()); if (empty()) return; if (front.empty()) { while (!back.empty()) { T v = back.back().val; back.pop_back(); T acc = (front.empty()) ? v : op(v, front.back().acc); front.push_back({v, acc}); } } front.pop_back(); } const T& front_element() const { assert(!empty()); if (empty()) throw std::out_of_range("SWAG::front_element called on empty queue"); if (front.empty()) { return back.front().val; } return front.back().val; } void clear() { front.clear(); back.clear(); } }; template inline SWAG make_swag(Op op, T identity) { return SWAG(op, identity); } template inline auto make_min_swag(T inf = std::numeric_limits::max()) { auto op = [](const T& a, const T& b) { return std::min(a, b); }; return SWAG(op, inf); } template inline auto make_max_swag(T minf = std::numeric_limits::lowest()) { auto op = [](const T& a, const T& b) { return std::max(a, b); }; return SWAG(op, minf); } template inline auto make_sum_swag(T zero = T{}) { auto op = std::plus{}; return SWAG(op, zero); } template inline auto make_gcd_swag() { auto op = [](const T& a, const T& b) { return std::gcd(a, b); }; return SWAG(op, T{0}); } void solve() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; read(N, M); auto A = read_vec(N); auto B = read_vec(N); auto swag = make_sum_swag(0L); long ans = 0; for (auto [a, b] : ranges::views::zip(A, B)) { long cur = a - b >= 0 ? a - b : 0; swag.push(cur); if (ssize(swag) > M) swag.pop(); if (ssize(swag) <= M) chmax(ans, swag.all_prod()); } println("{}", ans); } int main() { solve(); }