#include #define F first #define S second #define all(x) begin(x), end(x) #define pb push_back #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #ifdef LOCAL #define HEHE freopen("in.txt", "r", stdin); #define debug(HEHE...) cerr << #HEHE << " = ", dout(HEHE) void dout() { std::cerr << '\n'; } template void dout(T t, U ...u) { std::cerr << t << ' '; dout(u...); } #else #define HEHE ios_base::sync_with_stdio(0), cin.tie(0); #define debug(...) 7122 #endif using namespace std; #define chmax(a, b) (a) = (a) > (b) ? (a) : (b) #define chmin(a, b) (a) = (a) < (b) ? (a) : (b) #define int long long signed main() { HEHE int n, k; cin >> n >> k; vector a(n + 1); FOR (i, 1, n) cin >> a[i]; partial_sum(all(a), begin(a)); priority_queue pq; int ans = INT64_MIN, cur = 0; FOR (i, 2, k) pq.push(a[i - 1]), cur -= a[i - 1]; chmax(ans, a[k] * k + cur); FOR (i, k + 1, n) { pq.push(a[i - 1]); cur -= a[i - 1]; cur += pq.top(); pq.pop(); chmax(ans, a[i] * k + cur); } cout << ans << '\n'; }