#include #include "testlib.h" void solve() { int n = inf.readInt(1, 500000); inf.readSpace(); int k = inf.readInt(1, n); inf.readEoln(); std::vector A = inf.readInts(n, -10'000'000, 10'000'000); inf.readEoln(); long long ans = 0; for (int i = 0; i < k; i++) { ans += (long long)A[i] * (i + 1); } long long tot = 0; long long l_tot = 0; std::priority_queue pq; for (auto a : A) { tot += a; if (int(pq.size()) >= k - 1) { long long score = tot * k - l_tot; if (score > ans) ans = score; } l_tot += tot; pq.push(tot); if (int(pq.size()) == k) { l_tot -= pq.top(); pq.pop(); } } std::cout << ans << std::endl; } int main(int argc, char *argv[]) { std::cin.tie(0)->sync_with_stdio(0); registerValidation(argc, argv); solve(); inf.readEof(); }