#include #include #include #include int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::size_t n, k; std::cin >> n >> k; std::vector a(n), b(n + 1); for (auto &e : a) std::cin >> e; for (std::size_t i = 0; i < n; ++i) { b[i + 1] = b[i] + a[i]; } long long ans = std::numeric_limits::min(); std::priority_queue pq; long long pq_sum = 0; // sum of top (k - 1) for (std::size_t x = 1; x <= n; ++x) { if (x >= k) { ans = std::max(ans, static_cast(k) * b[x] - pq_sum); } pq.push(b[x]); pq_sum += b[x]; if (pq.size() >= k) { long long rem_val = pq.top(); pq.pop(); pq_sum -= rem_val; } } std::cout << ans << std::endl; return 0; }